39

An EJB method named Aby calls another EJB method named Bob

Bob is marked with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

When does bob transaction commits?:

a) when bob invocation ends

b) when aby invocation ends

c) other. when?

2 Answers 2

51

I think A is right. When the method Bob is being called, it creates new transaction for it and method Aby gets suspended until the Bob transaction is committed.

Also note that it has to be method from some other bean to make it transactional, methods called from the same bean do not act as a business methods.

See this great article for further explanation.

6
  • if we confirm this answer, you will be the winner
    – SDReyes
    Jul 23, 2012 at 14:51
  • 18
    +1. With my EJB Expert Group hat on I hereby confirm the answer :) Also note that the new transaction includes any interceptors (@AroundInvoke methods) that apply to Bob. Less known is that the class that declares the bob method can also declare an @AroundInvoke method and then the bean will automatically become an interceptor for itself. So the transaction starts before the invocation enters the interceptor chain (this includes the bean) and is terminated (commit or rollback) when the invocation leaves the interceptor chain. Jul 23, 2012 at 17:50
  • Thanks for your comment, you have widened my wisdom:-) Jul 23, 2012 at 18:28
  • 1
    Guys did you correctly read the question ? As far as i understand if Aby call Bob method annotated with requires_new, bob transaction is comitted before resuming aby transaction. this seems coherent with Petr Mensik quote "Aby gots suspended until the Bob transaction is commited." and so the answer is A, not B.
    – Gab
    Mar 28, 2013 at 14:36
  • 2
    Just need to be REALLY careful that REQUIRES_NEW doesn't step on stuff - 1) remember to make sure it really is a single, one time transaction and that if that fails, it's not going to corrupt other data down the path; and 2) remember that anything passed in to a method marked with REQUIRES_NEW will detach objects before returning - so if it returns a model object, it won't be attached to an entity manager. We typically call it on a method only that returns void, just to handle case 2 automatically
    – LinuxLars
    Jun 25, 2015 at 19:30
6

Actually, I think alternative a is correct. See:

EJB 3.0 - Nested Transaction != Requires New?

I have also done some research and looked into the db (Hyper Sonic) logs to actually see when it is committed in the db and it is committed when the REQUIRES_NEW method is finished. But since it's up to the container to handle the transactions maybe it could change depending on container. I've used JBoss while debugging this. And Hibernate (worth mentioning since I've tested this by checking db logs). I presume that a database write is involved since you ask about transactions.

My thoughts differs from the previous answer so it would be fun to be persuaded.

1
  • I agree, "A" is the correct answer. I think the answer by Petr Mensik and the comment by David Blevins actually intended to say the same, but mistakenly wrote "B" and not "A". Actually, even I misread the correct answer in the first place and thought "B" would be correct. But I can confirm that "A" is correct, since I checked this by myself. May 16, 2013 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.