我要连续调用多个存储过程,不成功就全部回滚,帮我看看这代码有没有问题。 public String myFunction(int arg1 ,String[] arg2,int arg3){
CallableStatement cstmt = null;
Connection conn=null;
try {
conn = this.dataBase.getConn();
conn.setAutoCommit(false);
String proc_1 = "{call proc_1(?,?,?,?,?)}";
String proc_2 = "{call proc_2(?,?)}";
String proc_3 = "{call proc_3(?,?)}"; cstmt = conn.prepareCall(proc_1) ;
for(int i=0;i<ss.length;i++){
cstmt.setInt(1,arg1);
cstmt.setString(2, arg2[i]);
cstmt.setInt(3, arg3);
cstmt.setInt(4,0);
cstmt.setString(5,"L");
cstmt.addBatch();
}
cstmt.execute();
cstmt = conn.prepareCall(proc_2) ;
cstmt.setInt(1,arg1);
cstmt.setInt(2,arg3);
cstmt.addBatch();
cstmt = conn.prepareCall(proc_3) ;
cstmt.setInt(1, arg1);
cstmt.setInt(2, arg3);
cstmt.addBatch();
cstmt.executeBatch();
conn.commit();
return "SUCCESS";
} catch (SQLException se) {
try {
conn.rollback();
if(cstmt!=null)
cstmt.close();
if(cstmt!=null)
cstmt.close();
conn.close();
} catch (SQLException e) {
}
se.printStackTrace();
return "FAIL";
}finally{
try {
if(cstmt!=null){cstmt.close();}
if(cstmt!=null){cstmt.close();}
if(conn!=null){conn.close();}
} catch (Exception e) {
}
}
}
}
CallableStatement cstmt = null;
Connection conn=null;
try {
conn = this.dataBase.getConn();
conn.setAutoCommit(false);
String proc_1 = "{call proc_1(?,?,?,?,?)}";
String proc_2 = "{call proc_2(?,?)}";
String proc_3 = "{call proc_3(?,?)}"; cstmt = conn.prepareCall(proc_1) ;
for(int i=0;i<ss.length;i++){
cstmt.setInt(1,arg1);
cstmt.setString(2, arg2[i]);
cstmt.setInt(3, arg3);
cstmt.setInt(4,0);
cstmt.setString(5,"L");
cstmt.addBatch();
}
cstmt.execute();
cstmt = conn.prepareCall(proc_2) ;
cstmt.setInt(1,arg1);
cstmt.setInt(2,arg3);
cstmt.addBatch();
cstmt = conn.prepareCall(proc_3) ;
cstmt.setInt(1, arg1);
cstmt.setInt(2, arg3);
cstmt.addBatch();
cstmt.executeBatch();
conn.commit();
return "SUCCESS";
} catch (SQLException se) {
try {
conn.rollback();
if(cstmt!=null)
cstmt.close();
if(cstmt!=null)
cstmt.close();
conn.close();
} catch (SQLException e) {
}
se.printStackTrace();
return "FAIL";
}finally{
try {
if(cstmt!=null){cstmt.close();}
if(cstmt!=null){cstmt.close();}
if(conn!=null){conn.close();}
} catch (Exception e) {
}
}
}
}
return "SUCCESS";根据执行结果判断下 if(){return "SUCCESS";}else{return "fail"}
return "SUCCESS";}else{return "fail"}
if(cstmt!=null){cstmt.close();}
怎么有两个??if(stmt.executeBatch){ conn.commit();
return "SUCCESS";}else{return "fail"}
我看不见的吧,很明显如果在cstmt.executeBatch();时出现异常,程序会跳转到catch部分去执行。