Skip to content

Commit 595c4c8

Browse files
Fixed bug that PostgresSQL can't work when create connection timed out. (#5327)
Co-authored-by: 李铭昕 <[email protected]>
1 parent 9e51de6 commit 595c4c8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Connectors/PostgresSqlSwooleExtConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function createConnection(array $config): PostgreSQL
5757
));
5858

5959
if ($result === false) {
60-
throw new Exception($connection->error);
60+
throw new Exception($connection->error ?? 'Create connection failed, Please check the database configuration.');
6161
}
6262

6363
return $connection;

tests/Cases/PostgreSqlSwooleExtConnectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
namespace HyperfTest\Database\PgSQL\Cases;
1313

14+
use Exception;
1415
use Hyperf\Database\Connection;
1516
use Hyperf\Database\Connectors\ConnectionFactory;
1617
use Hyperf\Database\Exception\QueryException;
@@ -89,4 +90,25 @@ public function testAffectingStatementWithWrongSql()
8990

9091
$connection->affectingStatement('UPDATE xx SET x = 1 WHERE id = 1');
9192
}
93+
94+
public function testCreateConnectionTimedOut()
95+
{
96+
if (SWOOLE_MAJOR_VERSION < 5) {
97+
$this->markTestSkipped('PostgreSql requires Swoole version >= 5.0.0');
98+
}
99+
100+
$connection = $this->connectionFactory->make([
101+
'driver' => 'pgsql-swoole',
102+
'host' => 'non-existent-host.internal',
103+
'port' => 5432,
104+
'database' => 'postgres',
105+
'username' => 'postgres',
106+
'password' => 'postgres',
107+
]);
108+
109+
$this->expectException(Exception::class);
110+
$this->expectExceptionMessage('Create connection failed, Please check the database configuration.');
111+
112+
$connection->affectingStatement('UPDATE xx SET x = 1 WHERE id = 1');
113+
}
92114
}

0 commit comments

Comments
 (0)