Reputation: 2020
I'm using a queue to queue transactions for a MySQL database in python. I'm using autocommit=False so that I can rollback a transaction if not all of the data queries are executed properly.
I'm wondering whether it's possible to check whether a worker has performed an action on the database before performing db.rollback()? Or can you perform db.rollback even if a worker hasn't done anything to the database without any errors occurring?
Thanks!
Upvotes: 0
Views: 38
Reputation: 951
If you issue a
START TRANSACTION;
(which brings the connection into the same as autocommit = false
), sending the command
ROLLBACK WORK;
(see also https://dev.mysql.com/doc/refman/5.5/en/commit.html ) returns with
0 row(s) affected
So, this means, you may rollback an empty transaction safely (tested on MySQL 5.1.49-3).
Alternatively, you always may store in a worker a local/static variable, which is initialized with false
. If at least a single update command has been issued by the worker, you set this variable to true
.
If you have to decide later on whether to ROLLBACK
or not, you just look at the variable: if it true
, you know that you have sent a least one update command through the DB channel.
Upvotes: 0
Reputation: 5246
If I've understood the question correctly, it is not an error to rollback a transaction where nothing else has happened, although it is an error to attempt to rollback when no transaction exists. To tell which is the case you can test the MySQLConnection.in_transaction
property.
Upvotes: 2