栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

批量执行sql使用hikariCP连接池,资源耗尽

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

批量执行sql使用hikariCP连接池,资源耗尽

        使用hibernate框架时,通常数据库的交互离不开框架的缓存机制。如果因业务需求,需要批量执行更新语句等情况也是难免的,但是直接多次执行更新语句明显性能会下降了许多。因此,可以采用jdbc的批量处理sql。可在、原来程序使用c3p0线程池,换成hikariCP后,在其使用一小段时间后,会陷入疑似阻塞的状态。也就是说整个程序不再执行任务了。

        经排查,是数据库连接池没有回收,最终导致无可用资源导致的。
        下面贴出我的修改部分,希望能帮到有缘人....

public void updateSqlList(List updateSqlList) throws Exception {
   Session session = this.getSessionFactory().openSession();
   Connection connection = session.connection();
   Statement statement = null;
   try {
      connection.setAutoCommit(false);
      statement = connection.createStatement();
      int updateSqlListSize = updateSqlList.size();

      long startTimeTotal = System.currentTimeMillis();
      for (int i = 0; i < updateSqlListSize; i++) {
         statement.addBatch(updateSqlList.get(i));
         if (i % 1000 == 0 && i != 0) {
            long startTime = System.currentTimeMillis();
            int[] count = new int[0];
            try {
               count = statement.executeBatch();
            } catch (SQLException e) {
               if(null != count){
                  for (int j = 0, countSize=count.length; j < countSize; j++) {
                     int i1 = count[j];
                     if(Statement.EXECUTE_FAILED == count[j]){
                        System.out.println("语句有误:" + updateSqlList.get(j));
                     }
                  }
               }
               
               logger.error("语句有误", e);
               throw e;
            }
            connection.commit();
            statement.clearBatch();
 
         }
      }

  
      statement.executeBatch();
      connection.commit();
      connection.setAutoCommit(true);
   } catch (Exception e) {
      logger.error("批量执行语句失败!", e);
      try {
         connection.rollback();
      } catch (Exception e1) {
         logger.error("回滚批量执行语句失败!", e1);
      }

      throw new Exception("系统异常,批量执行语句失败!");
   } finally {
      if (statement != null) {
         try {
            statement.close();
         } catch (SQLException sqlex) {
            logger.error("无法关闭数据库连接!", sqlex);
         }
      }
      if (connection != null) {
         try {
            connection.setAutoCommit(true);
            connection.close();
            logger.info("关闭数据库连接成功...");
         } catch (SQLException sqlex) {
            logger.error("无法关闭数据库连接!", sqlex);
         }
      }
      if(null != session){
         session.close();
      }
   }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489927.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号