C#SqlClient连接在线程中打开 - 没有捕获到SqlException

时间:2014-08-01 07:20:24

标签: c# sql-server multithreading sqlconnection sqlexception

我已经使用timeout参数实现了SqlClient连接功能。这意味着,connection.open()在另一个线程中,并且在线程启动后,我正在检查已用时间。如果时间已达到超时,则中止线程并且不建立连接。

问题是,如果我的超时时间超过默认connection.open()超时,open()会抛出SqlException,而Catch(SqlException)块中没有捕获。

我正在另一个线程中开始整个连接过程:

public void connect()
{
    Thread connectThread = new Thread(waitForTimeout);
    connectThread.Start();
}

连接线程 - >启动另一个超时等待的线程

public void waitForTimeout()
{
    connection = new SqlConnection(connectString);

    Thread timeoutThread = new Thread(openConnection);
    timeoutThread.Start();

    DateTime quitTime = DateTime.Now.AddMilliseconds(timeout);
    while (DateTime.Now < quitTime)
    {
        if (connection.State == ConnectionState.Open)
        {
            transac = connection.BeginTransaction();
            command = connection.CreateCommand();
            command.Transaction = transac;
            break;
        }
    }

    if (connection.State != ConnectionState.Open)
        timeoutThread.Interrupt();
}

此处在open()默认超时后未捕获异常:

private void openConnection()
{
    try
    {
        connection.Open();
    }
    catch(SqlException ex)
    {
        // This isn't caught anytime
    }
}

感谢您的任何想法!

2 个答案:

答案 0 :(得分:0)

是不是被抓住了还是不被抛出? Thread.start只调度线程运行,但并不意味着它会立即启动。也许代码运行直到threadTimeout中断,然后openConnection启动并始终成功在默认超时内打开连接。

---修改

在这种情况下,您可以尝试:

  1. SqlException替换为Exception并检查是否有类似ThreadInterruptedException的内容

  2. connection = new SqlConnection(connectString);之后立即将openConnection方法的内容放入waitForTimeout方法中(并注释其余代码)并查看是否仍未处理异常。如果没有,请将其放入connect()方法并再次检查。

答案 1 :(得分:0)

评论后,我尝试实施新的解决方案,现在是:

构造

/* ------------------------------------------------------------------------- */
public Database(string source, string database, string user, string password,
    int timeout, DelegateConnectionResult connectResult)
{
    this.timeout = timeout;
    this.connectResult = connectResult;

    connectString = "server=" + source +
          ";database=" + database +
          ";uid=" + user + 
          ";pwd=" + password + 
          ";connect timeout=" + Convert.ToInt16(timeout / 1000);
}

Asynchonous connect:

/* ------------------------------------------------------------------------- */
public void connectAsync()
{
    connectThread = new Thread(connectSync);
    connectThread.Start();
}

同步连接:

/* ------------------------------------------------------------------------- */
public void connectSync()
{
    connection = new SqlConnection(connectString);

    try
    {
        connection.Open();
        transac = connection.BeginTransaction();
        command = connection.CreateCommand();
        command.Transaction = transac;

        if (connection.State == ConnectionState.Open)
            connectResult(true);
        else
            connectResult(false);
    }
    catch
    {
        connectResult(false);
    }
}

我从这篇文章中找到了解决原始问题女巫SqlException的方法:它与Visual Studio调试器中的异常设置有关。如果我在Exceptions列表中的SqlException中取消选中“throw”选项(在Visual Studio中为CTRL + D + E),我终于能够捕获异常。