3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Sharding;
22 use Doctrine\DBAL\Connection;
23 use Doctrine\DBAL\Event\ConnectionEventArgs;
24 use Doctrine\DBAL\Events;
25 use Doctrine\DBAL\Driver;
26 use Doctrine\DBAL\Configuration;
28 use Doctrine\Common\EventManager;
30 use Doctrine\DBAL\Sharding\ShardChoser\ShardChoser;
33 * Sharding implementation that pools many different connections
34 * internally and serves data from the currently active connection.
36 * The internals of this class are:
38 * - All sharding clients are specified and given a shard-id during
40 * - By default, the global shard is selected. If no global shard is configured
41 * an exception is thrown on access.
42 * - Selecting a shard by distribution value delegates the mapping
43 * "distributionValue" => "client" to the ShardChooser interface.
44 * - An exception is thrown if trying to switch shards during an open
47 * Instantiation through the DriverManager looks like:
51 * $conn = DriverManager::getConnection(array(
52 * 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
53 * 'driver' => 'pdo_mysql',
54 * 'global' => array('user' => '', 'password' => '', 'host' => '', 'dbname' => ''),
56 * array('id' => 1, 'user' => 'slave1', 'password', 'host' => '', 'dbname' => ''),
57 * array('id' => 2, 'user' => 'slave2', 'password', 'host' => '', 'dbname' => ''),
59 * 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
61 * $shardManager = $conn->getShardManager();
62 * $shardManager->selectGlobal();
63 * $shardManager->selectShard($value);
65 * @author Benjamin Eberlei <kontakt@beberlei.de>
67 class PoolingShardConnection extends Connection
72 private $activeConnections;
77 private $activeShardId;
87 private $shardManager;
89 public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
91 if ( !isset($params['global']) || !isset($params['shards'])) {
92 throw new \InvalidArgumentException("Connection Parameters require 'global' and 'shards' configurations.");
95 if ( !isset($params['shardChoser'])) {
96 throw new \InvalidArgumentException("Missing Shard Choser configuration 'shardChoser'");
99 if (is_string($params['shardChoser'])) {
100 $params['shardChoser'] = new $params['shardChoser'];
103 if ( ! ($params['shardChoser'] instanceof ShardChoser)) {
104 throw new \InvalidArgumentException("The 'shardChoser' configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser");
107 $this->connections[0] = array_merge($params, $params['global']);
109 foreach ($params['shards'] as $shard) {
110 if ( ! isset($shard['id'])) {
111 throw new \InvalidArgumentException("Missing 'id' for one configured shard. Please specificy a unique shard-id.");
114 if ( !is_numeric($shard['id']) || $shard['id'] < 1) {
115 throw new \InvalidArgumentException("Shard Id has to be a non-negative number.");
118 if (isset($this->connections[$shard['id']])) {
119 throw new \InvalidArgumentException("Shard " . $shard['id'] . " is duplicated in the configuration.");
122 $this->connections[$shard['id']] = array_merge($params, $shard);
125 parent::__construct($params, $driver, $config, $eventManager);
129 * Connect to a given shard
131 * @param mixed $shardId
134 public function connect($shardId = null)
136 if ($shardId === null && $this->_conn) {
140 if ($shardId !== null && $shardId === $this->activeShardId) {
144 if ($this->getTransactionNestingLevel() > 0) {
145 throw new ShardingException("Cannot switch shard when transaction is active.");
148 $this->activeShardId = (int)$shardId;
150 if (isset($this->activeConnections[$this->activeShardId])) {
151 $this->_conn = $this->activeConnections[$this->activeShardId];
155 $this->_conn = $this->activeConnections[$this->activeShardId] = $this->connectTo($this->activeShardId);
157 if ($this->_eventManager->hasListeners(Events::postConnect)) {
158 $eventArgs = new Event\ConnectionEventArgs($this);
159 $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
167 * Connect to a specific connection
169 * @param string $shardId
172 protected function connectTo($shardId)
174 $params = $this->getParams();
176 $driverOptions = isset($params['driverOptions']) ? $params['driverOptions'] : array();
178 $connectionParams = $this->connections[$shardId];
180 $user = isset($connectionParams['user']) ? $connectionParams['user'] : null;
181 $password = isset($connectionParams['password']) ? $connectionParams['password'] : null;
183 return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
186 public function isConnected($shardId = null)
188 if ($shardId === null) {
189 return $this->_conn !== null;
192 return isset($this->activeConnections[$shardId]);
195 public function close()
198 $this->activeConnections = null;