I'm doing transactions in PHP with the mysqli class and I find the documentation of autocommit() woefully incomplete. I infer from the examples that setting autocommit to false implicitly performs a 'begin transaction'. Is this correct? More importantly, I don't see where autocommit gets reset to true which I assume it should be after a transaction is complete. Does this happen automatically when commit() or rollback() methods are called?
2 Answers
It does not implicitly perform a START TRANSACTION, because that performs ad-hoc transactions: http://dev.mysql.com/doc/refman/5.0/en/commit.html
With autocommit(false), any time you execute a write query you must COMMIT in order to have it update permanently on the disk. This is true for all queries in that connection from then on. With autocommit(true), if you use START TRANSACTION, run an update, run COMMIT, and then run another update, both updates will be applied immediately (i.e. no second COMMIT is needed).
autocommit never gets reset to true, but it is only set per-session (MySQL session, not PHP session).
Autocommit(FALSE) will stay false for the following queries. After you perform a commit-statement, the autocommit will revert back to true. Unsure of the rollback if it does the same. I've been using this way for a few years and it has always worked fine.
When you want consistency in your db, this is one way to go.