java项目的事务怎么处理_java-ee – 如何回滚事务并继续处理更新?

我正试图围绕事务管理,但我很难搞清楚如何从事务回滚中恢复并继续提交新事务.下面的代码是我正在尝试做的简化版本:

@Stateless

public class MyStatelessBean1 implements MyStatelessLocal1 {

@EJB

private MyStatelessLocal1 myBean1;

@TransationAttribute(TransactionAttributeType.NEVER)

public void processObjects(List objs) {

// this method just processes the data; no need for a transaction

for(Object obj : objs) {

// If the call to process results in the transaction being rolled back,

// how do I rollback the transaction and continue to iterate over objs?

this.myBean1.process(obj);

}

}

@TransationAttribute(TransactionAttributeType.REQUIRES_NEW)

public void process(Object obj) {

// do some work with obj that must be in the scope of a transaction

}

}

如果在对process(Object obj)的调用中发生事务回滚,则抛出异常并且objs中的其余Objects不会被迭代并且不提交任何更新.如果我想回滚发生错误的事务,但继续迭代objs List,我应该怎么做呢?如果我只是像下面的代码中那样捕获异常,那么我需要做些什么才能确保事务回滚?

public void processObjects(List objs) {

// this method just processes the data; no need for a transaction

for(Object obj : objs) {

// If the call to process results in the transaction being rolled back,

// how do I rollback the transaction and continue to iterate over objs?

try {

this.myBean1.process(obj);

} catch(RuntimeException e) {

// Do I need to do anything here to clean up the transaction before continuing to iterate over the objs?

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值