I was recently told that I should use transactions in my application as they can help run code quicker and prevent MySQL code from executing before a whole page has loaded (e.g. if someone is just refreshing on my page and the whole page doesn’t load, no MySQL calls will start and it reduces load on the server.)
I’m wondering if I wanted to switch my site to transactions, how difficult would this be, and what changes would I have to make?
Also, I’m using PDO as opposed to mysql_*, if that would make any difference?
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 ormysqli_*). 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 :
Using PDO,
mysql_*, ormysqli_*shouldn’t make much of a difference, here — even if it’s recommended not to usemysql_*(old, and doesn’t support some features of MySQL >= 4.1, such as prepared statements).