mysql交易,MySQL的交易 - 无法回滚

博主在使用MySQL5.0.27时遇到事务处理的困扰,尝试按照教程设置事务但未成功。问题出现在更新语句中,由于缺少单引号导致第二个更新操作失败,未能触发事务回滚。最终发现不需要设置`SET autocommit=0`也能实现事务。代码在处理异常并回滚事务方面存在缺陷。
摘要由CSDN通过智能技术生成

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

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

解决方案

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

command.CommandText = "SET autocommit = 0";

command.executeNonQuery();

So the good news is this code does work for transactions... bad news i didn't figure it out until i posted ;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值