locking tables between db connections in the same script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi, I've been experimenting with locking tables in oracle 8 using queries
like "lock table x in exclusive mode nowait".  In my stand-alone script,
using PEAR::DB objects, I am trying to lock a table for the duration of a
transaction, and using a second connection, attempt to take out the same
lock.

I would expect that a 2nd db connection would not be able to take out the
lock after the 1st one has acquired it, but it seems to work.  If I run the
same script twice (simultaneously), the 2nd script fails when it tries to
get the lock, as expected.

ie. I thought the following would output:

    got first lock
    can't get second lock

but instead i get:

    got first lock
    got second lock

and if run a second time while the first instance is sleeping:

    can't get first lock

Why does this behaviour occur?
Tristan.

<?
require_once ('DB.php');
$conn1 = DB::connect("oci8://***");
$conn2 = DB::connect("oci8://***");

$conn1->autoCommit(false);
$conn2->autoCommit(false);

// Lock the table in session 1
if (DB::isError($conn1->query("lock table x in exclusive mode nowait")))
{
    echo "can't get first lock\n";
    echo $conn1->error;
}
else
{
    echo "got first lock\n";
    // Attempt to take out lock in another session - this succeeds as well
    if (DB::isError($conn2->query("lock table x in exclusive mode nowait")))
    {
         echo "can't get second lock\n";
         echo $conn2->error;
    }
    else
         echo "got second lock\n";
}

// wait a bit so we can run the script again
sleep(10);
?>

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux