-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: ERROR: Hibernate session not bound to thread ...
PostPosted: Wed Jun 06, 2007 3:36 pm 
Newbie

Joined: Wed Jun 06, 2007 3:05 pm
Posts: 3
From a search of this forum this is a rather popular error :)

The error I'm getting is:

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

I'm using Hibernate 1.2.4.

Basically I get this error in a JUnit test when I'm actually going against a real (Sybase) database.

If I run the unit test against an in memory database (hsqldb), I don't get this error - all is good.

When the actual code is run in production (inside JBoss) against a real database (Sybase), there is no problem.

The only time I see this is with a JUnit test running against a real Sybase database.


The error in the JUnit test case occurs at the line.

Code:
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();



Here's my configuration:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="sybaseHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="properties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop>
            <prop key="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:jtds:sybase://somedomain.com:5000/SomeDBName</prop>
            <prop key="hibernate.connection.username">user</prop>
            <prop key="hibernate.connection.password">password</prop>
            <prop key="hibernate.connection.pool_size">1</prop>
            <prop key="hibernate.connection.autocommit">false</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            <prop key="hibernate.jdbc.fetch_size">512</prop>
            <prop key="hibernate.jdbc.batch_size">30</prop>
            <prop key="hibernate.hbm2ddl.auto">none</prop>
         </props>
      </property>
   </bean>
   
   <bean id="mappingResourcesList" class="org.springframework.beans.factory.config.ListFactoryBean">
      <property name="sourceList">
         <list>
            <value>com/somedomain/hibernate/DoSomething.hbm.xml</value>
         </list>
      </property>
   </bean>
   <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" lazy-init="false">
      <property name="mappingResources">
         <ref bean="mappingResourcesList"/>
      </property>
      <property name="hibernateProperties">
         <ref bean="sybaseHibernateProperties"/>
      </property>
   </bean>
   
   <bean id="myHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" lazy-init="true">
      <property name="sessionFactory" ref="mySessionFactory"/>
   </bean>
   
   <bean id="myDAOImpl" class="com.somedomain.MyDAO" lazy-init="true">
      <property name="hibernateTemplate" ref="myHibernateTemplate"/>
   </bean>
   
    <bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="true">
       <property name="sessionFactory" ref="mySessionFactory" />
    </bean>

    <bean id="myDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
       <property name="transactionManager" ref="myTransactionManager"/>
       <property name="target" ref="myDAOImpl"/>
       <property name="transactionAttributes">
          <props>
             <prop key="*">PROPAGATION_REQUIRED</prop>
          </props>
       </property>
    </bean>      
</beans>



So what am I doing wrong ?

I'm a hibernate newbie BTW in case you haven't figured it out.


Top
 Profile  
 
 Post subject: Re: Hibernate session not bound to thread ...
PostPosted: Thu Jun 07, 2007 12:42 am 
Newbie

Joined: Mon Dec 11, 2006 12:54 am
Posts: 14
Hi,

Try adding this property to your configuration(hibernate.cfg.xml), hopefully the error will go :-)

<property name="current_session_context_class">thread</property>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 7:15 pm 
Newbie

Joined: Wed Jun 06, 2007 3:05 pm
Posts: 3
I've tried that setting and it didn't work.

I still get that same exception "No hibernate session is bound ...".

I'm not going to give up on this !!

The problem with these problem is that it might be one little setting but it may take one days or weeks to figure out unless somebody knows and taps out the fix in 5 seconds.

Anyhow, i'm not going to give on this !!

There are a few clues.

One is that the problem does not happen with HSQLDB (in memory database).

It only happens while testing with JUnit against a Sybase database. The same piece of code works flawlessly when NOT running in a test case (live production code).

So, it's my conjecture that something about the JUnit test environment is screwing up the interaction - perhaps something to do with threading is my guess.

Here's what my test case looks like:


Code:
public class MyTest extends TestCase {
   SessionFactory sessionFactory;
   private MyDAO myDAO;
   private HibernateTemplate hibTemplate;
   
   public void setUp() throws Exception{
      // Obtain DAO implementation from someplace (spring or whatever)
      myDAO = getMyDAO();
         
     // Obtain the hibernate template from someplace (spring or whatever)
     hibTemplate = getMyDAOHibTemplate();
     
     sessionFactory = hibTemplate.getSessionFactory();
 
     Session session = SessionFactoryUtils.getSession(sessionFactory, true);
     TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
   }
   
   public void tearDown() throws Exception {
      Session session = sessionFactory.getCurrentSession();
      TransactionSynchronizationManager.unbindResource(sessionFactory);
      SessionFactoryUtils.releaseSession(session, sessionFactory);   
   }
   
   public void testSomething() throws Exception {
      // Code blows up here coz inside it has hibernateTemplate.getSessionFactory().getCurrentSession()
      myDAO.doSomething(); 
   }
   
}


Please help this newb :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 2:28 pm 
Newbie

Joined: Wed Jun 06, 2007 3:05 pm
Posts: 3
OK, i have more information to add:

I tried extending AbstractTransactionalSpringContextTests in my test case but I got a different problem.

I didn't see that infamous "No hibernate session is bound" error but no data was actually saved to the database even though the inserts completed without any problems.

Did the transaction not commit?

I'm not giving up !! :(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.