h2.Zend
Я только что установил Zend PDO_MYSQL для разделения подключений для чтения и записи.Для этого вам нужно просто указать дополнительные параметры в конфигах приложения:
'databases' => array (
'gtf' => array(
'adapter' => 'PDO_MYSQL',
'params' => array(
'host' => 'read.com',
'host_write' => 'write-database-host.com',
'dbname' => 'database',
'username' => 'reader',
'password' => 'reader',
'username_write' => 'writer',
'password_write' => 'writer',
'charset' => 'utf8'
)
),
Здесь все запросы «SELECT ...» будут использовать host .И все остальные запросы будут использовать * host_write *.Если host_write не указан, то во всех запросах используется host .
Patch:
diff --git a/Modules/Tools/Externals/Zend/Db/Adapter/Abstract.php b/Modules/Tools/Externals/Zend/Db/Adapter/Abstract.php
index 5ed3283..d6fccd6 100644
--- a/Modules/Tools/Externals/Zend/Db/Adapter/Abstract.php
+++ b/Modules/Tools/Externals/Zend/Db/Adapter/Abstract.php
@@ -85,6 +85,14 @@ abstract class Zend_Db_Adapter_Abstract
* @var object|resource|null
*/
protected $_connection = null;
+
+
+ /**
+ * Database connection
+ *
+ * @var object|resource|null
+ */
+ protected $_connection_write = null;
/**
* Specifies the case of column names retrieved in queries
@@ -299,10 +307,13 @@ abstract class Zend_Db_Adapter_Abstract
*
* @return object|resource|null
*/
- public function getConnection()
+ public function getConnection($read_only_connection = true)
{
$this->_connect();
- return $this->_connection;
+ if (!$read_only_connection && $this->_connection_write)
+ return $this->_connection_write;
+ else
+ return $this->_connection;
}
/**
diff --git a/Modules/Tools/Externals/Zend/Db/Adapter/Pdo/Abstract.php b/Modules/Tools/Externals/Zend/Db/Adapter/Pdo/Abstract.php
index d7f6d8a..ee63c59 100644
--- a/Modules/Tools/Externals/Zend/Db/Adapter/Pdo/Abstract.php
+++ b/Modules/Tools/Externals/Zend/Db/Adapter/Pdo/Abstract.php
@@ -57,7 +57,7 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
*
* @return string
*/
- protected function _dsn()
+ protected function _dsn($write_mode = false)
{
// baseline of DSN parts
$dsn = $this->_config;
@@ -65,10 +65,15 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
// don't pass the username, password, charset, persistent and driver_options in the DSN
unset($dsn['username']);
unset($dsn['password']);
+ unset($dsn['username_write']);
+ unset($dsn['password_write']);
unset($dsn['options']);
unset($dsn['charset']);
unset($dsn['persistent']);
unset($dsn['driver_options']);
+
+ if ($write_mode) $dsn['host'] = $dsn['host_write'];
+ unset($dsn['host_write']);
// use all remaining parts in the DSN
foreach ($dsn as $key => $val) {
@@ -91,9 +96,6 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
return;
}
// get the dsn first, because some adapters alter the $_pdoType
$dsn = $this->_dsn();
+ if ($this->_config['host_write'])
+ $dsn_write = $this->_dsn(true);
// check for PDO extension
if (!extension_loaded('pdo')) {
/**
@@ -120,14 +122,28 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
$this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
}
try {
$this->_connection = new PDO(
- $dsn,
+ $dsn_read,
$this->_config['username'],
$this->_config['password'],
$this->_config['driver_options']
);
+ if ($this->_config['host_write']) {
+ $this->_connection_write = new PDO(
+ $dsn_write,
+ $this->_config['username_write'],
+ $this->_config['password_write'],
+ $this->_config['driver_options']
+ );
+ }
+
$this->_profiler->queryEnd($q);
// set the PDO connection to perform case-folding on array keys, or not
diff --git a/Modules/Tools/Externals/Zend/Db/Statement/Pdo.php b/Modules/Tools/Externals/Zend/Db/Statement/Pdo.php
index 8bd9f98..4ab81bf 100644
--- a/Modules/Tools/Externals/Zend/Db/Statement/Pdo.php
+++ b/Modules/Tools/Externals/Zend/Db/Statement/Pdo.php
@@ -61,8 +61,11 @@ class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggrega
*/
protected function _prepare($sql)
{
+
+ $read_only_connection = preg_match("/^select/i", $sql);
+
try {
- $this->_stmt = $this->_adapter->getConnection()->prepare($sql);
+ $this->_stmt = $this->_adapter->getConnection($read_only_connection)->prepare($sql);
} catch (PDOException $e) {
require_once 'Zend/Db/Statement/Exception.php';
throw new Zend_Db_Statement_Exception($e->getMessage());