views:

1214

answers:

4

If I use @Transactional in my DAO will all of my EntityManager queries be encapsulated with commit and close? Or do I need to use Spring template (JPA template, Hibernate template)? What's the difference between using @Transactional and Spring template?

+2  A: 

The Spring template classes are only there to provide a nicer API for doing persistence operations - they do not deal with transactions. If you want to have transactional operations, you either need to use the @Transactional annotation approach, or use TransactionTemplate.

skaffman
A: 

do u mean usin @transactional will encapsulate my dao methods with commit,close or when using spring transaction template (jpatemplate, hibernatetemplate) ?

cometta
A: 

When you use @transactional with the proper Spring configuration, Spring will recognize that the method needs an transaction and will handle the transaction creation, commit and close for you.

Like skaffman said, @transactional is not directly tied to the template classes. They can be used for any class that may need transactions.

James McMahon
+1  A: 

The difference is that using annotation-based transaction demarcation (@Transactional) and the TransactionTemplate is that using the TransactionTemplate couples you to Spring's transaction infrastructure and means that you will programmatically handle setting the transaction status if the transaction should be rolled back. You can use annotation-based transaction demarcation with the Spring transaction support or with AspectJ transactions outside of a Spring container.

See also the online documentation for transactions in Spring.

Paul Morie