ADO .NET 事务范围即使在停止后也锁定事务
ADO .NET transaction scope locks transaction even after stopped
我已经在 Web API 应用程序中实现了事务范围。当我必须全部提交或全部回滚时,它工作正常。但有时在调试时,如果我在事务完成之前停止服务,那么事务就会被锁定。 table 仍显示为已锁定,即使在关闭 visual studio 后,该特定会话仍保留在数据库中。这种情况有时会发生,并且该特定事务不会回滚。我还为事务范围设置了超时。问题仍在发生。有没有办法在服务停止后立即中止 table 操作?
您需要使用 TransactionScope 吗?如果没有,您可以考虑使用与连接相关的 SqlTransaction。
If the client's network connection to an instance of the Database
Engine is broken, any outstanding transactions for the connection are
rolled back when the network notifies the instance of the break. If
the client application fails or if the client computer goes down or is
restarted, this also breaks the connection, and the instance of the
Database Engine rolls back any outstanding connections when the
network notifies it of the break. If the client logs off the
application, any outstanding transactions are rolled back.
using (SqlConnection cn = new SqlConnection("YOUR CONNECTION"))
{
cn.Open();
using (SqlTransaction tr = cn.BeginTransaction())
{
// your transactional code
tr.Commit();
}
}
我已经在 Web API 应用程序中实现了事务范围。当我必须全部提交或全部回滚时,它工作正常。但有时在调试时,如果我在事务完成之前停止服务,那么事务就会被锁定。 table 仍显示为已锁定,即使在关闭 visual studio 后,该特定会话仍保留在数据库中。这种情况有时会发生,并且该特定事务不会回滚。我还为事务范围设置了超时。问题仍在发生。有没有办法在服务停止后立即中止 table 操作?
您需要使用 TransactionScope 吗?如果没有,您可以考虑使用与连接相关的 SqlTransaction。
If the client's network connection to an instance of the Database Engine is broken, any outstanding transactions for the connection are rolled back when the network notifies the instance of the break. If the client application fails or if the client computer goes down or is restarted, this also breaks the connection, and the instance of the Database Engine rolls back any outstanding connections when the network notifies it of the break. If the client logs off the application, any outstanding transactions are rolled back.
using (SqlConnection cn = new SqlConnection("YOUR CONNECTION"))
{
cn.Open();
using (SqlTransaction tr = cn.BeginTransaction())
{
// your transactional code
tr.Commit();
}
}