Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transactions in MySQL - Unable to Roll Back

I'm using MySQL 5.0.27 and am trying to get transactions to work. I followed this tutorial:

http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqltransaction.html

and still cannot get these to work. The table I am trying to update is InnoDB and have tried to execute 'set autocommit=0' but it doesn't seem to be doing anything.... The code I've written is the following:

public int transactionUpdate()
{
    MySqlConnection connection = new MySqlConnection(connStr);
    connection.Open();
    MySqlCommand command = connection.CreateCommand();
    MySqlTransaction trans;
    trans = connection.BeginTransaction();
    command.Connection = connection;
    command.Transaction = trans;
    try
    {
        command.CommandText = "SET autocommit = 0";
        command.executeNonQuery();
        command.CommandText = "UPDATE TBL.rec_lang rl SET rl.lang_code = 'en-us' WHERE rl.recording=123456";
        command.executeNonQuery();
        command.CommandText = "UPDATE TBL.rec_lang rl SET rl.lang_code = en-us WHERE rl.recording=123456";      
        command.executeNonQuery();
        trans.Commit();
    }
    catch(Exception ex)
    {
        try
        {
            trans.Rollback();
        }
        catch(MySqlException mse)
        {
            log.error(mse);
        }
    }
}

The second command fails as it is missing the ' around 'en-us'. This should roll back the first query as well to a previous value but it isn't. Can you tell me what I'm doing wrong???

MySQLConnector v. 6.3.6.0

MySQL v. 5.0.27

C# VS2010

like image 491
Austin Avatar asked Mar 16 '11 21:03

Austin


People also ask

Why rollback is not working in MySQL?

and make sure that you are not using COMMIT after the Query which you need to rollback. Refer Table Engines and Transaction. And When a DB connection is created, it is in auto-commit mode by default.

Which statements Cannot be rolled back?

Some statements cannot be rolled back. In general, these include data definition language (DDL) statements, such as those that create or drop databases, those that create, drop, or alter tables or stored routines.

How do I rollback a MySQL transaction?

To roll back the current transaction and cancel its changes, you use the ROLLBACK statement. To disable or enable the auto-commit mode for the current transaction, you use the SET autocommit statement.

What happens if transaction rollback fails?

If a rollback fails, then you would have a serious problem. The reliability of the database cannot be guaranteed. In other words; you probably have some sort of corruption in your transaction log and will end up with an inconsistent database.


1 Answers

I had a second database open that had bad data showing ><... this method works. Turns out I didn't even need:

command.CommandText = "SET autocommit = 0";  
command.executeNonQuery();

So this code does work for transactions.

like image 155
Austin Avatar answered Oct 11 '22 10:10

Austin