-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDatabaseConnectionPool.php
More file actions
31 lines (27 loc) · 899 Bytes
/
Copy pathDatabaseConnectionPool.php
File metadata and controls
31 lines (27 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
namespace App\ObjectPool;
class DatabaseConnectionPool
{
private array $availableConnections = [];
private array $inUseConnections = [];
public function get(): DatabaseConnection
{
if (count($this->availableConnections) === 0) {
$connection = DatabaseConnection::getInstance();
$this->inUseConnections[] = $connection;
return $connection;
} else {
$connection = array_pop($this->availableConnections);
$this->inUseConnections[] = $connection;
return $connection;
}
}
public function release(DatabaseConnection $connection): void
{
$index = array_search($connection, $this->inUseConnections, true);
if ($index !== false) {
unset($this->inUseConnections[$index]);
$this->availableConnections[] = $connection;
}
}
}