Index: includes/bootstrap.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v retrieving revision 1.165 diff -u -r1.165 bootstrap.inc --- includes/bootstrap.inc 8 May 2007 16:36:55 -0000 1.165 +++ includes/bootstrap.inc 17 May 2007 06:23:47 -0000 @@ -444,10 +444,10 @@ 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(); + db_query_replace( + array("DELETE FROM {variable} WHERE name = '%s'", $name), + array("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, serialize($value)) + ); cache_clear_all('variables', 'cache'); Index: includes/cache.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/cache.inc,v retrieving revision 1.11 diff -u -r1.11 cache.inc --- includes/cache.inc 12 May 2007 18:26:15 -0000 1.11 +++ includes/cache.inc 17 May 2007 06:23:47 -0000 @@ -104,12 +104,10 @@ $data = serialize($data); $serialized = 1; } - db_lock_table($table); - db_query("UPDATE {". $table ."} SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, time(), $expire, $headers, $serialized, $cid); - if (!db_affected_rows()) { - @db_query("INSERT INTO {". $table ."} (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data, time(), $expire, $headers, $serialized); - } - db_unlock_tables(); + db_query_replace( + array("DELETE FROM {%s} WHERE cid = '%s'", $table, $cid), + array("INSERT INTO {%s} (cid, data, created, expire, headers) VALUES ('%s', %b, %d, %d, '%s')", $table, $cid, $data, time(), $expire, $headers) + ); } /** Index: includes/database.mysql.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/database.mysql.inc,v retrieving revision 1.71 diff -u -r1.71 database.mysql.inc --- includes/database.mysql.inc 8 May 2007 16:36:55 -0000 1.71 +++ includes/database.mysql.inc 17 May 2007 06:23:47 -0000 @@ -248,13 +248,22 @@ * with table prefixes. For example, db_next_id('{node}_nid'); */ function db_next_id($name) { + // Prefix any curly-bracket "{table}" names $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'); - - return $id; + // Assume initially that the sequence already exists. Attempt to update it atomically. + // LAST_INSERT_ID lets us get its value later. IGNORE suppresses update failures. + db_query("UPDATE IGNORE {sequences} SET id=LAST_INSERT_ID(id + 1) WHERE name = '%s'", $name); + // Check whether that worked + if (!db_affected_rows()) { + // Updated failed, so the sequence doesn't exist yet. + // In either case, try to create a new sequence starting from zero + // IGNORE suppresses insert error if e.g. a concurrent process got there first. + db_query("INSERT IGNORE INTO {sequences} VALUES ('%s', %d)", $name, 0); + // Sequence now exists (unless some database error occurred to prevent it) + // Retry update, allowing for possible concurrent process. It should work this time. + db_query("UPDATE {sequences} SET id = LAST_INSERT_ID(id + 1) WHERE name = '%s'", $name); + } + return mysql_insert_id(); } /** @@ -435,7 +444,20 @@ } /** + * Perform a replace query. + * + * @param $delete + * An array, where the first value is a query which deletes one row, + * subsequent values are arguments. Same syntax as of db_query. + * @param + * An array, where the first value is a query which inserts the new row, + * subsequent values are arguments. Same syntax as of db_query. + */ +function db_query_replace($delete, $insert) { + $insert_query = array_shift($insert); + db_query(preg_replace('/^INSERT /', 'REPLACE ', $insert_query), $insert); +} + +/** * @} End of "ingroup database". */ - - Index: includes/database.mysqli.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/database.mysqli.inc,v retrieving revision 1.35 diff -u -r1.35 database.mysqli.inc --- includes/database.mysqli.inc 21 Apr 2007 18:08:41 -0000 1.35 +++ includes/database.mysqli.inc 17 May 2007 06:23:47 -0000 @@ -239,13 +239,22 @@ * with table prefixes. For example, db_next_id('{node}_nid'); */ function db_next_id($name) { + // Prefix any curly-bracket "{table}" names $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'); - - return $id; + // Assume initially that the sequence already exists. Attempt to update it atomically. + // LAST_INSERT_ID lets us get its value later. IGNORE suppresses update failures. + db_query("UPDATE IGNORE {sequences} SET id=LAST_INSERT_ID(id + 1) WHERE name = '%s'", $name); + // Check whether that worked + if (!db_affected_rows()) { + // Updated failed, so the sequence doesn't exist yet. + // In either case, try to create a new sequence starting from zero + // IGNORE suppresses insert error if e.g. a concurrent process got there first. + db_query("INSERT IGNORE INTO {sequences} VALUES ('%s', %d)", $name, 0); + // Sequence now exists (unless some database error occurred to prevent it) + // Retry update, allowing for possible concurrent process. It should work this time. + db_query("UPDATE {sequences} SET id = LAST_INSERT_ID(id + 1) WHERE name = '%s'", $name); + } + return mysqli_insert_id(); } /** @@ -426,6 +435,21 @@ } /** + * Perform a replace query. + * + * @param $delete + * An array, where the first value is a query which deletes one row, + * subsequent values are arguments. Same syntax as of db_query. + * @param + * An array, where the first value is a query which inserts the new row, + * subsequent values are arguments. Same syntax as of db_query. + */ +function db_query_replace($delete, $insert) { + $insert_query = array_shift($insert); + db_query(preg_replace('/^INSERT /', 'REPLACE ', $insert_query), $insert); +} + +/** * @} End of "ingroup database". */ Index: includes/database.pgsql.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/database.pgsql.inc,v retrieving revision 1.45 diff -u -r1.45 database.pgsql.inc --- includes/database.pgsql.inc 21 Apr 2007 18:08:41 -0000 1.45 +++ includes/database.pgsql.inc 17 May 2007 06:23:47 -0000 @@ -433,6 +433,26 @@ } /** + * Perform a replace query. + * + * @param $delete + * An array, where the first value is a query which deletes one row, + * subsequent values are arguments. Same syntax as of db_query. + * @param + * An array, where the first value is a query which inserts the new row, + * subsequent values are arguments. Same syntax as of db_query. + */ +function db_query_replace($delete, $insert) { + $delete_query = array_shift($delete); + db_query('BEGIN'); + db_query(preg_replace('/^DELETE /', 'SELECT ', $delete_query .' FOR UPDATE'), $delete); + db_query($delete_query, $delete); + $insert_query = array_shift($insert); + db_query($insert_query, $insert); + db_query('COMMIT'); +} + +/** * @} End of "ingroup database". */