Reputation: 285
Can I use this function instead of 'LOCK TABLES' query?
Example:
pdo::beginTransaction();
SELECT id AS last_id FROM t WHERE...
INSERT INTO t (id,...) VALUES (last_id+1,....)
pdo::commit();
Upvotes: 1
Views: 2925
Reputation: 88697
The answer to this is: it depends. The most important resource to help you understand exactly what it does, what it doesn't do and how it works is here.
For a kick off, it depends whether the underlying driver (MySQL, MSSQL, etc etc) supports transaction functionality at all. If the driver does not support transactions, pdo::beginTransaction();
will fail and return FALSE
, and all your queries will be executed immediately. This is not to say that a LOCK TABLES
query would fail - it depends whether the underlying database engine supports it.
In fact, in MySQL at least, pdo::beginTransaction()
follows the same rules as a START TRANSACTION
statement. It does not lock the table immediately, it simply ensures that that the queries that are part of the transaction follow the rules of ACID.
I won't go into full details of exactly which combinations will work and what they will do because they are covered at length in the documents I linked to, and there is no sense in me writing it all out again.
Upvotes: 3