错误连接:超时已过期。从池中获取连接之前经过的超时时间

时间:2011-05-30 12:03:55

标签: c# .net mysql

public void enqueue(object e)
{
      try
      {
            DataChangeEventArgs e113 = (DataChangeEventArgs)e;
            resetdatasource();
            for (i=0; i < e113.sts.Length; i++)
            {
                  int hour113 = e113.sts[i].TimeStampNet.Hour;
                  int minute113 = e113.sts[i].TimeStampNet.Minute;
                  int second113 = e113.sts[i].TimeStampNet.Second;
                  int millisecond113 = e113.sts[i].TimeStampNet.Millisecond;
                  int year113 = e113.sts[i].TimeStampNet.Year;
                  int month113 = e113.sts[i].TimeStampNet.Month;
                  int day113 = e113.sts[i].TimeStampNet.Day;
                  DateTime sdate113 = new DateTime(year113, month113, day113, hour113, minute113, second113, millisecond113);

                  DataRow row1 = dt.NewRow();
                  row1["itemID"] = e113.sts[i].HandleClient;
                  row1["paramvalue"] = convert.ToString(e113.sts[i].DataValue);
                  row1["date_logged1"] = sdate113.ToString("dd-MM-yyyy HH:mm:ss.fff");
                  row1["Quality"] = e113.sts[i].Quality;
                  row1["date_logged"] = DateTime.FromFileTime(e113.sts[i].TimeStamp);
                  dt.Rows.Add(row1);
            }
            var threadupdate = new Thread(update);//start new thread & instance
            threadupdate.Start(dt);
      }
      catch { }
}


public void update(object dtnew)
            {
                  try
                  {
                           MySqlConnection con = new MySqlConnection(LocalConnection.GetLocalConnetionStringmysql());
                              DataSet oldvalueds=new DataSet();
                              DataTable newupdateddata = new DataTable();
                              MySqlDataAdapter da;


                              MySqlTransaction trans;
                              if (con.State == ConnectionState.Closed)
                                    con.Open();//throws exception
                              trans = con.BeginTransaction();
                              da = new MySqlDataAdapter();
                              da.InsertCommand = new MySqlCommand("INSERT INTO parameter (itemID,paramvalue, date_logged1,Quality,date_logged) " +
                                                               "VALUES (@itemID, @paramvalue,@date_logged1,@Quality,@date_logged)" +
                                                               "ON DUPLICATE KEY UPDATE " +
                                                               "itemID=VALUES(itemID),paramvalue=VALUES(paramvalue),date_logged1=VALUES(date_logged1),Quality=VALUES(Quality),date_logged=VALUES(date_logged)", con);

                              da.InsertCommand.Parameters.Add("@itemID", MySqlDbType.VarChar, 250, "itemID");
                              da.InsertCommand.Parameters.Add("@paramvalue", MySqlDbType.VarChar, 250, "paramvalue");
                              da.InsertCommand.Parameters.Add("@date_logged1", MySqlDbType.VarChar, 250, "date_logged1");
                              da.InsertCommand.Parameters.Add("@Quality", MySqlDbType.VarChar, 250, "Quality");
                              da.InsertCommand.Parameters.Add("@date_logged", MySqlDbType.VarChar, 250, "date_logged");

                           da.InsertCommand.Transaction = trans;
                              DataTable newupdateddt = (DataTable)dtnew;
                            int k = da.Update(newupdateddt);//exception thrown 2nd
                                 trans.Commit();
                                 con.Close();
                     }

                  catch(Exception ex) {

                     }
                  finally
                  {


                  }
            }
 

它将抛出异常,例如“连接错误:超时已到期。在从池中获取连接之前已经过了超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。”< / p>

我将在连接字符串中设置timeout = 200,抛出此异常 “尝试锁定时发现死锁;尝试重启事务” 提前谢谢

2 个答案:

答案 0 :(得分:2)

您正在设置打开的连接等 - 而不是将其丢弃。我实际上并不清楚为什么,因为对于适配器(da)而言,这实际上似乎并不是使用。但是:通常,您应该在所有此类(using)对象周围使用IDisposable

using(var con = new MySqlConnection(...))
{
    ... all the code that uses the connection here ...
}

这可确保对象以干净,快速的方式处理 - 将底层连接放回池中。这同样适用于实现IDisposable的任何其他对象 - 例如,可能该事务。

答案 1 :(得分:0)

此错误的原因可能是所有连接都在使用中并且已达到最大池大小。尝试增加连接字符串中的Pool Size,例如Max Pool Size = 100