tags:

views:

421

answers:

3

when using @transactional do i need to use jpatemplate/hibernatetemplate ?

+1  A: 

No, you don't. Spring has a built-in transaction manager that can be used for simple transactions, e.g. if you don't need to track transactions across more than one DataSource. The configuration should be as simple as this:

<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource"/>
</bean>

Where the bean named "dataSource" is some DataSource bean configured elsewhere in your XML file.

However if you're using JPA or Hibernate, it would be a good idea to use the JPATransactionManager or HibernateTransactionManager, respectively.

If you really wanted to you could also use JTA, which is Sun's standard transaction implementation. I think the spring class is called JTATransactionManager.

Using transaction managers other than Spring's out-of-the-box one (the one defined in the XML config above) will give you the ability to use transactions across multiple DataSources.

Alex Beardsley
+1  A: 

The answer depends on the what version of Hibernate you are using. With later versions the simple answer is no you don't need the templates. See here for a comprehensive discussion:

http://blog.springsource.com/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/

Pablojim
+1  A: 

Hi

I came across an article that explains the process of implementing TxManager using @Transactional in depth. If you are interested, you can check this article here. I tried and this works!

sundary