Jump to content

Java experts


Popkatapetapotapulti

Recommended Posts

2 hours ago, areyentiraidhi said:

under the hood , it is simply using AOP ***** putting under try block...

traditional ga we used to write like this

 

//get my transaction object

MyTransaction myTransaction = entityManager.getTransaction(); 
try { 
myTransaction .begin(); 

// doing alll sql stuff here


 //Commit my transaction

myTransaction .commit(); 
} catch(Exception exception)
 {
 //I will Rollback my transaction 
myTransaction .rollback(); 
throw exception;
 }

 

same thing happens  when you use @Transactional, its called declarative way .. spring takes care of that transaction handling under the hood using AOP.

you  whole method will be put under try/catch, spring is helping you not write all the boiler plate code. 

 

if the method executes fine, AOP commits will the transaction , if not (on any unchecked exception), it will perform  rollback by default.

 

 

as per this - is this how the Methd2 and Method3 will be represented?

 

 

 


try { 
myTransaction .begin(); 

// save method logic ---- Method2()

// call to microservice --- Method3()


 //Commit my transaction

myTransaction .commit(); 
} catch(Exception exception)
 {
 //I will Rollback my transaction 
myTransaction .rollback(); 
throw exception;
 }

Link to comment
Share on other sites

2 hours ago, Pavanonline said:

That’s how enterprise applications work, it’s called transaction management, not a new concept. All the db calls under the same transaction are committed at the end. Otherwise life would be hell. 
https://www.baeldung.com/java-transactions

I odnt think question clear ga you got it ani there is a microservice call after the save method, and if there is an exception in there it should rollback what was saved

Link to comment
Share on other sites

1 hour ago, Popkatapetapotapulti said:

I odnt think question clear ga you got it ani there is a microservice call after the save method, and if there is an exception in there it should rollback what was saved

yeah you are not really clear, it doesn't matter what type of calls method 3 has, as long as method3 is properly throwing the exception it should roll back all prior db transactions

Link to comment
Share on other sites

1 hour ago, Popkatapetapotapulti said:

as per this - is this how the Methd2 and Method3 will be represented?

 

 

 


try { 
myTransaction .begin(); 

// save method logic ---- Method2()

// call to microservice --- Method3()


 //Commit my transaction

myTransaction .commit(); 
} catch(Exception exception)
 {
 //I will Rollback my transaction 
myTransaction .rollback(); 
throw exception;
 }

yes, any unchecked exception in that try block will casue the rollback (from catch block).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...