Results 1 to 7 of 7

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

  1. #1
    Join Date
    Nov 2005
    Location
    US of A
    Posts
    43

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  3. #3
    Join Date
    Nov 2005
    Location
    US of A
    Posts
    43

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  5. #5
    Join Date
    Nov 2005
    Location
    US of A
    Posts
    43

    Default

    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

  6. #6
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote 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

  7. #7
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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).
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •