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

Ibatis.net + Npgsql +PostgreSql

Check the data connection is too good not to close are missing but still often encounter this problem
Timeout while getting a connection from pool.

Ibatis.net + Devart +PostgreSql

Often "sorry, too many clients already"

Case:

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 // ();
                }
                the catch (Exception EX)
                {
                    WriteTxt ( "test-Error - multithreaded QueryCustomer" + ex.ToString ());
                }
            }
            WriteTxt ( "timeout thread stop");

        }

        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 // ();
                }
                the catch (Exception EX)
                {
                    WriteTxt ( "test-Error - multithreaded QueryCustomer" + 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
            {

                queryCus = String "SELECT * from \" Cus_Customer \ "limit. 1";
                SmsOperateBatchDao.Singleton.OperateBatchSms (queryCus);
                WriteTxt ( "not normal timeout thread");
            }
            the catch (Exception EX)
            {
                WriteTxt ( "no timeout thread, abnormal "+ ex.ToString ());

            }
        }

private void WriteTxt(string msg)
{
    try
    {
        msg = DateTime.Now + "\r\n" + msg + "\r\n";
       
        File.AppendAllText(parth, msg);

    }
    The catch (Exception EX)
    {
        ExLog.Write ( "write file failed:" + 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:. Unable to read data from the transport connection: since the connection party after a period of time or not correctly answer the host connector does not respond The connection attempt failed. . ---> System.Net.Sockets.SocketException: Due to the connected party did not properly connected or reply after some time the host does not respond, the connection attempt fails.
   In System.Net.Sockets.Socket.Receive (Byte [] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   in System.Net.Sockets.NetworkStream.Read (Byte [] Buffer, offset Int32, Int32 size)
   --- end of inner exception stack trace ---
   in System.Net.Sockets.NetworkStream.Read (Byte [] buffer, Int32 offset, Int32 size)
   In Devart.Common.ya (Byte [] A_0, Int32 A_1, Int32 A_2)
   --- end of inner exception stack trace ---
   in Devart.Common.ya (Byte [] A_0, Int32 A_1, Int32 A_2)
   in Devart .Common.mc (Byte [] A_0, Int32 A_1, Int32 A_2)
   in Devart.Common.am.e (Byte [] A_0, A_1 Int32, Int32 A_2)
   --- end of inner exception stack trace ---
   in Devart .Data.PostgreSql.y.ad ()
   in Devart.Data.PostgreSql.ya ()
   in Devart.Common.DbConnectionInternal.ak ()
   in Devart.Common.DbConnectionFactory.a (DbConnectionBase A_0)
   in Devart.Common.DbConnectionClosed.Open (DbConnectionBase outerConnection)
   in Devart.Common.DbConnectionBase.Open ()
   in 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)
   In IBatisNet.DataMapper.MappedStatements.MappedStatement.ExecuteQueryForObject [T] (ISqlMapSession session, Object parameterObject)
   in IBatisNet.DataMapper.SqlMapper.QueryForObject [T] (String statementName, Object parameterObject)
   in Banger.Dao.IbatisMapper.IbatisObjectMapper.QueryValue [T ] (String of Statement, Object conds)
   [T] (String of Statement, Object conds) position F in Banger.Css.Common.CommonDao.QueryValue:. \ Banger-SVN \ customer service system \ trunk \ 03 coding \ CssServer \ Source \ Banger.Css.Common \ CommonDao.cs: line number 68
   in Banger.Css.SmsModule.Dao.SmsOperateBatchDao.OperateBatchSms (String sql) position F:. \ Banger-SVN \ customer service system \ trunk \ 03 coding \ CssServer \ Source \ Banger.Css.SmsModule \ Dao \ SmsOperateBatchDao.cs: line 26
   (Int32 threadId, Int32 count) position C in TestT.Form1.testNotTimeOutQuery: \ Users \ Ryan \ Desktop \ TestT \ Form1.cs: line number 285

 solution:

  no

Reproduced in: https: //www.cnblogs.com/ruanzb/archive/2011/04/21/2023286.html

Guess you like

Origin blog.csdn.net/weixin_33756418/article/details/94018890