Skip to content
Snippets Groups Projects
Commit d1581026 authored by Jared Hancock's avatar Jared Hancock
Browse files

Rollback to autocommit, except for sequence iteration

Keeping the transaction active for the entire request increases the
likelihood of a deadlock on Galera clusters.
parent bfffc144
No related branches found
No related tags found
No related merge requests found
......@@ -151,6 +151,9 @@ class Sequence extends VerySimpleModel {
* and assured to be session-wise atomic before the value is returned.
*/
function __next($digits=false) {
// Ensure this block is executed in a single transaction
db_autocommit(false);
// Lock the database object -- this is important to handle concurrent
// requests for new numbers
static::objects()->filter(array('id'=>$this->id))->lock()->one();
......@@ -161,6 +164,8 @@ class Sequence extends VerySimpleModel {
$this->updated = SqlFunction::NOW();
$this->save();
db_autocommit(true);
return $next;
}
......
......@@ -76,18 +76,7 @@ function db_connect($host, $user, $passwd, $options = array()) {
@db_set_variable('sql_mode', '');
// Start a new transaction -- for performance. Transactions are always
// committed at shutdown (below)
$__db->autocommit(false);
// Auto commit the transaction at shutdown and re-enable statement-level
// autocommit
register_shutdown_function(function() {
global $__db, $err;
if (!$__db->commit())
$err = 'Unable to save changes to database';
$__db->autocommit(true);
});
$__db->autocommit(true);
// Use connection timing to seed the random number generator
Misc::__rand_seed((microtime(true) - $start) * 1000000);
......@@ -95,6 +84,12 @@ function db_connect($host, $user, $passwd, $options = array()) {
return $__db;
}
function db_autocommit($enable=true) {
global $__db;
return $__db->autocommit($enable);
}
function db_close() {
global $__db;
return @$__db->close();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment