This commit is contained in:
2023-04-17 01:39:31 +08:00
parent 2e258c7e08
commit 409f75eff6
2 changed files with 24 additions and 39 deletions
+20 -27
View File
@@ -77,28 +77,17 @@ class Connection extends Component
private ?Schema $_schema = null; private ?Schema $_schema = null;
/**
* @param EventProvider $eventProvider
* @param Pool $connections
* @param ContainerInterface $container
* @param array $config
* @throws Exception
*/
public function __construct(public EventProvider $eventProvider, public Pool $connections, public ContainerInterface $container, array $config = [])
{
parent::__construct();
}
/** /**
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function init(): void public function init(): void
{ {
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0); $eventProvider = Kiri::getDi()->get(EventProvider::class);
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0); $eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
$eventProvider->on(Commit::class, [$this, 'commit'], 0);
$this->initConnections(); $this->initConnections();
} }
@@ -106,10 +95,12 @@ class Connection extends Component
/** /**
* @return void * @return void
* @throws ReflectionException
*/ */
public function initConnections(): void public function initConnections(): void
{ {
$this->connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([ $connections = Kiri::getDi()->get(Pool::class);
$connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([
'cds' => $this->cds, 'cds' => $this->cds,
'username' => $this->username, 'username' => $this->username,
'password' => $this->password, 'password' => $this->password,
@@ -175,11 +166,8 @@ class Connection extends Component
*/ */
public function getConnection(): PDO public function getConnection(): PDO
{ {
return $this->connections->get($this->cds); $connections = Kiri::getDi()->get(Pool::class);
if ($client === false) { return $connections->get($this->cds);
throw new Exception('waite db client timeout.');
}
return $client;
} }
@@ -237,7 +225,8 @@ class Connection extends Component
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
} }
$this->connections->push($this->cds, $pdo); $connections = Kiri::getDi()->get(Pool::class);
$connections->push($this->cds, $pdo);
Context::remove($this->cds); Context::remove($this->cds);
} }
} }
@@ -254,7 +243,8 @@ class Connection extends Component
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->commit(); $pdo->commit();
} }
$this->connections->push($this->cds, $pdo); $connections = Kiri::getDi()->get(Pool::class);
$connections->push($this->cds, $pdo);
Context::remove($this->cds); Context::remove($this->cds);
} }
} }
@@ -280,7 +270,8 @@ class Connection extends Component
*/ */
public function release(PDO $PDO) public function release(PDO $PDO)
{ {
$this->connections->push($this->cds, $PDO); $connections = Kiri::getDi()->get(Pool::class);
$connections->push($this->cds, $PDO);
} }
@@ -291,7 +282,8 @@ class Connection extends Component
*/ */
public function clear_connection() public function clear_connection()
{ {
$this->connections->clean($this->cds); $connections = Kiri::getDi()->get(Pool::class);
$connections->clean($this->cds);
} }
@@ -300,7 +292,8 @@ class Connection extends Component
*/ */
public function disconnect() public function disconnect()
{ {
$this->connections->clean($this->cds); $connections = Kiri::getDi()->get(Pool::class);
$connections->clean($this->cds);
} }
} }
+1 -9
View File
@@ -23,19 +23,11 @@ use Kiri\Di\Inject\Container;
class DatabasesProviders extends Providers class DatabasesProviders extends Providers
{ {
/**
* @var EventProvider
*/
#[Container(EventProvider::class)]
public EventProvider $provider;
/** /**
* @param LocalService $application * @param LocalService $application
* @return void * @return void
* @throws ContainerExceptionInterface * @throws \ReflectionException
* @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public function onImport(LocalService $application): void public function onImport(LocalService $application): void
{ {