设为首页 收藏本站
查看: 1777|回复: 0

[经验分享] Ibatis.net + Npgsql +PostgreSql 多线程“Timeout while getting a connection from pool

[复制链接]

尚未签到

发表于 2016-11-21 11:03:54 | 显示全部楼层 |阅读模式
  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
  解决方案:
  无

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303406-1-1.html 上篇帖子: PostgreSQL 中的递归查询 与oracle 的比较 下篇帖子: PHP实现在PostgreSQL里的Bytea字段中读写文献或图片
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表