EJB容器管理事务-提交和回滚

本文探讨了EJB容器管理的事务处理,包括在异常情况下如何进行事务提交和回滚。代码示例展示了在方法中设置回滚状态与抛出异常对事务的影响。容器会在系统异常、特定EJBException或调用EJBContext.setRollbackOnly()时回滚事务。同时,指出Bean管理事务与容器管理事务在使用setRollbackOnly()方法上的区别。
摘要由CSDN通过智能技术生成

1.  Example
SwiftMsgCollectorBean.handleMsg() -->
SwiftMsgCollectorBean 是容器管理事务, 方法配置为 Required.

代码A:
try {
 //4. to overwrite, so delete replica if exist
 this.delReplica(refNo);
   
 //5. Format the msg
 swiftMsg = this.replaceTabByBlank(swiftMsg);
   
 //6. save the msg
 this.insertSwiftMsg(orderNo, refNo, swiftMsg);
      
} catch (Exception e) {
 e.printStackTrace();

}

代码A:即使insertSwiftMsg抛出异常, delReplica 的事务也会提交。


代码B:
try {
 //4. to overwrite, so delete replica if exist
 this.delReplica(refNo);
   
 //5. Format the msg
 swiftMsg = this.replaceTabByBlank(swiftMsg);
   
 //6. save the msg
 this.insertSwiftMsg(orderNo, refNo, swiftMsg);
      
} catch (Exception e) {
 sessionContext.setRollbackOnly();  --> 如果insertSwiftMsg抛出异常&#x

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值