我应该提交或回滚一个读事务? [英] Should I commit or rollback a read transaction?

查看:141
本文介绍了我应该提交或回滚一个读事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个读查询,我在一个事务中执行,这样我可以指定隔离级别。一旦查询完成后,我该怎么办?

I have a read query that I execute within a transaction so that I can specify the isolation level. Once the query is complete, what should I do?

  • 提交事务
  • 回滚事务
  • 请什么(这将导致事务在using块结束时回滚)

什么是做各的含义是什么?

What are the implications of doing each?

using (IDbConnection connection = ConnectionFactory.CreateConnection())
{
    using (IDbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted))
    {
        using (IDbCommand command = connection.CreateCommand())
        {
            command.Transaction = transaction;
            command.CommandText = "SELECT * FROM SomeTable";
            using (IDataReader reader = command.ExecuteReader())
            {
                // Read the results
            }
        }

        // To commit, or not to commit?
    }
}

编辑:现在的问题是不是如果一个事务应该被使用或者是否有其他的方法来设置事务级别。现在的问题是,如果它叫不修改任何一个事务被提交或回滚任何区别。是否有性能差异?这是否会影响其他连接?任何其他方面的差异?

The question is not if a transaction should be used or if there are other ways to set the transaction level. The question is if it makes any difference that a transaction that does not modify anything is committed or rolled back. Is there a performance difference? Does it affect other connections? Any other differences?

推荐答案

您提交。期。有没有其他的明智的选择。如果你开始交易,你应该关闭它。提交的版本,你可能有任何锁,并且同样是理智的对待READUNCOMMITTED或可序列化隔离级别。依托隐式回退 - 虽然可能在技术上等同 - 仅仅是可怜的形式

You commit. Period. There's no other sensible alternative. If you started a transaction, you should close it. Committing releases any locks you may have had, and is equally sensible with ReadUncommitted or Serializable isolation levels. Relying on implicit rollback - while perhaps technically equivalent - is just poor form.

如果已经不相信你,只是想象一下未来的家伙谁在插入你的code中的中间一个更新语句,并具有跟踪发生并删除他的数据,隐含回滚

If that hasn't convinced you, just imagine the next guy who inserts an update statement in the middle of your code, and has to track down the implicit rollback that occurs and removes his data.

这篇关于我应该提交或回滚一个读事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆