Commit transaction after exception - undo setRollb

2019-09-14 06:31发布

问题:

Within my method with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) there can sometimes happen a org.eclipse.persistence.exceptions.DatabaseException due to an SQLException. I catch this exception and handle the situation by another database query. However, this query does not get through, as due to the exception, setRollbackOnly is automatically set and I am unable to commit the transaction any more at this point.

In Java SE I'd do an explicit

manager.getTransaction().rollback();
manager.getTransaction().begin();

but within the application server this is not working of course.

Can I somehow get my database changes to commit in this situation?

回答1:

The hint given in esej's comment was the solution: "Call a method on another bean annotated with REQUIRES_NEW."

Thanks



回答2:

You will 'Routine was invoked etc..' error when you attempt to start a new global transaction while the previous transaction was neither committed nor rollbacked.

In order to fix this, you should do the following:

When you catch the first exception, don't leave it up to the container to flag the transaction as 'rollback'. You have to manually flag the transaction as rollback.