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;
 }

原文地址:https://www.cnblogs.com/whhjava/p/6890413.html