0
votes

Well, I possibly might be wrong in this question, but I need your valuable views/suggestions to stand on right path.

I want to know the good, better and best way to handle transaction management in Spring + Hibernate, and Spring + JDBCTemplate and pros and cons of each.

Some schemes/approaches that I know to do the same are:

1) Spring Transaction Management using AOP Annotations(@Aspect, @PointCut with Advice like @Before, @After, @Around etc) .

Question - 1: I earlier done a Logging sample with Spring AOP, but i have no clue for doing the same for Transaction Management. Will the Transaction Start, Roll back and Commit will be done in that Aspect class with with Advice like @Before, @After, @Around ? Please share some code sample/snippet for this.

Question - 2: Does this approach required the Hibernate Transaction Manager Strongly, or can implemented without it?

2) Spring Transaction Manager with @Transactional Annotation : Another transaction management scheme that involve Hibernate Transaction Manager for declarative transaction management with @Transactional Annotation.

Question - 3: Using @Transactional Annotation on a method guarantees all the transaction management in that method body but How will you manage the roll-back feature with this way in Multi-DAO operation on different entities in that particular method? Say,you are doing 3-4 different DAO operation(on different DAO Method with @Transactional) in that method and at 3rd DAO operation some exception occurs, How will you rollback.

Please share your views!

Regards,

Arun

1
Why reinvent the wheel and try to implement your own transaction management? So imho option 1 isn't an option. To use Spring with transactions you need a PlatformTransactionManager implementation which to use depends on which persistence mechanism you use or if you need to use JTA or not. Your transactional boundary should be the service layer not the individual dao methods (if you do the latter you will have multiple transactions instead of one).M. Deinum
@M. Deinum : Does it mean that there is nothing like Transaction Management with AOP Annotations : @Before and @Afteror it is not the right choice to use them in this way?Arun Kumar
Your best choice is indeed Spring Transaction Manager with @Transactional Annotation. And if you invoke a DAO from another DAO, which both are transactionals there´s no problem with the atomicity, the whole transaction is atomic and there will be rollback for all the callspaul
Transaction management is done using AOP however why would you roll your own if spring already provides a well tested and used one?M. Deinum

1 Answers

1
votes

Think you should use option 2 (e.g. Spring transaction management stuff) because it has been already implemented and tested:). About 'Question 3': it depends of propagation policy that you have chosen for all your "transactional" DAO API. Please, check this javadoc @Transactional and 'propagation' section. Hope this helps.