我有一个读取查询,我在事务内执行它,以便我可以指定隔离级别。一旦查询完成,我应该怎么做?
- 提交事务
- 回滚事务
- 什么也不做(这将导致在使用块结束时回滚事务)
每种做法的影响是什么?
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?
}
}
编辑:问题不是是否应该使用事务或是否有其他设置事务级别的方式。问题是一个不修改任何内容的事务提交或回滚是否有任何区别。它是否会造成性能差异?它是否影响其他连接?是否存在其他差异?