php's mysqli::commit has a $name param - what does it do

Viewed 48
2 Answers

So I turned on query logging

and this is what was logged

passing name to commit():

Query   START TRANSACTION
Query   INSERT INTO `bob` (`t`) VALUES ("test 1")
Query   SAVEPOINT `foo`
Query   INSERT INTO `bob` (`t`) VALUES ("test 2")
Query   COMMIT /*foo*/

both queries were logged

passing name to rollback:

Query   START TRANSACTION
Query   INSERT INTO `bob` (`t`) VALUES ("test 1")
Query   SAVEPOINT `foo`
Query   INSERT INTO `bob` (`t`) VALUES ("test 2")
Query   ROLLBACK /*foo*/
Query   COMMIT /*comment*/

neither insert happened... entire transaction was rolled back

Nutshell:

mysqli::commit and mysqli::rollback have $name parameters that don't do anything beside add a comment to the query.

To actually rollback to a savepoint, you'll have to execute a query (ROLLBACK TO `name`).

mysqli extension provides a savepoint($name) method, but no rollback_to_savepoint!

When you start a transaction, you have the option of creating a savepoint name for the transaction. You can then use that name to commit. From the MySQL docs:

The SAVEPOINT statement sets a named transaction savepoint with a name of identifier. If the current transaction has a savepoint with the same name, the old savepoint is deleted and a new one is set.

Related