【发布时间】:2019-11-26 08:45:54
【问题描述】:
这是典型的项目结构,一旦用户登录到应用程序,就会创建一个数据库连接。
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 就像做 Select、update、insert 操作一样。
query = conConnectionDB.prepareCall(strCommand);
query.execute();
query.close();
if(errCode != 0){
throw new Exception(errCode,errorDesc);
}
}
问题 我可以看到存储过程返回给我的错误代码不是 0,所以它进入我的 if 条件并引发异常。我正在捕获异常并调用回滚(),但我看到没有任何更改被回滚。我的交易继续进行。
我还验证了 SP 不包含提交语句。
Java 代码中的 commit 语句都没有被执行,因为抛出了异常。
我还在开头设置了AutoCommit(false)。
请告诉我如何解决这个问题。
【问题讨论】:
-
您还检查了 SP 对函数的内部调用,例如没有提交?
-
请提供minimal reproducible example。您当前的代码只是片段,甚至不是其所示形式的有效 Java。另外,“我还在分层中设置了 AutoCommit(false)。” 是什么意思?什么是stating?为什么它没有显示在您的代码中?
-
@user7294900 是也验证了所有函数调用,没有提交
-
@MarkRotteveel
strating是一个错字,已更正,我想我已经提供了解决方案,发布一个示例将非常困难。 -
即使将其更改为启动,也不清楚您的意思。如果您无法提供证明错误行为的minimal reproducible example,您希望如何找出您的情况有什么问题。
标签: java oracle jdbc transactions rollback