[hibernate-dev] Exceptions thrown in a tx synchronization are eaten

Adam Warski adam at warski.org
Sat Jul 10 04:05:18 EDT 2010


because the thread is a bit old a small reminder what it was about:
Envers used TX Synchronizations to write the audit data to a database, before the transaction completed. However, exceptions thrown during a synchronization are "eaten" (JDBCTransaction:274). The solution then was to use, instead of a Synchronization, a BeforeTransactionCompletionProcess.

This, however, as it turns out, causes big problems in a JTA environment (with resource-local txs all works fine): audit records are sometimes not written. This is because the before tx completion process is called before tx synchronizations, and in JTA the auto-flush at the end of a session is done using a TX synchronization (AbstractEntityManagerImpl:1020). And it doesn't look like exceptions are eaten when thrown from a synchronization in JTA - otherwise any exception that occurred during a flush would go unnoticed (and I know from practice this doesn't happen ;) ).

So isn't this inconsistent? Shouldn't the exceptions in JDBCTransaction be rethrown as well?

For now a fix is to register both a BTCP and a TX Sync to write the audit records, but I'd say it's rather a temporary patch :)

On Mar 31, 2010, at 1:00 PM, Adam Warski wrote:

>> In an ideal world, a Synchronization should swallow exceptions (or do whatever it wants with it) if the exception should not tamper with the main Hibernate execution. In a word, Synchronization would be in control. It has my preference but It's a change of semantic.
> Then maybe do as Steve suggested, either:
> - introduce a new method to Synchronization (that would break existing synchronizations)
> or
> - introduce a new interface SynchronizationRollback which can be optionally implemented by the synchronization method
> Then in JDBC transaction we could do:
> if ((sync instanceof SynchronizationRollback) && ((SynchronizationRollback) sync).shouldRollback(exception) { // rollback }
> -- 
> Adam Warski
> http://www.warski.org
> http://www.softwaremill.eu
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev

Adam Warski

More information about the hibernate-dev mailing list