我们有两个直接相关的问题,可以简化为一段非常简单的代码。假设机器 1 托管应用程序,机器 2 托管数据库,通过集线器通过以太网连接。我们将通过从集线器上拔下网络电缆来模拟网络问题。
您可能会说“让您的网络可靠”。我也是。在客户相信之前,我需要通过明确地捕捉问题来证明这不是首先。
我们无法通过超时解决这个问题,因为我们有一些/非常/长时间运行的查询和非查询。是的,其中一些确实需要一个小时或更长时间,并且用户不会忍受盯着冻结的会话足够长的时间来得到真正的错误。他们反而杀了它。
using System.Data;
using System.Data.SqlClient;
public class test {
public static void hang1()
{
using SqlConnection oConnection = applib.getConnection() // returns an open connectin
{
using SqlCommand oCmd = new SqlCommand("WAITFOR DELAY 00:01:00", oConnection)
oCmd.ExecuteNonQuery(); // unplug cable between hub and database server when in this call and this call never returns
}
}
public static void hang2()
{
using SqlCommand oTCmd = new SqlCommand("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE", oConnection)
oCmd.ExecuteNonQuery();
using oTransaction = new SqlClient.SqlTransaction
{
using SqlCommand oCmd = new SqlCommand("SELECT max(id) FORM Table1")
{
oCmd.Transaction = oTransaction;
oCmd.ExecuteScaler();
System.Threading.Thread.Sleep(60 * 1000);
// Disconnect the cable between the hub and the application server here
// Now table Table1 is locked and will remain locked until the transaction
// is manually killed from the database server.
oCmd.ExecuteScaler();
}
}
}
}
我们正在寻找一种解决方案来检测事务是否被卡住,而无需对其设置超时。如果我正在设计我自己的 TCP/IP 协议,我会在其中设计一个心跳,以便在足够多的秒数内没有响应 = 断定,退出,清理。我想要一种实现相同想法的方法,即将安静的挂起变成嘈杂的崩溃,以便清理代码可以清理它。