diff --git a/include/class.sequence.php b/include/class.sequence.php
index 1b3fc3182ec1e890d8517d3c2969818d862fb40a..cc27801c596a8dd17acac088286e15bc7de6b4aa 100644
--- a/include/class.sequence.php
+++ b/include/class.sequence.php
@@ -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;
     }
 
diff --git a/include/mysqli.php b/include/mysqli.php
index 71681975cd8bdd8e1281a3290eac9234690fa6af..ed70cd82ef3c5fc8ae0f209759ed9c969f2ceb61 100644
--- a/include/mysqli.php
+++ b/include/mysqli.php
@@ -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();