1

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

Community
  • 1
  • 1
mmatloka
  • 1,976
  • 1
  • 20
  • 45
  • I would guess the problem is that the @Transaction Annotation on your services is not taken in account. -- Please post one of the service methods with the transation annotation, as well as the method with the statment that invoke the method and the part of the configuration that enables the transactional annotation support. – Ralph Dec 31 '11 at 13:20
  • @Ralph I've extended my question. – mmatloka Dec 31 '11 at 13:42

2 Answers2

1

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.

tobsen
  • 5,228
  • 3
  • 30
  • 48
  • Resolving lazili initialize exception by initilizing this fields in dao has also another drawback - we're still working on detached objects. I'm concidering extension of session scope to presentation layer using OpenSessionInViewFilter. – mmatloka Dec 31 '11 at 14:49
  • you should not try catch the exception but make sure that the objects passed back from your service layer to the presentation layer contain all needed - and therefore initialized - values. – tobsen Dec 31 '11 at 15:17
0

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?

Community
  • 1
  • 1
duffymo
  • 293,097
  • 41
  • 348
  • 541
  • I've configured JTA transaction manager as it was described in atomikos documentation. – mmatloka Dec 31 '11 at 13:24
  • Does the class signature conform to Spring's JTA? If not, how will it be used? Sorry, I'm not familiar with Atomikos. Check out that Hibernate forum question. It talked about session issues. – duffymo Dec 31 '11 at 13:26