Pergunta

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!).

Foi útil?

Solução

IMO, you should initialize those fields of the objects in the DAOs which are later on needed in the presentation layer.

If eagerly initializing those objects feels somewhat "dirty" to you, you should introduce new presentation layer specific classes (a viewmodel) which are mapped to by your service layer (which still has an open transaction to read from if necessary).

Since I have no Java background, I don't know if "spring security" needs to access the database. If it does, you have to add those transactional attributes on the corresponding servicelayer too, as you already found out. However, I don't think that you necessarily should put transactional attributes around DAO methods, that is often one level to deep.

Outras dicas

No, the error is telling you to configure a JTA transaction manager:

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/transaction/jta/JtaTransactionManager.html

Pick the one that works best for your situation.

This Hibernate forum question might be pertinent as well:

https://forum.hibernate.org/viewtopic.php?p=2430788

Controllers should not be transactional. You've got them in the right place - they belong on services.

OpenSessionInView might be your solution:

http://springtips.blogspot.com/2007/07/open-session-in-view.html

Or it might not:

Why is Hibernate Open Session in View considered a bad practice?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top