[Solar-svn] Revision 3303
pmjones at solarphp.com
pmjones at solarphp.com
Thu Jul 31 18:05:37 CDT 2008
Solar_Sql_Adapter_MysqlReplicated: [FIX] If you are in a transaction, use the master for SELECTs. This makes sure that inserts and updates to the master are reflected by SELECTs within the transaction.
Modified: trunk/Solar/Sql/Adapter/MysqlReplicated.php
===================================================================
--- trunk/Solar/Sql/Adapter/MysqlReplicated.php 2008-07-31 22:52:20 UTC (rev 3302)
+++ trunk/Solar/Sql/Adapter/MysqlReplicated.php 2008-07-31 23:05:37 UTC (rev 3303)
@@ -129,6 +129,17 @@
/**
*
+ * Whether or not we're in a transaction.
+ *
+ * When true, all SELECTs go to the master.
+ *
+ * @param bool
+ *
+ */
+ protected $_in_transaction = false;
+
+ /**
+ *
* Follow-on setup for the constructor to build the $_slaves array.
*
* @return void
@@ -350,8 +361,8 @@
/**
*
* Prepares an SQL query as a PDOStatement object, using the slave PDO
- * connection for all 'SELECT' queries, and the master PDO connection for
- * all other queries.
+ * connection for all SELECT queries outside a transation, and the master
+ * PDO connection for all other queries (incl. in-transaction SELECTs).
*
* @param string $stmt The text of the SQL statement, optionally with
* named placeholders.
@@ -367,7 +378,7 @@
// prepare the statment
try {
- if ($is_select) {
+ if ($is_select && ! $this->_in_transaction) {
// slave
$config = $this->_slaves[$this->_slave_key];
$this->connect();
@@ -410,6 +421,7 @@
$this->connectMaster();
$time = microtime(true);
$result = $this->_pdo_master->beginTransaction();
+ $this->_in_transaction = true;
$this->_addProfile($time, '__BEGIN_MASTER');
return $result;
}
@@ -426,6 +438,7 @@
$this->connectMaster();
$time = microtime(true);
$result = $this->_pdo_master->commit();
+ $this->_in_transaction = false;
$this->_addProfile($time, '__COMMIT_MASTER');
return $result;
}
@@ -442,6 +455,7 @@
$this->connectMaster();
$time = microtime(true);
$result = $this->_pdo_master->rollBack();
+ $this->_in_transaction = false;
$this->_addProfile($time, '__ROLLBACK_MASTER');
return $result;
}
More information about the Solar-svn
mailing list