Terminating a Transaction in an Exception Handler
Calling Session.commit
from an exception handler that wraps a request participating in a multi-statement transaction may raise java.lang.IllegalStateException
. You may always safely call Session.rollback
from such a handler.
Usually, an exception raised during multi-statement transaction processing leaves the Session
open, allowing you to continue working in the transaction after handling the exception. However, in order to preserve consistency, exceptions occurring under the following circumstances always roll back the transaction:
After an XQuery statement has finished but before the XCC request is completed
In the middle of an explicit commit or rollback
If such a rollback occurs, the current transaction is terminated before control reaches your exception handler. Calling Session.commit
when there is no active transaction raises a java.lang.IllegalStateException
. Calling Session.rollback
when there is no active transaction does not raise an exception, so rollback from a handler is always safe.
Therefore, it is usually only safe to call Session.commit
from an exception handler for specific errors you expect to receive and for which you can predict the state of the transaction.