*
The moose likes Spring and the fly likes Understand regarding REQUIRED and REQUIRES_NEW Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Understand regarding REQUIRED and REQUIRES_NEW" Watch "Understand regarding REQUIRED and REQUIRES_NEW" New topic
Author

Understand regarding REQUIRED and REQUIRES_NEW

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

I understood that when Transaction attribute is REQUIRED, the transaction continues in the existing Transaction and if its REQUIRES_NEW, the transaction starts in a new transaction suspendin the existing transaction.

But I don't find any concrete evidence for the same.

Below is the code snippet i tried



I expected following things, but never happened

1. When inner method completes successfully and exception is thrown in outer method, I expected the inner table will not be rolledback as it is in a new transaction. But both tables were rolled back.

2. When inner table throws error, data from inner table is rolledback but not sure why data fro outer table is also rolled back.

All this behaviour seems to be following ACID, but what is the difference between REQUIRED and REQUIRES_NEW. Ideally both seems to be doing something.

Could someone clarify this


Ashwin Sridhar
SCJP | SCWCD | OCA
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

How are you running this code? (whats your main like)?

For @Transactional annotation to work you need have a TransactionProxy (created automatically) when you use <tx:annotation-driven>
However spring proxies don't work in nested call scenario's - if a method of the class calls another method in the same class the proxy gets by-passed.

Provide your spring config & your main, if you need more info ...

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

Please find below my config and java files.



config file is

Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

yep your code looks good - but where is the main? how do you run this code?

To try out transaction - here is a suggestion. Put myMethod2() in a different class say Test2. Do you now notice any difference in the transaction working between Propagation.REQUIRED and Propagation.REQUIRES_NEW
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I really don't find any appreciable difference between the two. Atleast am not able to notice.

In main i just get the context and call the method. I use applicationContext to do this.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

When i had the second method within another class, the inner transaction gets committed and outer transaction rolls back when i throw a exception in outer transaction.

Could you ellaborate this behaviour
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

The answer depends on what you already know.

Do you know how Spring works when you add a @Transactional annotation?
Ans: It does so by creating a proxy class for the class which has annotated methods.

Do you know how Spring Proxy object works when one method in the proxied class calls another method in the same proxied class?
Ans: Sprig is not able to handle this scenario implicitly. Any annotation on the called method would be ignored (since the call happens on 'this' rather than on the Proxy)
You need to switch to AspectJ to handle such scenario's

If you really like to understand this behavior I recommend reading this section of the Spring documentation.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

thank you very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understand regarding REQUIRED and REQUIRES_NEW
 
Similar Threads
Transactions
how does rollbackFor work ?
Understanding Propagation Required and Nested scnearios
JTA and nested transactions
Clarification on Propagation Nested in spring