cvs: php4 /pear/DB mysql.php
| From: | Stig Bakken | Date: | Mon, 23 Jul 2001 22:21:22 +0000 |
| Subject: | cvs: php4 /pear/DB mysql.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-339@lists.php.net to get a copy of this message | ||
ssb Mon Jul 23 18:21:22 2001 EDT
Modified files:
/php4/pear/DB mysql.php
Log:
* fix race conditions when dealing with new sequence emulation (form Oleg)
Index: php4/pear/DB/mysql.php
diff -u php4/pear/DB/mysql.php:1.65 php4/pear/DB/mysql.php:1.66
--- php4/pear/DB/mysql.php:1.65 Sun Jul 22 10:18:42 2001
+++ php4/pear/DB/mysql.php Mon Jul 23 18:21:22 2001
@@ -383,13 +383,32 @@
$result->getCode() == DB_ERROR_NOSUCHTABLE) {
$repeat = 1;
$result = $this->createSequence($seq_name);
+ // Since createSequence initializes the ID to be 1,
+ // we do not need to retrieve the ID again (or we will get 2)
if (DB::isError($result)) {
return $result;
+ } else {
+ // First ID of a newly created sequence is 1
+ return 1;
}
} elseif (DB::isError($result) &&
$result->getCode() == DB_ERROR_ALREADY_EXISTS) {
// Must be using old sequence emulation implementation,
- // clean up the dupes
+ // we need to clean up the dupes.
+
+ // Obtain a user-level lock... this will release any previous
+ // application locks, but unlike LOCK TABLES, it does not abort
+ // the current transaction and is much less frequently used.
+ $result = $this->getOne("SELECT
GET_LOCK('${sqn}_seq_lock',10)");
+ if (DB::isError($result)) {
+ return $result;
+ }
+ if ($result == 0) {
+ // Failed to get the lock, can't do the conversion, bail
+ // with a DB_ERROR_NOT_LOCKED error
+ return $this->mysqlRaiseError(1100);
+ }
+
$highest_id = $this->getOne("SELECT MAX(id) FROM ${sqn}_seq");
if (DB::isError($highest_id)) {
return $highest_id;
@@ -401,6 +420,15 @@
if (DB::isError($result)) {
return $result;
}
+
+ // If another thread has been waiting for this lock,
+ // it will go thru the above procedure, but will have no
+ // real effect
+ $result = $this->getOne("SELECT
RELEASE_LOCK('${sqn}_seq_lock')");
+ if (DB::isError($result)) {
+ return $result;
+ }
+
// This should kill all rows except the highest, now we
// can try again
$repeat = 1;
@@ -426,7 +454,7 @@
if (DB::isError($res)) {
return $res;
}
- // nextId call will generate ID 1
+ // insert yields value 1, nextId call will generate ID 2
return $this->query("INSERT INTO ${sqn}_seq VALUES(0)");
}