This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Understand regarding REQUIRED and REQUIRES_NEW

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
  • 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: 607
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi,

Please find below my config and java files.



config file is

 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 607
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thank you very much
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic