26

What is this error about? "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here". My spring config file looks something like this.

<bean id="jndiDataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:/devDS</value>
    </property>
</bean>
<bean id="stsaDBFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="jndiDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>xx.yy.zz.User</value>
            <value>xx.yy.UserResponse</value>

        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbmddl.auto">create</prop>
        </props>
    </property>
</bean>

<!-- ################################### Aspects ################################################## -->

<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="stsaDBFactory" />
    </property>
</bean>

All the DAO test passes when i test them outside of the container using junit. When I deploy it in jBoss as a portal app,I get this exception. Also it works fine if i remove the portal specific configuration and make it a simple web app and deploy it on jboss.Any idea?

chedine
  • 2,384
  • 3
  • 19
  • 24
  • See also http://stackoverflow.com/questions/734614/spring-hibernate-junit-no-hibernate-session-bound-to-thread – Vadzim Oct 16 '13 at 05:20

2 Answers2

34

You have defined a TransactionManager in your spring config but you are trying to execute a hibernate query in a method that is not transactional. Try adding @Transactional to your method or class.

florin
  • 13,986
  • 6
  • 46
  • 47
rjsang
  • 1,757
  • 3
  • 16
  • 26
  • 1
    Its just a select query. Do i have to add this annotation to all DAO methods? – chedine Jul 20 '10 at 13:04
  • It can be on the DAO method or on the service method or on either of the classes, but it has to be somewhere in the stack if you're using a TransactionManager or you will get the exception you encountered because you are trying to run a query outside of a transaction. – rjsang Jul 20 '10 at 13:49
  • 1
    Possibly they are being run from a context where no TransactionManager has been defined? – rjsang Jul 20 '10 at 14:38
  • 1
    It can work without annotations as, by default, transaction demarcation is expected to be defined in configuration, unless the element is used (see http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html#tx-decl-explained) – jjmontes Jun 02 '11 at 10:21
  • I don't think adding a `@Transacional` to each method and/or class is the right solution here. – Bunnynut Sep 06 '19 at 15:00
4

I got around this problem by specifying the current_session_context_class in hibernate config to be "thread", as per the simple configuration shown in the hibernate configuration documentation.

But it recommends that its not safe for production usage.

Trying to add the following in your hibernate config should also help:

<property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>

Check out http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/architecture.html#architecture-current-session for more details.

Tony Rad
  • 2,479
  • 20
  • 32
pprabhu
  • 51
  • 1
  • 7
  • 1
    Just a warning to those who try this and are using Spring: I just got a different error when configuring 'current_session_context_class'. In several other questions this gave way to problems with transactions. Effectively this seems to remove spring transaction management. (see for example: http://stackoverflow.com/questions/4293098/how-to-integrate-spring-with-hibernate-session-and-transaction-management) – Yashima Jul 17 '12 at 15:31
  • I also had different behavior. I loaded an object from the database, made a change, then tried to persist it. In order for it to work I had to evict it from the session then save. – Zoidberg Oct 04 '13 at 16:00