Issue
I am currently working on a spring mvc + hibernate application. It has Service and DAO layer.
What I want to know is is there a way to achieve dependent rollback among different DAO implementation.
for example is I add a entry/ row in table A mapped with Model A by using DAO implementation of A. then I am trying to do same for Table B. However while trying to same for Table C an error occurs then I wish to rollback both the changes done in Table A and Table B.
Is there a way to achieve this, Let me know any help is appreciated and thank you in advance :D
NOTE to Moderators : I know this question may sound to opinionated but be assured if I don't find any satisfactory answer in few days I will close it myself.
Solution
Obviously, you will call all these dao methods from your service class. Make that service method/class transactional. Annotate the service method/class with @Transactional
.
- This will create a new Hibernate session at the start of method execution.
- Will use that session for all your db transaction in dao layer.
- Commits/Rollback all those db transactions at the end.
If there is any Runtime exception occures, during your Table C modification, rollback will be done in Tables A & B, automatically.
Refer these
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html http://blog.jhades.org/how-does-spring-transactional-really-work/
Answered By - Ramanujan R
Answer Checked By - Gilberto Lyons (JavaFixing Admin)