这是典型的项目结构,一旦用户登录到应用程序,就会创建一个数据库连接。
Login.java{
Creating DB Connection
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
java.sql.Connection conn = DriverManager.getConnection(url, strUserDB, strPasswordDB);
CustomSession customSession = new CustomSession();
customSession.setConnectionDB(conn);
}
CustomSession.java{
private Connection conDBConnection;
// getter, setter
}
SubmitAction{
method1(ActionMapping map,
ActionForm form, HttpServletRequest request,
HttpServletResponse response)
Connection conDB = null;
CustomSession customSession = null;
AService aservice = null;
BService bservice = null;
CService cservice = null;
HttpSession session = request.getSession(false);
try{
customSession = (Connection) session.getAttribute("customSession");
conDB = objOFSession.getDBConnection();
conDB.setAutoCommit(false);
aservice = new AService(conDB);
bservice = new BService(conDB);
cservice = new CService(conDB);
aservice.method2();
bservice.method3();
cservice.method4(); // Exception comes, and commit is never executed
conn.commit();
}catch(Exception e){
conDB.rollback();
}
每个服务(* aservice,bservice,cservice)通常没有数据库连接并执行存储过程。 SP就像执行选择,更新,插入操作。
query = conConnectionDB.prepareCall(strCommand);
query.execute();
query.close();
if(errCode != 0){
throw new Exception(errCode,errorDesc);
}
}
问题 我可以看到存储过程返回了0以外的错误代码,因此它进入了if条件并抛出异常。我正在捕获Exception并调用rollback(),但是正如我所见,所有更改都不会回滚。我的交易继续进行。
我还验证了SP不包含提交语句。
由于抛出了异常,因此Java代码中的commit语句均未执行。
我也已经在开头设置了AutoCommit(false)
。
请让我知道,我该如何进一步解决这个问题。
答案 0 :(得分:1)
数据库事务包含...
...
一种数据定义语言(DDL)语句
即发出DDL声明后CREATE TABLE
声明所有在之前完成的更改被提交,而ROLLBACK
对它们没有影响。
插图示例
SQL> insert into test(x) values(1);
1 row created.
SQL>
SQL> create table tmp (x number);
Table created.
SQL>
SQL> rollback;
Rollback complete.
SQL>
SQL> select * from test;
X
----------
1