qsoft
qsoft

Reputation: 454

MySQL rollback on transaction with lost/disconnected connection

I need to make MySQL server to rollback transaction immediately after its client disconnected, because each client works concurrently. The problem can be reproduced like these (using an innodb table type)

On Client A:

START TRANSACTION;
SELECT MAX(ID) FROM tblone FOR UPDATE;
#... then disconnect your connection to the server

On Client B:

START TRANSACTION;
SELECT MAX(ID) FROM tblone FOR UPDATE;
#... lock wait time out will occur here

I had set MySQL's server option like innodb_rollback_on_timeout and using mysql's client mysql --skip-reconnect on both client. I tried this using one server and two client, on a network. I disconnected the network physically (unplug the cable) after SELECT ... FOR UPDATE; line. I need to make other clients to be able to use tblone on a transaction (lock it, update it) immediately, and for that to happen I think the server should rollback the transaction for Client A, after Client A disconnects.

Upvotes: 24

Views: 33047

Answers (2)

Rick James
Rick James

Reputation: 142560

This is to discuss some of the Comments. Please note that this disagrees with some of the Comments. I'll use an INSERT instead of SELECT..FOR UPDATE because the effect is more visible.

Let's look at some different cases:

(1) No SQL + timeout

START TRANSACTION;
do some SQL statement(s)
do no SQL for more than the timeout (before COMMITing)

Avoid this because of the cases detailed below. Solution: Don't depend on InnoDB to help you with long transactions.

(2) long-running query

START TRANSACTION;
do some SQL statement(s)
run an SQL query that takes more than the timeout
COMMIT;

All is well. The timeout does not apply as long as the server (mysqld) is continuing to perform queries. That is, the timeout 'clock' starts over at the end of each SQL statement

(3) (auto-reconnect)

START TRANSACTION;
INSERT ... VALUES (123);
    time passes; no SQL performed for longer than the timeout
    disconnect occurs
INSERT ... VALUES (456);
    auto-reconnect (because you have it ENabled);
    the INSERT proceeds
COMMIT;

123 will be rolled back; 456 will be inserted. (Similarly SELECT..FOR UPDATE would lose the locks.) Not good. The solution is to turn off "auto-reconnect". Instead, check for errors and treat the disconnect error as a fatal error for the Transaction. (Then start over the transaction.)

The INSERT 456 will be running in a new transaction, controlled by autocommit.

(4) (NO auto-reconnect)

START TRANSACTION;
INSERT ... VALUES (123);
    time passes; no SQL for longer than the timeout
    disconnect occurs
INSERT ... VALUES (456);
    NO auto-reconnect (because you have it DISabled)
COMMIT;

123 will be rolled back. The INSERT of 456 will get an error something like "connection lost". Start the transaction over.

Upvotes: 3

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40061

When you are physically disconnecting a client you're not sending a normal disconnect (which would have caused a rollback) and the MySQL protocol isn't very chatty so the server never knows that the client isn't there. I think this is a flaw in the protocol when comparing to other database systems where the client and server talks internally much more.

Anyway. There are two variables that you could change. They basically do the same but for different clients.

The first is wait_timeout and it is used by application clients like java or php.

The other is interactive_timeout and it is used by the mysql client (as in your tests)

In both cases the server to kills the connection after a number of seconds and when doing so rollbacks all transactions and releases all locks.

Upvotes: 25

Related Questions