Skip to content

Commit 4907ff9

Browse files
authored
连接中心增加 createConnection() 方法,重构 Imi\Db\Db::getNewInstance() (#699)
1 parent da74e38 commit 4907ff9

File tree

6 files changed

+72
-60
lines changed

6 files changed

+72
-60
lines changed

doc/base/version/2.1-3.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ return [
8181

8282
* 事件名称规则统一改为全小写,为保持兼容请[参考文档](/v3.0/event/index.html#3.0%20兼容性)
8383

84+
### 数据库
85+
86+
* `Imi\Db\Db::getNewInstance()` 返回值改为 `Imi\ConnectionCenter\Contract\IConnection`
87+
8488
### 模型
8589

8690
* UUID 发号器的 `type` 类型改为枚举,大小写有所变化

src/Components/connection-center/src/ConnectionCenter.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,23 @@ public function getConnectionManagers(): array
7474
return $this->connectionManagers;
7575
}
7676

77+
/**
78+
* 获取连接.
79+
*
80+
* 需要调用 getInstance() 获取实际的对象进行操作.
81+
*
82+
* 需要自行管理连接生命周期,连接对象释放即归还连接池.
83+
*/
7784
public function getConnection(string $name): IConnection
7885
{
7986
return $this->getConnectionManager($name)->getConnection();
8087
}
8188

89+
/**
90+
* 获取连接.
91+
*
92+
* 自动管理生命周期,当前上下文结束时自动释放.
93+
*/
8294
public function getRequestContextConnection(string $name): IConnection
8395
{
8496
$requestContext = RequestContext::getContext();
@@ -119,4 +131,22 @@ public function getRequestContextConnection(string $name): IConnection
119131

120132
return $connection;
121133
}
134+
135+
/**
136+
* 创建新的连接.
137+
*
138+
* 返回的连接是实际的对象,不受连接管理器管理生命周期.
139+
*/
140+
public function createConnection(?string $name = null, bool $autoConnect = true): mixed
141+
{
142+
$driver = self::getConnectionManager($name)->getDriver();
143+
$instance = $driver->createInstance();
144+
145+
if ($autoConnect)
146+
{
147+
return $driver->connect($instance);
148+
}
149+
150+
return $instance;
151+
}
122152
}

src/Components/connection-center/src/Facade/ConnectionCenter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @method static IConnectionManager[] getConnectionManagers()
1616
* @method static \Imi\ConnectionCenter\Contract\IConnection getConnection(string $name)
1717
* @method static \Imi\ConnectionCenter\Contract\IConnection getRequestContextConnection(string $name)
18+
* @method static mixed createConnection(?string $name = NULL, bool $autoConnect = true)
1819
*/
1920
#[
2021
\Imi\Facade\Annotation\Facade(class: \Imi\ConnectionCenter\AppConnectionCenter::class)

src/Components/connection-center/tests/Tests/ConnectionCenterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ public function testRequestContextDestroy(): void
198198
}
199199
}
200200

201+
public function testCreateConnection(): void
202+
{
203+
foreach (self::$names as $name)
204+
{
205+
$connection1 = self::$connectionCenter->createConnection($name);
206+
$this->assertTrue($connection1->connected);
207+
208+
$connection2 = self::$connectionCenter->createConnection($name, false);
209+
$this->assertFalse($connection2->connected);
210+
211+
$this->assertTrue(self::$connectionCenter->createConnection($name) !== self::$connectionCenter->createConnection($name));
212+
}
213+
}
214+
201215
/**
202216
* @depends testNewInstance
203217
*/

src/Components/database/src/Db.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Imi\Db;
66

77
use Imi\Config;
8+
use Imi\ConnectionCenter\Contract\IConnection;
89
use Imi\ConnectionCenter\Facade\ConnectionCenter;
910
use Imi\Db\ConnectionCenter\DatabaseDriverConfig;
1011
use Imi\Db\Interfaces\IDb;
@@ -19,28 +20,42 @@ class Db
1920
use \Imi\Util\Traits\TStaticClass;
2021

2122
/**
22-
* 获取新的数据库连接实例.
23+
* 获取数据库连接实例,每个上下文中共用一个.
2324
*
2425
* @param string|null $poolName 连接池名称
2526
* @param int $queryType 查询类型
2627
*/
27-
public static function getNewInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
28+
public static function getInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
2829
{
29-
$driver = ConnectionCenter::getConnectionManager(self::parsePoolName($poolName, $queryType))->getDriver();
30-
$instance = $driver->createInstance();
30+
return ConnectionCenter::getRequestContextConnection(self::parsePoolName($poolName, $queryType))->getInstance();
31+
}
3132

32-
return $driver->connect($instance);
33+
/**
34+
* 创建新的数据库连接实例.
35+
*
36+
* @param string|null $poolName 连接池名称
37+
* @param int $queryType 查询类型
38+
*/
39+
public static function createInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
40+
{
41+
return ConnectionCenter::createConnection(self::parsePoolName($poolName, $queryType));
3342
}
3443

3544
/**
36-
* 获取数据库连接实例,每个RequestContext中共用一个.
45+
* 获取新的数据库连接实例.
46+
*
47+
* 返回的是连接池连接对象,需调用 `getInstance()` 获取实际的对象进行操作.
48+
*
49+
* 连接对象释放即断开连接,操作期间需要保证连接对象不被释放.
50+
*
51+
* 如果不是特别必要,不推荐使用此方法!!!
3752
*
3853
* @param string|null $poolName 连接池名称
3954
* @param int $queryType 查询类型
4055
*/
41-
public static function getInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
56+
public static function getNewInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IConnection
4257
{
43-
return ConnectionCenter::getRequestContextConnection(self::parsePoolName($poolName, $queryType))->getInstance();
58+
return ConnectionCenter::getConnection(self::parsePoolName($poolName, $queryType));
4459
}
4560

4661
/**

src/Components/database/src/Listener/CheckPoolResource.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)