关闭连接并创建一个新的连接,在295次插入后不超过最大游标数



以前的开发人员在代码中留下了这条消息。

它运行create语句并执行查询,在295条记录之后创建新的连接。

以下是java代码:

private void dbUpdate() throws SQLException, Exception {
Statement st = null;
String sql = "";
int count = 0;
try {
getNewConnection();
conn.setAutoCommit(false);
for (Iterator it = sqlList.iterator(); it.hasNext();) {
if (count < 295) { //Closes connection and creates a new one so as not to exceed max cursors
count++;
} else {
st.close();
conn.close();
getNewConnection();
count = 0;
}
sql = (String) it.next();
//                System.out.println(sql + " insert count=" + count);
st = conn.createStatement();
try {
st.executeQuery(sql);
} catch(Exception ex) {
Logger.getLogger(LoadMain.class.getName()).log(Level.SEVERE, sql);
Logger.getLogger(LoadMain.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
sb.append("n").append("Error SQL:" + sql + "|LocalizedMessage:" +ex.getLocalizedMessage());
}
}
} catch (SQLException ex) {
Logger.getLogger(LoadMain.class.getName()).log(Level.INFO, sql);
Logger.getLogger(LoadMain.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
throw new SQLException(ex);
} catch (Exception ex) {
//            Logger.getLogger(loadMain.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
throw new Exception(ex);
} finally {
try {
st.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(LoadMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

重新连接背后有什么逻辑吗?(此外,开发人员将autocommit设置为false,但没有看到提交或回滚,只有st.close((方法。(

有人能启发吗

似乎开发人员试图实现连接池,现在可以轻松地与DBCP/Hikari/其他数据库连接池集成。

例如,您不需要在DAO级别/方法上提交如果代码由具有@Transactional的方法调用,则在服务级别处理commit。

此外,您不能依赖关闭将提交或回滚,不同的oracle驱动程序可能会有不同的结果

根据javadoc,在调用close方法之前,应该尝试提交或回滚。否则,结果将由实现定义。

最新更新