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

  • org.hibernate.HibernateException: No Hibernate Session bound to thread

    Me using spring-framework-1.2.1 + hibernate-3.0

    I am trying to perform a login using spring + hibernate and get this exception; I did find a couple of threads with this problem , but none matched

    SEVERE: Could not complete request
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.hibernate3.LocalSessionFac toryBean$TransactionAwareInvocationHandler.invoke( LocalSessionFactoryBean.java:948)
    at $Proxy0.getCurrentSession(Unknown Source)


    The code is use to perform the login is

    Criterion idCriterion = Expression.eq("userID",new Integer(userID));
    Criterion pwdCriterion = Expression.eq("password",password);
    Criteria loginCriteria = this.sessionFactory.getCurrentSession().createCrit eria(User.class);
    loginCriteria.add(idCriterion);
    loginCriteria.add(pwdCriterion);
    user = (User)loginCriteria.uniqueResult();



    My DAO beans are defined as


    <beans>

    <!-- Data source bean -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value></property>
    <property name="url">
    <value>jdbc:mysql://localhost:3306/esimsdb</value></property>
    <property name="username"><value>esims</value></property>
    <property name="password"><value>esims</value></property>
    </bean>

    <!-- Spring's wrapper around Hibernate-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>

    <property name="mappingResources">
    <list>
    <value>org/esims/auth/model/User.hbm.xml</value>
    </list>
    </property>

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
    </props>
    </property>
    </bean>


    <!-- The bean that does the authentication using hibernate -->
    <bean id="authDAOBean" class="org.esims.auth.dao.HibernateAuthDAO">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

    </beans>

    Thnx

  • #2
    You need to provide a transactional scope. This can be done either using Spring's declarative transaction model (recommended) or by using TransactionTemplate programmatically.
    See the reference for details.

    Regards,
    Andreas

    Comment


    • #3
      No luck, I added this to the dao.xml

      <bean id="txManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
      </bean>

      but still get

      org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

      Comment


      • #4
        It is not enough to just declare a transaction manager. You also have to specify the methods which are expected to be transactional. This can be done by adding appropriate TransactionInterceptors. Have a look at the link I posted. There should be examples about how to set up transactional methods properly.

        Regards,
        Andreas

        Comment


        • #5
          Thanks Andreas, I changed my approach to use hibernate callback

          Hence, I changed the code to

          HibernateTemplate ht = new HibernateTemplate(this.sessionFactory);
          user = (User) ht.execute(
          new HibernateCallback() {

          public Object doInHibernate(Session session) throws HibernateException {
          Query query = session.createQuery("from org.esims.auth.model.User as user where user.userid=? and user.password=?");
          query.setInteger(0, userID);
          query.setString(1, password);
          return query.list();
          }
          }
          );


          now it works

          Reason. I guess is

          A further disadvantage of that DAO style is that Hibernate's getCurrentSession() feature just works within
          JTA transactions. It does not work with any other transaction strategy out-of-the-box, in particular not with
          local Hibernate transactions.
          :: Spring reference

          Comment


          • #6
            Originally posted by jvictor

            A further disadvantage of that DAO style is that Hibernate's getCurrentSession() feature just works within
            JTA transactions. It does not work with any other transaction strategy out-of-the-box, in particular not with
            local Hibernate transactions.
            :: Spring reference
            Not sure, but I think I read somewhere that this has been changed with hibernate 3.1.1 (or was it the upcoming 3.2?)

            Anyway, I, personally, had never the need to use getCurrentSession() explicitly as I can always use HibernateTemplate which will provide me with the current session, passed into my HibernateCallbacks or used implicitly. And this also works with local transactions.

            Regards,
            Andreas

            Comment


            • #7
              Not sure, but I think I read somewhere that this has been changed with hibernate 3.1.1 (or was it the upcoming 3.2?)
              This was changed in 3.1 after ThreadLocalSessionContext has been introduced. You should be fine if you are using 3.1.2+ since they fixed some subtle bugs (related also to sessionContext).

              Comment

              Working...
              X