We have a Java application running on JBoss 5.1 and in some cases we need to prevent a transaction from being closed in case a JDBCException
is thrown by some underlying method.
We have an EJB method that looks like the following one
@PersistenceContext(unitName = "bar")
public EntityManager em;
public Object foo() {
try {
insert(stuff);
return stuff;
} (catch PersistenceException p) {
Object t = load(id);
if (t != null) {
find(t);
return t;
}
}
}
If insert
fails because of a PersistenceException
(which wraps a JDBCException
caused by a constraint violation), we want to continue execution with load
within the same transaction.
We are unable to do it right now because the transaction is closed by the container. Here's what we see in the logs:
org.hibernate.exception.GenericJDBCException: Cannot open connection
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
...
Caused by: javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: 7f000101:85fe:4f04679d:182 status: ActionStatus.ABORT_ONLY >
The EJB class is marked with the following annotations
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
Is there any proper way to prevent the transaction from rolling back in just this specific case?