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?

Community
  • 1
  • 1
SDReyes
  • 9,798
  • 16
  • 53
  • 92

2 Answers2

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.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • if we confirm this answer, you will be the winner – SDReyes Jul 23 '12 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. – David Blevins Jul 23 '12 at 17:50
  • Thanks for your comment, you have widened my wisdom:-) – Petr Mensik Jul 23 '12 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 '13 at 14:36
  • I agree with Gab. "A" is the correct answer and I think Petr and David actually wanted to say the same. – Peter Wippermann May 16 '13 at 14:27
  • 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 '15 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.

Community
  • 1
  • 1
Mike
  • 443
  • 9
  • 26
  • 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. – Peter Wippermann May 16 '13 at 14:26