Java 管理多个查询使用语句和结果集的可选查询
目前,我们有一个java应用程序,它有许多不同的查询,并不是所有的查询都在一个特定的时间运行。因此,对于每个查询,我们计划有一个新的语句和结果集,并立即关闭它们?下面给出了我们现在如何运行查询的代码片段。我们试图用try-and-catch覆盖每个查询,但问题是如果查询失败,回滚将无法在全局级别工作。如何最好地将它们放置到位,以确保没有内存泄漏呢Java 管理多个查询使用语句和结果集的可选查询,java,try-catch,resultset,Java,Try Catch,Resultset,目前,我们有一个java应用程序,它有许多不同的查询,并不是所有的查询都在一个特定的时间运行。因此,对于每个查询,我们计划有一个新的语句和结果集,并立即关闭它们?下面给出了我们现在如何运行查询的代码片段。我们试图用try-and-catch覆盖每个查询,但问题是如果查询失败,回滚将无法在全局级别工作。如何最好地将它们放置到位,以确保没有内存泄漏呢 try{ //main try outside //lots of inner queries run based on some logics o
try{ //main try outside
//lots of inner queries run based on some logics of if else etc
//sample query of opening and closing both statement and resultsets.
Statement stmt1 = null;
stmt1 = dbconn.createStatement();
String selectQuery1 = "Select query";
ResultSet rs1 = stmt1 .executeQuery(selectQuery1);
while(rs1.next()) {
//process here
}
try{
if (rs1 != null ){
rs1.close();
}
if (stmt1!= null ){
stmt1.close()
}
}
catch(SQLException ex){
ex.printStackTrace(System.out);
}
dbconn.commit();
}
catch (SQLException ex) {
try {
dbconn.rollback();
}
catch (Exception rollback){
rollback.printStackTrace(System.out);
}
}
catch (Exception e){
try{
dbconn.rollback();
}
catch (Exception rollback) {
rollback.printStackTrace(System.out);
}
}
要使回滚工作,首先必须检查
autoCommit
最初是否设置为false
。您只希望在所有操作都已成功执行时提交
一种方法是使用如下结构:
Connection connection = getDBConnection(); //Depends on how you get your connection
boolean autoCommit = connection.getAutoCommit();
try{
//Set autoCommit to false. You will manage commiting your transaction
connection.setAutoCommit(false);
//Perform your sql operation
if(doCommit){ //all your ops have successfully executed, you can use a flag for this
connection.commit();
}
}catch(Exception exe){
//Rollback
}finally{
connection.setAutoCommit(autoCommit); //Set autoCommit to its initial value
}
“回滚在全局级别不起作用”-可能自动提交设置为“开”?您看过SpringJDBC模板吗?它减少了所有这些SQLException的混乱。在其他任何操作开始之前,自动提交在最顶端设置为false;设置在最上面。但我想知道我们使用的是什么方法,是不是正确的方法?这里的问题是,对于单个查询,这是可以的,我们有多种类型的查询,并且在任何时候都不是所有的查询都被执行。那么你对这种情况有什么建议呢?我想除了设置一个变量并在最后检查它的状态之外,没有其他方法了?
Please try keeping dbconn.setAutoCommit(false) as first statement in your first try block so that it will not insert/update/delete query unless you say dbconn.commit()
try{
conn.setAutoCommit(false);
Statement stmt1 = null;
stmt1 = dbconn.createStatement();
String selectQuery1 = "Select query";
ResultSet rs1 = stmt1 .executeQuery(selectQuery1);
while(rs1.next()) {
//process here
}
conn.commit();
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
try{
if(conn!=null)
conn.rollback();
}catch(SQLException se2){
se2.printStackTrace();
}//end try
}catch(Exception e){
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(rs!=null)
rs.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}//