I am new to transactions, but they're causing me a lot of grief right now...
Basically I do this:
sql_query('agis_core_master', 'SET autocommit=0');
sql_query('agis_core_master', 'START TRANSACTION');
$query_result .= sql_query('agis_core_master', "UPDATE registration
SET registration_name = ? WHERE id_aircraft = ? LIMIT 1",
$query_result .= sql_query('agis_core_master', "UPDATE sdu_tray SET
sdu_tray_id = ? WHERE id_aircraft = ? LIMIT 1", array($data['sdu_tray_id'],
if ($query_result == '11')
//weird that I don't need to do this, since I had to set it manually
//sql_query('agis_core_master', 'SET autocommit=1');
But it seems that sometimes these UPDATEs take a long time (longer than they
should) and so they just accumulate locks in mysql and I can't figure out
how to clear them all. If I refresh my page, it exacerbates the situation
with now even more locks.
I'm confused especially because the documentation is not only sparce in
examples, but also seems to contradict itself.
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
"Beginning a transaction causes any pending transaction to be committed. ...
Beginning a transaction also causes table locks acquired with LOCK TABLES to
be released, as though you had executed UNLOCK TABLES. "
I don't understand why I have to manually 'SET autocommit=0' and then it's
even more confusing why I don't have to 'SET autocommit=1' (or do I? -- I've
tried it both ways and it doesn't seem to help)
Furthermore this page gives a completely different example/usage:
"The correct way to use LOCK TABLES and UNLOCK TABLES with transactional
tables, such as InnoDB tables, is to begin a transaction with SET autocommit
= 0 (not START TRANSACTION) followed by LOCK TABLES, and to not call UNLOCK
TABLES until you commit the transaction explicitly. For example, if you need
to write to table t1 and read from table t2, you can do this:"
LOCK TABLES t1 WRITE, t2 READ, ...;
... do something with tables t1 and t2 here ...
So WTF? How am I supposed to really do this transaction stuff, and HOW do I
force the clearing of locks or stuck queries or whatever should something
I've logged in as root and typed all sorts of commands to no avail:
FLUSH TABLES WITH READ LOCK;
FLUSH TABLES WITH WRITE LOCK;
But even if there is some magic incantation that root can use to "fix" this,
it seems pretty fragile that ANY QUERY I can make should cause my RDBMS to
be hosed like this. Why isn't InnoDB/MySQL more resilient and bounce back,
clearing locks, etc.
"/etc/init.d/mysql restart" will "fix" the problem, but we have nearly a
billion rows, so bringing the database down and up is painfully slow as it
does it's auto-checking and "fixing" of rows and whatever other magic it
does behind the scenes. Not an option.
Nor do we want/need to be sitting there restarting servers or issuing CLI
commands to fix the database should it be hosed by some failed transaction.
Isn't that the point of transactions?
We're running Ubuntu LTS 8.04 and mysql 5.0.51a (with all patches, etc.)