When does @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) commit?
Asked Answered
M

2

40

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?

Mistaken answered 23/7, 2012 at 14:17 Comment(0)
M
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.

Moreville answered 23/7, 2012 at 14:32 Comment(6)
if we confirm this answer, you will be the winnerMistaken
+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.Phenanthrene
Thanks for your comment, you have widened my wisdom:-)Moreville
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.Retuse
I agree with Gab. "A" is the correct answer and I think Petr and David actually wanted to say the same.Unschooled
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 automaticallyAdumbrate
L
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.

Lowerclassman answered 30/3, 2013 at 10:20 Comment(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.Unschooled

© 2022 - 2024 — McMap. All rights reserved.