Should I commit or rollback a read transaction?

后端 未结 12 1192
臣服心动
臣服心动 2020-12-02 10:42

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?

  • Commit the tr
12条回答
  •  生来不讨喜
    2020-12-02 11:23

    Just a side note, but you can also write that code like this:

    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())
        {
            // Do something useful
        }
        // To commit, or not to commit?
    }
    

    And if you re-structure things just a little bit you might be able to move the using block for the IDataReader up to the top as well.

提交回复
热议问题