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

Time:05-20

https://www.php.net/manual/en/mysqli.commit.php

public mysqli::commit(int $flags = 0, ?string $name = null): bool

Parameters
flags: A bitmask of MYSQLI_TRANS_COR_* constants.
name: If provided then COMMIT/*name*/ is executed.

My question: what is COMMIT/*name*/ ??

I can't find any Mysql documentation for this

https://dev.mysql.com/doc/refman/8.0/en/commit.html
https://dev.mysql.com/doc/refman/8.0/en/innodb-autocommit-commit-rollback.html

nor any usage in the wild

CodePudding user response:

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.

CodePudding user response:

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.

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

  • Related