Conditions That Can Make a MySQL COMMIT Query Fail

MySQLmysql-5.7transaction

I have an application where I run an explicit COMMIT query on a MySQL standalone server

COMMIT;

The query works as expected in the vast majority of cases, but sometimes the query seems successful but at the same time seems not to commit the transaction to the DB.

So I was digging into the possibility that a COMMIT query could fail. I found this similar question: https://stackoverflow.com/questions/3960189/can-a-commit-statement-in-sql-ever-fail-how

but on the official MySQL doc never mentions that a COMMIT can fail.

I want to understand in which conditions a commit query can fail and how to reproduce it, possibly supported by official doc pages.

Best Answer

This depends entirely on how you setup the transaction.

If you have START TRANSACTION and COMMIT; surrounding a series of INSERT, UPDATE, and DELETE queries, there are some commands that trigger an implicit commit:

  • ALTER TABLE
  • CREATE INDEX
  • DROP DATABASE
  • DROP INDEX
  • DROP TABLE
  • RENAME TABLE
  • TRUNCATE TABLE
  • LOCK TABLES
  • UNLOCK TABLES
  • SET AUTOCOMMIT = 1
  • BEGIN (can break another BEGIN or START TRANSACTION)
  • START TRANSACTION (can break another START TRANSACTION or BEGIN)

I have mentioned this before in the DBA StackExchange

You can see the latest list of commands that trigger implicit commits in the MySQL 5.7 Docs. Why consider implicit commits ? Data may commit out of context with other data too soon or too late.

Aside from these commands, terminating a DB Connection will implicitly rollback a transaction. You should monitor the status variables Aborted_connects and Aborted_clients to see if DB Connections are failing on entry or in session.