Transactions, in web applications, are generally used for Atomicity and for Isolation.
They don't "prevent MySQL code from executing before a whole page has loaded" : MySQL queries are still executed when you call mysql_query
(or any equivalent using PDO or mysqli_*
). But they will all be "cancelled" if you do not commit the transaction.
They also don't have that much to do with helping "run code quicker" ; even if they can sometimes have that effect, it's only because if you don't explicitly use a transaction for a group of queries (resulting in only one transaction), MySQL will implicitly use one transaction for each query (resulting in several transactions, which might put some additionnal load on the DB server)
Notes :
- of course, this is using a DB engine that supports transactions -- like InnoDB.
- for the "quicker" point, I've seen it a couple of times ; but it was in special situations, where I needed speed more than ACID -- or maybe it was just a matter of load on the server (which means if you want to go down that path, you have to profile, to know what's faster in your specific case ! )
Using PDO, mysql_*
, or mysqli_*
shouldn't make much of a difference, here -- even if it's recommended not to use mysql_*
(old, and doesn't support some features of MySQL >= 4.1, such as prepared statements).