我有一个读取查询,在事务中执行,以便我可以指定隔离级别。一旦查询完成,我该怎么办?
- Commit the transaction
- Rollback the transaction
- Do nothing (which will cause the transaction to be rolled back at the end of the using block)
做每件事情的影响是什么?
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?
}
}
编辑:问题不在于是否应该使用事务或是否有其他设置事务级别的方法。问题在于,对于不修改任何内容的事务,提交或回滚是否有任何区别。是否存在性能差异?是否影响其他连接?是否存在其他差异?