1. 程式人生 > >Java調用存儲過程,隨著按鈕點擊增多,調用存儲過程也增多,會出現超時問題

Java調用存儲過程,隨著按鈕點擊增多,調用存儲過程也增多,會出現超時問題

audit delegate public stack ktr urn exe trac ive

剛開始代碼是這樣的直接通過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調用存儲過程,隨著按鈕點擊增多,調用存儲過程也增多,會出現超時問題