[cdi-dev] [JBoss JIRA] (CDI-724) Transactional observers fired in inconsistent manner when synchronisation cannot be placed
Jan-Willem Gmelig Meyling (JIRA)
issues at jboss.org
Mon Mar 19 14:45:00 EDT 2018
Jan-Willem Gmelig Meyling created CDI-724:
Summary: Transactional observers fired in inconsistent manner when synchronisation cannot be placed
Project: CDI Specification Issues
Issue Type: Feature Request
Affects Versions: 2.0 .Final
Reporter: Jan-Willem Gmelig Meyling
I have noticed some unexpected behaviour with the current workings of transactional observers. I have initially reported these issues under WELD-2444 , where [~mkouba] pointed out that WELD simply followed the CDI spec here:
??If the transaction is in progress, but javax.transaction.Synchronization callback cannot be registered due to the transaction being already marked for rollback or in state where javax.transaction.Synchronization callbacks cannot be registered, the before completion, after completion and after failure observer methods are notified at the same time as other observers, but after_success observer methods get skipped.??
This choice has to some very unexpected side effects, namely:
* If the synchronisation cannot be placed, AFTER_FAILURE observers will be invoked, even if the actual transaction succeeded or is about to succeed. This may confuse users that want to use AFTER_FAILIURE if and only if the actual transaction rolled back.
* If the synchronisation cannot be placed, AFTER_FAILURE and AFTER_COMPLETION will be invoked immediately. This may confuse users that expect the actual transaction to be finished during the execution of the AFTER_FAILURE and AFTER_COMPLETION observers.
* If the synchronisation cannot be placed, the exception is swallowed silently (with the only mechanism to observe the failure being the unfortunate invocation of the AFTER_FAILURE observer). This may make it very difficult for someone trying to figure out why the transactional observer was never invoked in the first place.
In my opinion would be best to just throw an exception if the synchronisation cannot be placed, all of the above quirks will then be resolved.
While at it: the issue originates from the fact that the synchronisation couldn't be added during the COMMITTING state. While I do understand this limitation, I think it might be worthwhile to consider to use an interposed (run last) synchronisation rather than plain transaction synchronisations. This would AFAIK also allow placing additional synchronisations while processing the existing synchronisations (except the interposed synchronisations that is). This can for example come in handy, when fireing events from JPA entity listeners (which are typically invoked during FLUSH, which is on itself is triggered through a transaction synchronisation).
This message was sent by Atlassian JIRA
More information about the cdi-dev