考虑下面的代码,如果捕获到异常,它不会回滚事务。
transaction = connection.BeginTransaction();
command.Transaction = transaction;
try {
// interact with database here
catch {}
finally {
connection.Close();
}
这样做的后果是什么?是否有必要回滚事务?
发布于 2011-03-08 09:00:42
它将在数据库上留下一个打开的事务,这可能会阻止其他查询。
摘自here
在使用事务时,请考虑以下一般指导原则,以避免导致死锁:
发布于 2011-03-08 09:01:30
最好的方法是在using块中生成事务,如下所示:
using( /*code to create the transaction you want )
{
//perform your transaction here
transaction.Commit();
}
如果在调用commit之前代码失败,那么当using块退出时,代码将自动回滚。
https://stackoverflow.com/questions/5230295
复制相似问题