chedine
chedine

Reputation: 2374

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

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?

Upvotes: 26

Views: 48925

Answers (2)

pprabhu
pprabhu

Reputation: 51

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.

Upvotes: 4

rjsang
rjsang

Reputation: 1757

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.

Upvotes: 34

Related Questions