`

怪异的 No Hibernate Session bound to Thread !异常

阅读更多

java .lang .IllegalStateException: No Hibernate Session bound to thread , and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java :350)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java :200)
at com.sea.user.dao.BaseDAO.getSession(BaseDAO.java :23)
at com.sea.user.dao.impl.UserDAO.saveUser(UserDAO.java :16)
at com.sea.user.service.impl.UserService.saveUser(UserService.java :14)
at com.sea.user.action.UserAction.register(UserAction.java :48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java :39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java :25)

====================

意思是说你没有事务,无法产生Session与当前线程绑定,这通常是getHibernateTemplate() getSession()产生的问题,does not allow creation of non-transactional one here 不允许创建非事务,看过一些别人的见解,是说这里的Session必须和事务关联才能产生,(然后绑定到线程,进而在事务处理过程中Session只是唯 一的,这是我想的,保证在一次操作中是事务性的,Session(即一个连接唯一 )才可能,)而用HibernateCallback()就不会出现问题,然而很多人可能直接在Service里想得到 Session.

==================

reference to link:

https://forums.hibernate.org/viewtopic.php?p=2400339&sid=981dbe2a38a5e3a8b031bdbb8edde389

http://www.iteye.com/problems/33765

============================

 Spring的 getHibernateTemplate().getSessionFactory().getCurrentSession()的意思是得到当前线程 绑定的session,而当前线程绑定的session是通过当前的事务产生的,如果没有配置事务的话,当前线程threadlocal中就不存在 session,这样就出现no session错误。

而execute的回调方法,看源码HibernateTemplate中写道

public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");

    Session session = getSession();
    boolean existingTransaction = (!isAlwaysUseNewSession() &&
        (!isAllowCreate() || SessionFactoryUtils.isSessionTransactional(session, getSessionFactory())));


其中getSession,代码如下

protected Session getSession() {
    if (isAlwaysUseNewSession()) {
        return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());
    }
    else if (isAllowCreate()) {
        return SessionFactoryUtils.getSession(
        getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
    }
    else {
        try {
            return getSessionFactory().getCurrentSession();
        }
        catch (HibernateException ex) {
            throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex);
        }
    }


        其中默认private boolean alwaysUseNewSession = false,所以代码会走到else if (isAllowCreate())
注 意这里:else if (isAllowCreate()),其中在HibernateTemplate类中默认private boolean allowCreate = true;其实通过函数名字就很清楚。意思说如果当前线程中的session不存在的话,是否允许创建,而默认是允许的,接下来是创建当前线程中的 session的代码,所以在没有事务的状态下,用execute回调方法,就不会出现上述问题。

分享到:
评论

相关推荐

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

    然而,在使用 Hibernate 进行数据库操作时,经常会遇到 "No Hibernate Session bound to thread" 的错误信息。本文将详细介绍该错误的解决方案。 错误原因 "No Hibernate Session bound to thread" 错误信息通常是...

    Hibernate-nosession

    然而,在某些特定场景下,我们可能并不需要频繁地打开和关闭Session,这时“Hibernate-nosession”就显得尤为重要。本文将深入探讨Hibernate-nosession的概念、应用场景以及如何在实际代码中实现。 首先,理解什么...

    OA系统整体设计及约定、搭建环境.rar_OA系统及配置_OA系统整体设计及约定、搭建环境_creation

    hibernate就会抛出: No Hibernate Session bound to thread, and configuration does not allow creation of one here}异常。 在实际的SSH web应用开发中,我们通常用spring来进行事务的管理。我们一般不会在dao层...

    JAVA错误文档.pdf

    4. Hibernate框架错误:文档还包含了“org.hibernate.HibernateException: No Hibernate Session bound to thread”等错误信息,这是Hibernate框架常见的异常,表明在当前线程上没有绑定Hibernate Session。...

    使用Hibernate一些常见错误解决办法

    当遇到`no current session bound to current context`错误时,通常是因为事务管理方式不正确。Hibernate提供了多种会话上下文配置,如`thread`、`jta`等。在这种情况下,推荐使用`thread`配置,确保每个线程都有...

Global site tag (gtag.js) - Google Analytics