user4865748
user4865748

Reputation:

TransactionAttributeType.REQUIRES_NEW in conjunction with @Observes(during = TransactionPhase.AFTER_SUCCESS)

I recently stumbled into the next piece of Java EE6 code:

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
        public void example(@Observes(during = TransactionPhase.AFTER_SUCCESS) final ExampleEvent exampleEvent) {

Is REQUIRES_NEW really needed? I mean example method will always be called only after any previous transaction has ended successfully ( because of TransactionPhase.AFTER_SUCCESS).

Or am I missing something?

Upvotes: 1

Views: 824

Answers (1)

Dennis Kriechel
Dennis Kriechel

Reputation: 3749

Your are only observing ExampleEvent, so your example()-Method will not be called by itself (based on the @TransactionAttribute) unless your do something like this:

 @Inject
 private Event<ExampleEvent> exampleEvent;


 @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
 public void example(@Observes(during = TransactionPhase.AFTER_SUCCESS) final ExampleEvent exampleEvent) {
    ExampleEvent event = new ExampleEvent();
    exampleEvent.fire(event);
}

It makes sense to keep the @TransactionAttribute because the previous transaction just finished (AFTER_SUCCESS), so there is no current transaction therefore a new one is needed to be created. It can be possible that this would be done automatically (depending on the AS implementation) even without the annotation, but the result should be the same.

Upvotes: 1

Related Questions