遇到org.hibernate.SessionException问题:会话已关闭!在Hibernate中

9 java orm session hibernate

我已经对此进行了相当多的研究而没有运气,但所有答案都倾向于指向配置文件中的会话上下文设置.奇怪的是,我第一次点击页面时获得了会话连接(因此,成功的结果集),但是当我重新加载时,我得到以下异常:org.hibernate.SessionException:会话关闭!

以下是我的配置设置,不是与DB连接字符串相关的:

<property name="hibernate.show_sql">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>        
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_minimal_puts">false</property>
Run Code Online (Sandbox Code Playgroud)

这是我做出的一个调用的例子,它产生了我上面描述的情况.

public T get(int id) {
    session.beginTransaction();
    T type;
    try {
        type = getTypeClass().cast(session.get(getTypeClass(), id));
    } catch (ClassCastException classCastException) {
        throw new ClassCastException(classCastException.getMessage());
    }
    session.getTransaction().commit();
    return type;
}
Run Code Online (Sandbox Code Playgroud)

会话变量引用是包含当前会话的静态字段.所有会话连接详细信息都是教科书参考手册.例如,这是我的Hibernate会话实用程序:

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactoryUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 8

当您获得会话时sessionFactory.getCurrentSession(),会在提交事务时自动刷新并关闭会话(有关详细信息,请参阅会话和事务).所以,在这里我怀疑1.你得到会话一次(这可以解释为什么第一个调用有效以及为什么后续调用失败)这是错误的2.你似乎使用了每个操作会话反模式更糟糕的是.

在Web应用程序中,您应该使用每个请求会话策略,这意味着" 单个会话和单个数据库事务实现特定请求事件的处理 ".再次,请参阅 会话和交易文档.

如果要从数据访问代码中删除事务划分,则可以使用拦截器在每个请求开始时启动数据库事务,并在请求结束时提交它.有一个看看在视图打开会话此模式(有样品展示DAO的好处)的实现.