Expose STM transaction lifecycle listeners
Peter Veentjer (14 May):
What you also can do is directly access the Transaction lifecyclelistener functionality only when a lifecycle callback really is needed (so you only pay the price when it is used). If a user wants to have a deferred executing task (so after a commit completes) or a compensating task (executed when the transaction aborts), you register a Runnable when needed.
You can wrap this in some Scala magic, but essentially what you need to do is:
What you also can do is directly access the Transaction lifecyclelistener functionality only when a lifecycle callback really is needed (so you only pay the price when it is used). If a user wants to have a deferred executing task (so after a commit completes) or a compensating task (executed when the transaction aborts), you register a Runnable when needed.
You can wrap this in some Scala magic, but essentially what you need to do is:
static void registerDeferredTask(final Runnable task){
Transaction tx = getRequiredThreadLocalTransaction();
tx.registerLifecycleListener(new TransactionLifecycleListener() {
@Override
public void notify(Transaction tx, TransactionLifecycleEvent event) {
if (event == TransactionLifecycleEvent.postCommit) {
task.run();
}
}
});
}
Leave a comment
on 2010-06-02 16:30 *
By Peter Vlugter
Assigned to set to pvlugter
Status changed from New to Accepted