2012-08-17 88 views
0

目前我们有一个java应用程序,它有很多不同的查询,并且并不是所有的查询都在特定的时间运行。因此,对于每个查询,我们计划有一个新的语句和结果集并立即关闭它们?下面给出了我们现在如何运行查询的代码片段。我们试图用try和catch来覆盖每个查询,但是如果查询失败,回滚在全局级别上不起作用。如何最好的让他们到位以确保没有内存泄漏呢?使用语句和结果集管理多个查询可选查询

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); 
} 
} 
+1

“回滚不工作在全球范围内” - 也许自动提交设置为“开”? – alfasin 2012-08-17 18:14:05

+0

你有没有看过Spring JDBC模板?它减少了所有这些SQLExceptions的混乱。 – 2012-08-17 18:15:08

+0

在开始其他任何事情之前,自动提交在最上面设置为false。 – user837306 2012-08-18 07:13:51

回答

1

对于回滚工作,你必须先检查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 
} 
+0

我想除了单个变量的设置之外没有其他方法,并检查它的状态到底? – user837306 2012-08-18 07:11:47

1
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 
}// 
+0

yes dbconn.setAutoCommit(false);设置在最上面。但我想知道我们使用的方法是正确的方法吗? – user837306 2012-08-18 07:10:53

+0

请参阅我的回复中的代码附加代码 – 2012-08-18 08:18:19

+0

这里的问题在于,对于单个查询,我们有多种类型的查询并且在任何时候都不会执行所有查询。那么你对这种情况有什么建议? – user837306 2012-08-18 10:32:41