Andremoniy
Andremoniy

Reputation: 34920

Which exception should be thrown from a transactional method according to JTA standard?

Let us say that we develop some custom JTA implementation.

Consider the following method:

@Transactional
public void foo() {
   em.save(...); // some interaction with EntityManager
   throw new IllegalStateException("Foo");
}

Let us say that em.save(...) works fine without throwing an exception. So we always end up with throw new IllegalStateException("Foo"). It is clear that transaction should be rolled back in this case.

The question is: in runtime, according to JTA standard, should invocation of the foo() method throw a RollbackException or the original IllegalStateException?

In other words:

try {
   foo();
} catch (Exception e) {
   // What type of exception we should expect here?
}

My personal point of view is that the original exception (IllegalStateException("Foo") in this case) should be expected. However I would like to receive some answer strictly based on JTA documentation or other conventional contracts.

Upvotes: 2

Views: 44

Answers (1)

rcomblen
rcomblen

Reputation: 4649

The documentation of RollbackException (https://docs.oracle.com/javaee/7/api/javax/persistence/RollbackException.html) mentions:

Thrown by the persistence provider when EntityTransaction.commit() fails.

In your case, EntityTransaction.commit() should never called, so this exception shall not be thrown.

Upvotes: 1

Related Questions