【发布时间】:2017-07-17 06:53:08
【问题描述】:
过去几天我一直在崩溃。我有一个方法级别为@TransactionAttribute(TransactionAttributeType.REQUIRED) 的MDB。在不同 EJB 中调用的方法被标记为@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW),从而暂停 MDB 事务。当在新事务中引发异常时,它不会回滚并将我的更改提交到数据库。我确实用@ApplicationException(rollback=true) 标记了我检查的异常,这是代码示例
public class MDB implements MessageListener {
@EJB
Foo foo;
@Inject
ExceptionHandler exceptionHandler;
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void onMessage(Message message) {
//message gets unmarshalled here
try {
foo.makeChanges(unmarshalledMessage)
} catch (MyException ex) {
exceptionHandler.handleException(ex);
}
}
}
@Stateless
public class Foo {
@EJB
DatabaseDao databaseDao;
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void makeChanges(String unmarshalledMessage) throws MyException {
//Some logic to handle message
try (Connection conn = databaseDao.getConnection()) {
for (ManipulatedString string : listOfManipulatedString) {
try {
databaseDao.makeChanges(manipulatedString);
//RuntimeException thrown
} catch (RuntimeException ex) {
throw new MyException(ex);
}
}
}
}
}
@ApplicationException(rollback = true)
public class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(Throwable cause) {
super(cause);
}
}
请记住,我只想回滚第二个事务(在 Foo 类中创建的事务),而不是第一个事务(MDB 中的事务)。我明确抛出了否定测试的异常。
我有一条一条地发送到数据库的记录,因此是for。在第三条记录上它失败了(我的做法),它应该回滚并且不提交任何东西,但由于某种原因它提交了前一个 .你知道为什么
我只使用 Java EE。
【问题讨论】:
标签: transactions ejb java-ee-7 jta