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[T](RequestScope request, ISqlMapSession session, Object parameterObject, T resultObject)
   在 IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForObject[T](ISqlMapSession session, Object parameterObject, T resultObject)
   在 IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForObject[T](ISqlMapSession session, Object parameterObject)
   在 IBatisNet.DataMapper.SqlMapper.QueryForObject[T](String statementName, Object parameterObject)
   在 Banger.Dao.IbatisMapper.IbatisObjectMapper.QueryValue[T](String statement, Object conds)
   在 Banger.Css.Common.CommonDao.QueryValue[T](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

 解决方案:

  无

转载于:https://www.cnblogs.com/ruanzb/archive/2011/04/21/2023286.html

猜你喜欢

转载自blog.csdn.net/weixin_33756418/article/details/94018890