• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Understand regarding REQUIRED and REQUIRES_NEW

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please find below my config and java files.



config file is

 
Saifuddin Merchant
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much
 
I child proofed my house but they still get in. Distract them with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic