Announcement Announcement Module
Collapse
No announcement yet.
No Hibernate Session bound to thread with hibernate3 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No Hibernate Session bound to thread with hibernate3

    Hellu,

    I just changed my application such that i uses hibernate 3.0 and Springframework 1.2.5.
    I only get the exeption that my hibernate session isn't bound to a thread when I use it in the following way:
    I have a Service layer that talk to DAO's.
    In the DAO, I have:
    Code:
        private SessionFactory sessionFactory;
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    Just like explained in the new Springframework manual, and in the method:

    Code:
                    this.sessionFactory.getCurrentSession();
    before using the session to look up something in the db. This is how I understood it from the manual.
    When I call this method above, I get this hiberate exception, telling me that no session is bound.
    Just before I get the exception i get the following logging:
    Code:
    DEBUG org.springframework.orm.hibernate3.SessionFactoryUtils - Opening Hibernate Session
    DEBUG org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session
    I then noticed, though some debubbing, that I do have a NEW session, but that the following method (in SessionFactoryUtils) returns false:
    Code:
                      isSessionTransactional(session, sessionFactory)
    This method will look if I have a current session bound to my thread.
    So that means that I do have a NEW session but no current session and so notransaction. Is this correct? and Why not ?


    Below you find the important parts from the beans.xml file.
    Please some help.
    I noticed that the getCurrentSession() method is not allowed to create a new session and it looks up the session bound to the current transaction. So basically the current transaction must first already have created a session. But where and how??...do I make sure this happens..??... I would suppose throught the correct transaction configuration in the beans.xml below... but appearantly... it's enough... or it contains a mistake...

    Cheers,
    Ed Bras

    Code:
    <!-- The hibernate session factory -->
       <bean id="HibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="mappingResources">
               <list>
                   <value>MrMain.hbm.xml</value>
                   <value>MrMainProperty.hbm.xml</value>
                   <value>MrProperty.hbm.xml</value>
                   <value>MrPropertyKey.hbm.xml</value>
                   <value>MrRelated.hbm.xml</value>
                   <value>MrRelatedProperty.hbm.xml</value>
                   <value>MrDbQueries.hbm.xml</value>
               </list>
           </property>
           <property name="hibernateProperties">
               <props>
                   <prop key="hibernate.dialect">
                   		org.hibernate.dialect.MySQLDialect
                   	</prop>
    				<prop key="hibernate.connection.pool_size">
    					3
    				</prop>
    				<prop key="hibernate.show_sql">
    					true
    				</prop>								
    				<prop key="hibernate.connection.isolation">
    					2
    				</prop>								
               </props>
           </property>
           <property name="dataSource">
               <ref bean="DataSource"/>
           </property>
       </bean>
    
    	<!-- The main dao implementation that does the real work -->
    	<bean id="MainDao" class="org.ited.mr.persist.dao.impl.BasicMainDao">
    		<property name="sessionFactory">
    			<ref local="HibernateSessionFactory"/>
    		</property>
            <property name="mainCls">
                <ref bean="MrMain"/>
            </property>
            <property name="persistUtility">
                <ref bean="PersistUtility"/>
            </property>
    	</bean>
    
    
    	<!-- The backend service -->
        <bean id="Service" 
        	class="org.ited.mr.service.impl.BasicService">
            <property name="mainDao">
                <ref bean="MainDao"/>
            </property>
            <property name="relatedDao">
                <ref bean="RelatedDao"/>
            </property>
            <property name="dtoConverter">
                <ref bean="DtoConverter"/>
            </property>
            <property name="ctxUtils">
                <ref bean="ContextUtility"/>
            </property>
            <property name="mainCls">
                <ref bean="Main"/>
            </property>
            <property name="relatedCls">
                <ref bean="Related"/>
            </property>
        </bean>
    
    	<!-- Transaction demarcation of the backend service -->
       <bean id="BackendServiceProxy" 
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref bean="TransactionManager"/>
            </property>
            <property name="target">
                <ref bean="Service"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>

  • #2
    Is the SessionFactory in:
    private SessionFactory sessionFactory;
    of type
    Code:
    org.hibernate.SessionFactory
    instead of
    Code:
    net.sf.hibernate.SessionFactory

    Comment


    • #3
      Thanks for the quick response.

      The answer: yes the sessionfactory if of type org.hibernate.SessionFactory.

      Appearantly I don't have a transaction when entering my DAO, which should be the case.

      Comment


      • #4
        Hellu there,

        I got it working with a Interceptor.
        But I still think there is something wrong, as it should also work as listed below.
        So I hope someone can have a look at it, as maybe it's a little bug, as I got it working before!

        What I have no, is the configuration like listed in the manual with an interceptor:

        So the code is the same, but the beans.xml contains the following.
        Note that the transaction manager and session factory beans are unchanged. I now talk to the service proxy bean in my code.

        Code:
          <bean id="TransactionInterceptor"
              class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager">
        		<ref bean="TransactionManager"/>
            </property>
            <property name="transactionAttributeSource">
              <value>
                com.ited.mr.service.intf.IService.*=PROPAGATION_REQUIRED
              </value>
            </property>
          </bean>
        
          <bean id="ServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces">
              <value>com.ited.mr.service.intf.IService</value>
            </property>
            <property name="interceptorNames">
              <list>
                <value>TransactionInterceptor</value>
                <value>Service</value>
              </list>
            </property>
          </bean>
        Cheers,
        Ed

        Comment


        • #5
          I'm also getting the same error. Is this confirmed to be a bug or was there a configuration problem?

          Comment


          • #6
            I recently ran into the exact same sort of error, and beat my head against the wall trying to figure out what the problem was.

            In my case, I was using the `OpenSessionInViewFilter` configured in the `web.xml` file, along with the Spring Security configuration there as well.

            Since both Spring Security and `OpenSessionInViewFilter` are filters, the order in which they are applied depends on the order they appear in the `web.xml` file.

            In my case, I initially had the `OpenSessionInViewFilter` below the Spring Security filter. Once I swapped their positions (ie. put the `OpenSessionInViewFilter` above the Spring Security one), everything worked just fine!

            Comment

            Working...
            X