Meg*_*ind 6 c# transactions transactionscope
当我在数据库中保存数据时,我使用TransactionScope并将IsolationLevel设置为Serializable.
TransactionOptions options = new TransactionOptions
{
IsolationLevel=IsolationLevel.Serializable
};
using (var transaction = new TransactionScope(TransactionScopeOption.Required,options))
{
transation.Complete();
}
Run Code Online (Sandbox Code Playgroud)
现在执行结束后,我想更改TransactionScopeIsolationLevel.
编辑
我理解的是,如果IsolationLevel设置为Serializable,那么在完成事务之后,连接对象将关闭并返回到连接池,当其他一些请求到达时,它从池中获取该连接对象,从而受到前一个IsolationLevel的影响.所以我想在每次交易后将隔离级别更改为默认值.
你是对的:返回到池的连接时不会重置隔离级别.这是可怕的行为,但我们坚持下去......
有两种策略:
我建议你做后者.
如果你坚持做(1),你只需在关闭后更改隔离级别,TransactionScope但必须使用连接对象.例:
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (var transaction = new TransactionScope(TransactionScopeOption.Required,options))
{
connection.Open(); //open inside of scope so that the conn enlists itself
transation.Complete();
}
//conn is still open but without transaction
conn.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL XXX"); //pseudo-code
} //return to pool
Run Code Online (Sandbox Code Playgroud)
这对你有用吗?