Jump to content

Java experts


Popkatapetapotapulti

Recommended Posts

how do I use @Transactional ?

@Transactional

public void Method1() {

   //save to db - sql statement

Method2();

// microservice call - no sql

Method3();

}

 

I want to have this behavior when after Method2() gets successful, if there is an exception in Method3() it should rollback what was saved in Method2(). Is this possible?

Link to comment
Share on other sites

20 minutes ago, Popkatapetapotapulti said:

how do I use @Transactional ?

@Transactional

public void Method1() {

   //save to db - sql statement

Method2();

// microservice call - no sql

Method3();

}

 

I want to have this behavior when after Method2() gets successful, if there is an exception in Method3() it should rollback what was saved in Method2(). Is this possible?

RollbackFor ani oka attribute untadi bro transaction ki yiu can define for what exception it should roll back

Link to comment
Share on other sites

4 minutes ago, csrcsr said:

RollbackFor ani oka attribute untadi bro transaction ki yiu can define for what exception it should roll back

will it work even if something is already saved in Method2?

Link to comment
Share on other sites

39 minutes ago, Popkatapetapotapulti said:

how do I use @Transactional ?

@Transactional

public void Method1() {

   //save to db - sql statement

Method2();

// microservice call - no sql

Method3();

}

 

I want to have this behavior when after Method2() gets successful, if there is an exception in Method3() it should rollback what was saved in Method2(). Is this possible?

 

if you want to handle method 2 and method 3 together, refactor both of them in to method 4  and use propagation on method1 so that it wil execute on its independent transaction. 

@Transactional(propagation = Propagation.REQUIRES_NEW)

public void method1(){

//sql 

method4();

}

 

@Transsactional

public void Method4(){

method2();

//your logic

method3();

}

 

@transaction will rollback for any unchecked exception by default. for checked exceptions make it explict @Transactional(rollbackFor = Exception.class)

Link to comment
Share on other sites

16 minutes ago, Popkatapetapotapulti said:

will it work even if something is already saved in Method2?

Yaa auto commit by default false anukunta oka sari adit heck chesuko bro it will work

Link to comment
Share on other sites

30 minutes ago, areyentiraidhi said:

 

if you want to handle method 2 and method 3 together, refactor both of them in to method 4  and use propagation on method1 so that it wil execute on its independent transaction. 

@Transactional(propagation = Propagation.REQUIRES_NEW)

public void method1(){

//sql 

method4();

}

 

@Transsactional

public void Method4(){

method2();

//your logic

method3();

}

 

@transaction will rollback for any unchecked exception by default. for checked exceptions make it explict @Transactional(rollbackFor = Exception.class)

so even if something is changed in the db in method2() and committed, error in method3 will rollback the change happeneded in method2?

 

how does it wok behind the scenes?

Link to comment
Share on other sites

30 minutes ago, Popkatapetapotapulti said:

so even if something is changed in the db in method2() and committed, error in method3 will rollback the change happeneded in method2?

 

how does it wok behind the scenes?

You cannot commit bro that is what transactional block and rollback options are helping you

  • Upvote 2
Link to comment
Share on other sites

1 hour ago, csrcsr said:

You cannot commit bro that is what transactional block and rollback options are helping you

how? I am amazed at how an error in microservice call in Method3 is rolling back a transaction in Method2. Ediana documents unaya brother for reference?

 

2 hours ago, Popkatapetapotapulti said:
public void Method1() {

   //save to db - sql statement

Method2();

// microservice call - no sql

Method3();

 

Link to comment
Share on other sites

3 hours ago, csrcsr said:

You cannot commit bro that is what transactional block and rollback options are helping you

That RollBack might work only if calls are on Transactional database, preferably one Datasource. Here the problem is Method2() is calling another external Microservice. I think you nee to have Method2Undo() in case of any exception

Link to comment
Share on other sites

7 hours ago, Popkatapetapotapulti said:

how do I use @Transactional ?

@Transactional

public void Method1() {

   //save to db - sql statement

Method2();

// microservice call - no sql

Method3();

}

 

I want to have this behavior when after Method2() gets successful, if there is an exception in Method3() it should rollback what was saved in Method2(). Is this possible?

Method 4() // call micro service to rill back otherwise how do third party rollback

this should be called from rollback or catch block 

Link to comment
Share on other sites

2 hours ago, bharathicement said:

That RollBack might work only if calls are on Transactional database, preferably one Datasource. Here the problem is Method2() is calling another external Microservice. I think you nee to have Method2Undo() in case of any exception

Catch block lo rollback rasukovatam one option.. methohd 2 is not  commited rows to db yet

Link to comment
Share on other sites

5 hours ago, Popkatapetapotapulti said:

how? I am amazed at how an error in microservice call in Method3 is rolling back a transaction in Method2. Ediana documents unaya brother for reference?

 

 

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.

 

 

Link to comment
Share on other sites

6 hours ago, Popkatapetapotapulti said:

how? I am amazed at how an error in microservice call in Method3 is rolling back a transaction in Method2. Ediana documents unaya brother for reference?

 

 

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

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...