0

I am using the Spring configuration to test Spring-Hibernate Transactions.

<beans ...>

    <tx:annotation-driven  transaction-manager="transactionManager"/>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!--  hibernate 4 onwards annotationsessionfactorybean is replaced with localsessionfactory bean -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.fg.arch.test.transaction.Foo</value>
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <!-- <prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop> -->
            </props>
        </property>
    </bean>
</beans>

My service layer is annotated with @Transactional.

This is my DAO:

public class FooHibernateDaoImpl implements FooDao {

    private SessionFactory sessionFactory;  

    public void testFoo(Foo foo) throws Throwable {
        System.out.println(" --- ");
        sessionFactory.openSession().save(foo);     
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

Explicitly opening the session using the openSession() method does not cause a problem however when I change to getCurrentSession() I am getting an exception.

I have two questions.

  1. Is it good practice to call openSession() in every DAO method.
  2. How can I make getCurrentSession() work so that it will not give me an exception like no active transaction present ?

Thanks.

JamesENL
  • 6,400
  • 6
  • 39
  • 64
user2775185
  • 1,099
  • 3
  • 17
  • 30
  • 1
    Remove `hibernate.current_session_context_class` that breaks proper spring tx management. Never set it when using spring unless you are using JTA. And make sure that your `@Transactional` annotated beans are loaded in the same context as where the `` is. – M. Deinum Jun 20 '14 at 05:42

1 Answers1

2

To answer your questions:

  1. No, its not. The @Transactional annotation that should be on your service class method calling testFoo() is opening the session for you. You should use getCurrentSession() in the DAO to get this session.

  2. You can, but you shouldn't. That's the entire point of using the Hibernate SessionFactory with annotation based transaction management. As long as you are marking your service methods transactional, you shouldn't have a problem.

As a side note, why are you not Autowiring your SessionFactory? Don't use setters to set something that should be Autowired. Otherwise you may as well not use Spring.

JamesENL
  • 6,400
  • 6
  • 39
  • 64
  • Thanks James, but then for getCurrentSession why i am getting exception "save is not valid without active trasaction" – user2775185 Jun 20 '14 at 05:23
  • is the service implementing an interface and is the method calling your `testFoo` method annotated as `@Transactional` – JamesENL Jun 20 '14 at 05:25
  • also as a shameless self-plug if you are new to Hibernate you might find [this](http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring) helpful – JamesENL Jun 20 '14 at 05:27
  • Yes. My service is implementing an interface but method is not marked @transactional – user2775185 Jun 20 '14 at 05:41
  • See my update, a session won't be created if your service layer isn't marked `@Transactional` because the Hibernate `TransactionManager` won't be invoked – JamesENL Jun 20 '14 at 05:43
  • I have marked Service class @Transactional so my understanding is all methods will be transactional. right ? – user2775185 Jun 20 '14 at 05:46
  • In theory yes, but theories are treacherous things. Post your entire service class – JamesENL Jun 20 '14 at 05:47
  • Fixed the Issue. It was because of the below mentioned property. thread – user2775185 Jun 20 '14 at 06:37