码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-05-22 17:47:48      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:manager   his   tde   sed   varchar   otf   use   out   bmp   

刚开始代码是这样的直接通过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;
 }

 

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

标签:manager   his   tde   sed   varchar   otf   use   out   bmp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!