Popkatapetapotapulti Posted October 7, 2022 Report Posted October 7, 2022 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? Quote
csrcsr Posted October 7, 2022 Report Posted October 7, 2022 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 Quote
Popkatapetapotapulti Posted October 7, 2022 Author Report Posted October 7, 2022 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? Quote
areyentiraidhi Posted October 7, 2022 Report Posted October 7, 2022 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) Quote
csrcsr Posted October 7, 2022 Report Posted October 7, 2022 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 Quote
Popkatapetapotapulti Posted October 7, 2022 Author Report Posted October 7, 2022 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? Quote
csrcsr Posted October 7, 2022 Report Posted October 7, 2022 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 2 Quote
Popkatapetapotapulti Posted October 7, 2022 Author Report Posted October 7, 2022 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(); Quote
bharathicement Posted October 7, 2022 Report Posted October 7, 2022 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 Quote
Gorantlamdhav Posted October 7, 2022 Report Posted October 7, 2022 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 Quote
YOU Posted October 7, 2022 Report Posted October 7, 2022 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 Quote
YOU Posted October 7, 2022 Report Posted October 7, 2022 Why sql first.. then microservice call? Why not otherway around? Quote
areyentiraidhi Posted October 7, 2022 Report Posted October 7, 2022 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. Quote
Pavanonline Posted October 7, 2022 Report Posted October 7, 2022 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.