简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM