diff -urp includes/bootstrap.inc includes/bootstrap.inc
--- includes/bootstrap.inc	2007-01-15 05:52:02.000000000 -0600
+++ includes/bootstrap.inc	2007-05-24 01:07:56.000000000 -0500
@@ -384,11 +384,11 @@ function variable_get($name, $default) {
 function variable_set($name, $value) {
   global $conf;
 
-  db_lock_table('variable');
-  db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
-  db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, serialize($value));
-  db_unlock_tables();
-
+  $serialized_value = serialize($value);
+  db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $serialized_value, $name);
+  if (!db_affected_rows()) {
+    @db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $serialized_value);
+  }
   cache_clear_all('variables', 'cache');
 
   $conf[$name] = $value;
diff -urp includes/cache.inc includes/cache.inc
--- includes/cache.inc	2006-11-10 01:26:27.000000000 -0600
+++ includes/cache.inc	2007-05-24 01:04:47.000000000 -0500
@@ -91,12 +91,11 @@ function cache_get($key, $table = 'cache
  *   A string containing HTTP header information for cached pages.
  */
 function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL) {
-  db_lock_table($table);
-  db_query("UPDATE {%s} SET data = %b, created = %d, expire = %d, headers = '%s' WHERE cid = '%s'", $table, $data, time(), $expire, $headers, $cid);
+  $created = time();
+  db_query("UPDATE {%s} SET data = %b, created = %d, expire = %d, headers = '%s' WHERE cid = '%s'", $table, $data, $created, $expire, $headers, $cid);
   if (!db_affected_rows()) {
-    @db_query("INSERT INTO {%s} (cid, data, created, expire, headers) VALUES ('%s', %b, %d, %d, '%s')", $table, $cid, $data, time(), $expire, $headers);
+    @db_query("INSERT INTO {%s} (cid, data, created, expire, headers) VALUES ('%s', %b, %d, %d, '%s')", $table, $cid, $data, $created, $expire, $headers);
   }
-  db_unlock_tables();
 }
 
 /**
diff -urp includes/database.mysql.inc includes/database.mysql.inc
--- includes/database.mysql.inc	2007-01-21 20:20:50.000000000 -0600
+++ includes/database.mysql.inc	2007-05-24 13:14:34.000000000 -0500
@@ -259,13 +259,11 @@ function db_error() {
  * with table prefixes. For example, db_next_id('{node}_nid');
  */
 function db_next_id($name) {
-  $name = db_prefix_tables($name);
-  db_query('LOCK TABLES {sequences} WRITE');
-  $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
-  db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id);
-  db_query('UNLOCK TABLES');
+  global $active_db;
+  $name = db_prefix_tables($name); 
+  db_query('INSERT INTO {sequences} VALUES ("%s", LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name);
 
-  return $id;
+  return mysql_insert_id($active_db);
 }
 
 /**
diff -urp includes/database.mysqli.inc includes/database.mysqli.inc
--- includes/database.mysqli.inc	2006-12-27 16:50:09.000000000 -0600
+++ includes/database.mysqli.inc	2007-05-24 13:14:06.000000000 -0500
@@ -239,13 +239,11 @@ function db_error() {
  * with table prefixes. For example, db_next_id('{node}_nid');
  */
 function db_next_id($name) {
-  $name = db_prefix_tables($name);
-  db_query('LOCK TABLES {sequences} WRITE');
-  $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
-  db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id);
-  db_query('UNLOCK TABLES');
+  global $active_db;
+  $name = db_prefix_tables($name); 
+  db_query('INSERT INTO {sequences} VALUES ("%s", LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name);
 
-  return $id;
+  return mysqli_insert_id($active_db);
 }
 
 /**
