Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

hibernate

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?

like image 891
chedine Avatar asked Jul 20 '10 12:07

chedine


People also ask

What is @transactional in hibernate?

Generally the @Transactional annotation is written at the service level. It is used to combine more than one writes on a database as a single atomic operation. When somebody call the method annotated with @Transactional all or none of the writes on the database is executed.

What is Current_session_context_class in hibernate?

hibernate. context. CurrentSessionContext ) and a new configuration parameter ( hibernate. current_session_context_class ) have been added to allow pluggability of the scope and context of defining current sessions. See the Javadocs for the org.

What is SessionFactory in hibernate with example?

The SessionFactory is a thread safe object and used by all the threads of an application. The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file.

Can you describe the different contextual sessions in hibernate?

For example: a single transaction could be such a context, hence if the Hibernate Session's lifecycle matches a life of this transaction, the Session could be called contextual, where a single transaction defines such a context . Sometimes this particular case is labeled as a session-per-request model.


2 Answers

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.

like image 199
rjsang Avatar answered Sep 30 '22 14:09

rjsang


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.

like image 33
pprabhu Avatar answered Sep 30 '22 13:09

pprabhu