binghai03 发表于 2016-11-21 11:03:54

Ibatis.net + Npgsql +PostgreSql 多线程“Timeout while getting a connection from pool

  Ibatis.net + Npgsql +PostgreSql
  数据的连接检查过关闭的都很好没有漏掉的 但还是经常遇到这个问题
Timeout while getting a connection from pool.
  Ibatis.net + Devart +PostgreSql
  经常出现“sorry, too many clients already”
  案例:
  for (int i = 0; i < 10; i++)
{
    ThreadProcessor testThread1 = ThreadProcessor.Create(TestTimeOutQueryCus, OnCompleted);
  testThread1.Name = "testTimeOutThread";
    testThread1.Start();
  }
  for (int i = 0; i < 10; i++)
{
    ThreadProcessor testThread2 = ThreadProcessor.Create(TestNotTimeOutQueryCus, OnCompleted);
    testThread2.Name = "testNotTimeOutThread";
    testThread2.Start();
}
  private bool isSotp = false;
      private void TestTimeOutQueryCus()
      {
            int count = 0;
            while (!isSotp)
            {
                try
                {
                  count++;
                  int threadId = _threadId;
                  //ICustomerModule module =
                  //    (ICustomerModule)ModuleContext.GetModule(typeof(ICustomerModule));
                  //ICustomer[] cus = module.GetCustomer("10000");
  testTimeOutQuery(threadId, count);
  
                  //testNpgsql();
                }
                catch (Exception ex)
                {
                  WriteTxt("测试多线程QueryCustomer-Error--" + ex.ToString());
                }
            }
            WriteTxt("超时线程停止");
  }
  private void TestNotTimeOutQueryCus()
      {
            int count = 0;
            while (true)
            {
                try
                {
                  count++;
                  int threadId = _threadId;
                  //ICustomerModule module =
                  //    (ICustomerModule)ModuleContext.GetModule(typeof(ICustomerModule));
                  //ICustomer[] cus = module.GetCustomer("10000");
  testNotTimeOutQuery(threadId, count);
  
                  //testNpgsql();
                }
                catch (Exception ex)
                {
                  WriteTxt("测试多线程QueryCustomer-Error--" + ex.ToString());
                }
            }
  }
  private void testTimeOutQuery(int threadId, int count)
      {
            try
            {
  string queryCus = "select *,xmin from \"Cus_Customer\" c where 1=1          and            (( EXISTS (select \"CustomerId\" from \"Cus_CustomerGroup\" where c.\"CustomerId\" = \"Cus_CustomerGroup\".\"CustomerId\" and \"GroupId\" in (4)) ))and(c.\"DefaultNumberType\" = 'Mobile1' or c.\"DefaultNumberType\" = 'Mobile2')";
                SmsOperateBatchDao.Singleton.OperateBatchSms(queryCus);
                WriteTxt("超时线程正常");
            }
            catch (Exception ex)
            {
                isSotp = true;
                WriteTxt("超时线程,异常");

            }
      }
  private void testNotTimeOutQuery(int threadId, int count)
      {
  count++;
  try
            {
  string queryCus = "select * from \"Cus_Customer\" limit 1";
                SmsOperateBatchDao.Singleton.OperateBatchSms(queryCus);
                WriteTxt("不超时线程正常");
            }
            catch (Exception ex)
            {
                WriteTxt("不超时线程,异常" + ex.ToString());
  }
      }
  private void WriteTxt(string msg)
{
    try
    {
      msg = DateTime.Now + "\r\n" + msg + "\r\n";
      
      File.AppendAllText(parth, msg);
  }
    catch (Exception ex)
    {
      ExLog.Write("写入文件失败:" + ex.ToString());
    }
}
  IBatisNet.DataMapper.Exceptions.DataMapperException: Unable to open connection to "DevartPgsql, DevartPgsql provider V4.95.140.0". ---> Devart.Data.PostgreSql.PgSqlException: Server did not respond within the specified timeout interval. ---> System.TimeoutException: Server did not respond within the specified timeout interval. ---> System.IO.IOException: 无法从传输连接中读取数据: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。。 ---> System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
   在 System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   在 Devart.Common.y.a(Byte[] A_0, Int32 A_1, Int32 A_2)
   --- 内部异常堆栈跟踪的结尾 ---
   在 Devart.Common.y.a(Byte[] A_0, Int32 A_1, Int32 A_2)
   在 Devart.Common.m.c(Byte[] A_0, Int32 A_1, Int32 A_2)
   在 Devart.Common.am.e(Byte[] A_0, Int32 A_1, Int32 A_2)
   --- 内部异常堆栈跟踪的结尾 ---
   在 Devart.Data.PostgreSql.y.ad()
   在 Devart.Data.PostgreSql.y.a()
   在 Devart.Common.DbConnectionInternal.ak()
   在 Devart.Common.DbConnectionFactory.a(DbConnectionBase A_0)
   在 Devart.Common.DbConnectionClosed.Open(DbConnectionBase outerConnection)
   在 Devart.Common.DbConnectionBase.Open()
   在 Devart.Data.PostgreSql.PgSqlConnection.Open()
   在 IBatisNet.DataMapper.SqlMapSession.OpenConnection(String connectionString)
   --- 内部异常堆栈跟踪的结尾 ---
   在 IBatisNet.DataMapper.SqlMapSession.OpenConnection(String connectionString)
   在 IBatisNet.DataMapper.SqlMapSession.OpenConnection()
   在 IBatisNet.DataMapper.Commands.DbCommandDecorator.System.Data.IDbCommand.ExecuteReader()
   在 IBatisNet.DataMapper.MappedStatements.MappedStatement.RunQueryForObject(RequestScope request, ISqlMapSession session, Object parameterObject, T resultObject)
   在 IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForObject(ISqlMapSession session, Object parameterObject, T resultObject)
   在 IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForObject(ISqlMapSession session, Object parameterObject)
   在 IBatisNet.DataMapper.SqlMapper.QueryForObject(String statementName, Object parameterObject)
   在 Banger.Dao.IbatisMapper.IbatisObjectMapper.QueryValue(String statement, Object conds)
   在 Banger.Css.Common.CommonDao.QueryValue(String statement, Object conds) 位置 F:\Banger-SVN\客户服务系统\trunk\03.编码\CssServer\Source\Banger.Css.Common\CommonDao.cs:行号 68
   在 Banger.Css.SmsModule.Dao.SmsOperateBatchDao.OperateBatchSms(String sql) 位置 F:\Banger-SVN\客户服务系统\trunk\03.编码\CssServer\Source\Banger.Css.SmsModule\Dao\SmsOperateBatchDao.cs:行号 26
   在 TestT.Form1.testNotTimeOutQuery(Int32 threadId, Int32 count) 位置 C:\Users\Ryan\Desktop\TestT\Form1.cs:行号 285
  解决方案:
  无
页: [1]
查看完整版本: Ibatis.net + Npgsql +PostgreSql 多线程“Timeout while getting a connection from pool