I'm using Hibernate 3.6.9 with Atomikos and Spring 3.1. After reading Where does the @Transactional annotation belong? I've removed @Transactional annotations from all DAO's and I've left them only on Service's. After removing those annotations on any dao db operation I receive
org.hibernate.HibernateException: Unable to locate current JTA transaction
My configuration:
<tx:annotation-driven transaction-manager="jtaTransactionManager" />
<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="jtaTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
</bean>
<!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<!-- when close is called, should we force transactions to terminate or
not? -->
<property name="forceShutdown" value="false" />
</bean>
<!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean>
Session factory properties:
<prop key="hibernate.connection.isolation">3</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory
</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
</prop>
How should I manage DAO's transactions, and how can I use DAOs outside Service's? Is the only way to solve this to start transactions manualy (Transactional with propagation requires_new) in any layer that uses daos? However when using Transactional with DAOs I've encountered problems with lazy initialization exceptions (transaction is closed before presentation layer - tries to initilizize entity fields).
Edit:
How I should manage transactions when spring mvc's controller can access directly DAO? Should controller be transactional?
My problem also appears while login process, because spring security uses dao (without @Transactional) so no layer starts a transaction?
Adding @Transactional to e.g. daos used by spring security solves the problem -> when there is @Transactional everything works, but its not possible to use db without this annotations. But adding @Transactional to some DAOs brings problems, because when spring mvc wants to display some data lazy initialization exception appears, and then only manual Hibernate.initialize in dao works (because last @Transactional closes transaction before presentation layer!).