Transactions in MySQL - Unable to Roll Back

删除回忆录丶 提交于 2020-01-14 09:01:37

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5332374/transactions-in-mysql-unable-to-roll-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!