Skip to content

Commit

Permalink
fix: Connection pool in Swoole does not support in-memory or temporar…
Browse files Browse the repository at this point in the history
…y SQLite databases
  • Loading branch information
deminy committed Feb 17, 2024
1 parent 031eba5 commit eaf6a43
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
Fixed:

* Fix accessing undefined properties in method \Swoole\NameResolver::checkResponse(). ([commit](https://github.com/swoole/library/commit/7a6396e45f4d4517a049584a746285d6501cf71d))
* Fix the implementation of method `\Swoole\MultibyteStringObject::chunk()`.
* Fix the implementation of method `\Swoole\MultibyteStringObject::chunk()`. ([commit](https://github.com/swoole/library/commit/031eba5f6db2ffac66ce1cca6d1d63a213203724))
* Connection pool in Swoole does not support in-memory or temporary SQLite databases.

Changed:

* Refactor: Rename parameter in method `\Swoole\Database\PDOStatementProxy::setFetchMode()` for consistency.
* Refactor: Rename parameter in method `\Swoole\MultibyteStringObject::substr()` for consistency.
* Refactor: Enhance method `\Swoole\FastCGI\Message::withBody()` with explicit parameter type.
* Refactor: Rename parameter and default value of method `\Swoole\StringObject::chunkSplit()` for consistency.
* Refactor: Rename parameter in method `\Swoole\StringObject::chunk()` for consistency.
* Refactor: Rename parameter and default value of method `\Swoole\StringObject::chunkSplit()` for consistency. ([commit](https://github.com/swoole/library/commit/031eba5f6db2ffac66ce1cca6d1d63a213203724))
* Refactor: Rename parameter in method `\Swoole\StringObject::chunk()` for consistency. ([commit](https://github.com/swoole/library/commit/031eba5f6db2ffac66ce1cca6d1d63a213203724))
* FastCGI: Make constructor argument required for records. ([commit](https://github.com/swoole/library/commit/497bb74eaad51f661c91bc936f976b8660ce716c))

## 5.1.2 (2024-01-24)
Expand Down
10 changes: 10 additions & 0 deletions src/core/Database/PDOPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ private function createDSN(string $driver): string
$dsn = 'oci:dbname=' . ($this->config->hasUnixSocket() ? $this->config->getUnixSocket() : $this->config->getHost()) . ':' . $this->config->getPort() . '/' . $this->config->getDbname() . ';charset=' . $this->config->getCharset();
break;
case 'sqlite':
// There are three types of SQLite databases: databases on disk, databases in memory, and temporary
// databases (which are deleted when the connections are closed). It doesn't make sense to use
// connection pool for the latter two types of databases, because each connection connects to a
//different in-memory or temporary SQLite database.
if ($this->config->getDbname() === '') {
throw new \Exception('Connection pool in Swoole does not support temporary SQLite databases.');
}
if ($this->config->getDbname() === ':memory:') {
throw new \Exception('Connection pool in Swoole does not support creating SQLite databases in memory.');
}
$dsn = 'sqlite:' . $this->config->getDbname();
break;
default:
Expand Down
29 changes: 24 additions & 5 deletions tests/unit/Database/PDOPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ class PDOPoolTest extends TestCase
{
use HookFlagsTrait;

protected static $sqliteDatabaseFile;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$sqliteDatabaseFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'swoole_pdo_pool_sqlite_test.db';
if (file_exists(self::$sqliteDatabaseFile)) {
unlink(self::$sqliteDatabaseFile);
}
}

public static function tearDownAfterClass(): void
{
if (file_exists(self::$sqliteDatabaseFile)) {
unlink(self::$sqliteDatabaseFile);
}
parent::tearDownAfterClass();
}

public function testPutWhenErrorHappens()
{
self::saveHookFlags();
Expand Down Expand Up @@ -108,6 +127,7 @@ public function testPostgres(): void
}

$waitGroup->wait();
$pool->close();
self::restoreHookFlags();
});
}
Expand Down Expand Up @@ -157,6 +177,7 @@ public function testOracle(): void
}

$waitGroup->wait();
$pool->close();
self::restoreHookFlags();
});
}
Expand All @@ -166,11 +187,8 @@ public function testSqlite(): void
self::saveHookFlags();
self::setHookFlags(SWOOLE_HOOK_ALL);
run(function () {
$config = (new PDOConfig())
->withDriver('sqlite')
->withHost('sqlite::memory:')
;
$pool = new PDOPool($config, 10);
$config = (new PDOConfig())->withDriver('sqlite')->withDbname(self::$sqliteDatabaseFile);
$pool = new PDOPool($config, 10);

$pdo = $pool->get();
$pdo->exec('CREATE TABLE IF NOT EXISTS test(id INT);');
Expand All @@ -194,6 +212,7 @@ public function testSqlite(): void
}

$waitGroup->wait();
$pool->close();
self::restoreHookFlags();
});
}
Expand Down

2 comments on commit eaf6a43

@deminy
Copy link
Member Author

@deminy deminy commented on eaf6a43 Feb 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NathanFreeman 请看看有什么建议或者需要调整的地方。

@NathanFreeman
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deminy 我觉得可以

Please sign in to comment.