• Java调用存储过程,随着按钮点击增多,调用存储过程也增多,会出现超时问题


    刚开始代码是这样的直接通过jpa连接,刚开始点击调用存储过程的按钮,没啥问题,等点击多了就会没反应:日志报数据库连接超时:

    public String execute(Entity entity) {
      
      Session session = (Session) this.getJpa().getManager().getDelegate();
      SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();
      Connection conn = null;
      String result = null;
      try {
       conn = sessionFactory.getConnectionProvider().getConnection();
       CallableStatement call = conn.prepareCall("{call SP_FORMER_BMP_INFO_AUDIT(?,?,?,?,?,?,?,?,?,?,?,?)}");
       call.setString(1, entity.getParam1());
       call.setString(2, entity.getParam2());
       call.setString(3, entity.getParam3());
       call.setString(4, entity.getParam4());
       call.setString(5, entity.getParam5());
       call.setString(6, entity.getParam6());
       call.setString(7, entity.getParam7());
       call.setString(8, entity.getParam8());
       call.setString(9, entity.getParam9());
       call.setString(10, entity.getParam10());
       call.setString(11, entity.getParam11());
       call.registerOutParameter(12, Types.VARCHAR);
       call.execute();

       result = call.getString(12);
       System.out.println("参数:"+entity.getParam1()+";"+entity.getParam2()+";"+entity.getParam3()+";"
         +entity.getParam4()+";"+entity.getParam5()+";"+entity.getParam6()+";"
         +entity.getParam7()+":"+entity.getParam8()+";"+entity.getParam9()+";"
         +entity.getParam10()+";"+entity.getParam11());
       System.out.println("老平台出、入金存储过程的执行结果是:"+result);
      } catch (SQLException e) {
       e.printStackTrace();
      }finally{
       if (conn != null) {
        try {
         conn.close();
        } catch (SQLException e) {
         e.printStackTrace();
        }
       }
      }
      
      return result;
     }

    --后来增加判断:若连接为空,则重新通过jdbc连接,暂时未出现连接超时问题

    public String execute(Entity entity) {
      
      Session session = (Session) this.getJpa().getManager().getDelegate();
      SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();
      Connection conn = null;
      String result = null;
      try {
       conn = sessionFactory.getConnectionProvider().getConnection();
       if (conn == null || conn.isClosed()) {
        try {
         Class.forName(driverClass);
         conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
         e.printStackTrace();
        }
       }
      } catch (SQLException e1) {
       e1.printStackTrace();
      }
      try {
    //   conn = sessionFactory.getConnectionProvider().getConnection();
       CallableStatement call = conn.prepareCall("{call SP_FORMER_BMP_INFO_AUDIT(?,?,?,?,?,?,?,?,?,?,?,?)}");
       call.setString(1, entity.getParam1());
       call.setString(2, entity.getParam2());
       call.setString(3, entity.getParam3());
       call.setString(4, entity.getParam4());
       call.setString(5, entity.getParam5());
       call.setString(6, entity.getParam6());
       call.setString(7, entity.getParam7());
       call.setString(8, entity.getParam8());
       call.setString(9, entity.getParam9());
       call.setString(10, entity.getParam10());
       call.setString(11, entity.getParam11());
       call.registerOutParameter(12, Types.VARCHAR);
       call.execute();

       result = call.getString(12);

       System.out.println("老平台出、入金存储过程的执行结果是:"+result);
      } catch (SQLException e) {
       e.printStackTrace();
      }finally{
       if (conn != null) {
        try {
         conn.close();
        } catch (SQLException e) {
         e.printStackTrace();
        }
       }
      }
      
      return result;
     }

  • 相关阅读:
    数据库基础理解学习-Mysql
    玫瑰花小制作分享-JavaScript(七夕专属浪漫)
    爬虫探索Chromedriver+Selenium初试
    Python 数据结构理解分享
    Python 实用第三方库安装方法
    Python安装-Pycharm+Anaconda
    oracle父子级查询数据树结构
    如何查看class文件的编译jdk版本号
    PL/SQL developer 11.0注册码
    linux 下tomcat出现 Native memory allocation (malloc) failed to allocate 1915224064 bytes for committing reserved memory问题
  • 原文地址:https://www.cnblogs.com/whhjava/p/6890413.html
Copyright © 2020-2023  润新知