locking - What is the result of a mysql SELECT query on a table already LOCKED (write) by another session? -
everything in title...
if use:
lock table tablea write commands unlock tables
what happen if session/user tries make select query on table? can't find in mysql documentation...
will mysql respond error kind of "error xxx table locked! retry later" or mysql automatically wait until table unlocked? how long? mysql respond timeout error if table stay locked long time?
btw wonder if case happen:
lock table tablea write
and php/mysql crashes
and tablea stay locked 'forever' until mysql receive unlock tables command.
just tested php :
first point :
let's use script that:
- locks table
- invoke script try select on same table (done in same file switch on $argc)
foo.php:
function showtablelocked( $link, $table ) { $query = mysqli_query( $link, "show open tables `table` '$table'" ); $data = mysqli_fetch_assoc( $query ); echo "is table locked ? :". (($data['in_use'] == '1' )? 'yes' : 'no' )."\n"; } // show table status showtablelocked( $link, $table ); if ( $argc == 1 ) { // lock table mysqli_query( $link, "lock table $table write" ); // check lock showtablelocked( $link, $table ); echo "call concurrent script\n"; echo "----------------\n"; passthru( 'php '.__file__.' --no-recursion' ); echo -"---------------\n"; } else { echo "let's try select something\n"; // select $query = mysqli_query( $link, "select count(*) foo $table" ); $data = mysqli_fetch_assoc( $query ); echo "my select result: ". $data['foo']."\n"; } // table still locked ? showtablelocked( $link, $table ); // eof
execution outputs :
$ php foo.php table locked: no table locked: yes call concurrent script ---------------- table locked: yes let's try select
and have stop script since second call waiting lock removed. select not possible.
for second point :
foo.php
// include showtablelocked func def , init vars... // // show table status showtablelocked( $link, $table ); // lock table mysqli_query( $link, "lock table $table write" ); // check lock showtablelocked( $link, $table ); // cal undefined function make php crash generatesommefatalerror();
notice did not unlock table nor closed database connection.
first execution gives :
$ php foo.php table locked: no table locked: yes php fatal error: call undefined function generatesommefatalerror() in foo.php on line xxx
second execution gives same output, especialy first test :
$ php foo.php table locked: no
so looks table automaticaly unlocked, guess done when mysqli connection automaticaly ended php.
Comments
Post a Comment