使用 Struts2 休眠 - 使用完整休眠插件或其他方法关闭会话? [英] Hiberate with Struts2 - Use Full Hibernate Plugin or another method to close Sessions?

查看:26
本文介绍了使用 Struts2 休眠 - 使用完整休眠插件或其他方法关闭会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Struts 2.2.1.1 和 Hibernate 3.6.2.Final.我还将 C3P0 用于在 Tomcat 7.0.11 上运行的连接池.

I'm using Struts 2.2.1.1 and Hibernate 3.6.2.Final. I'm also using C3P0 for my connection pool which is running on Tomcat 7.0.11.

我遇到的问题是我的 Hibernate 会话没有被关闭,并且我很快超过了hibernate.c3p0.max_size"属性中配置的最大打开连接数.

I'm having issues where my Hibernate Sessions are not being closed and I'm quickly exceeding the maximum number of open connections as configured in the "hibernate.c3p0.max_size" property.

我认为这是因为我的 Hibernate 会话已打开但从未关闭.我正在从存储在 ServletContext 中的 SessionFactory 打开 Sessions.我尝试在 Action 类的 finally{} 块中关闭会话,但这会引发 org.hibernate.LazyInitializationException 异常.

I think it is because my Hibernate Sessions are opened but never closed. I'm opening Sessions from a SessionFactory that is stored in the ServletContext. I tried closing the session in a finally{} block on my Action class but that throws org.hibernate.LazyInitializationException exceptions.

我做了一些研究,发现了 Full Hibernate Plugin 方法以及 Open Session in View 方法.

I did some research and I found the Full Hibernate Plugin approach and also the Open Session in View approach.

我假设这是一个常见问题,我想了解一下最常用的解决方案.

I'm assuming this is a common problem and I'd like to get a feel for the most commonly used solution.

我注意到的另一件事是 Full Hibernate 插件支持 Struts 2.0.9+ 到 2.1.6,但我使用的是 2.2.1.1.不确定这是否是个问题,或者该网站是否尚未更新以列出较新版本.

Another thing I noticed is the Full Hibernate Plugin supports Struts 2.0.9+ to 2.1.6 but I'm using 2.2.1.1. Not sure if this would be a problem or if the website just hasn't been updated to list the newer version.

非常感谢任何意见.

推荐答案

我从未使用过 hibernate 插件,但我建议你采用 Open Session in View 模式.你肯定想结束你的会话.

I've never used the hibernate plugin, but I would encourage you to adopt the Open Session in View pattern. You definitely want to be closing your sessions.

处理此问题的最常见方法之一是在请求开始时创建会话,将其存储在本地线程中,然后在请求结束时关闭它.这可以通过 Struts 拦截器或 servlet 过滤器来完成.基本上:

One of the most common ways to handle this is to create a session at the start of the request, store it in a thread local, and then close it at the end of the request. This can be done from a Struts interceptor or a servlet filter. Basically:

public class HibernateSessionInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(final ActionInvocation invocation) throws Exception {
        try {
            // create the session and place it in the ThreadLocal
            return invocation.invoke();
        } finally {
            // close the session and remove it from the ThreadLocal
        }
    }
}

如果您使用 Google Guice,则有一个基于 JPA 的持久性插件 (guice-persist).它使用相同的方法,但使用了 servlet 过滤器.

If you use Google Guice, there is a persistence plugin (guice-persist) that is based on JPA. It uses the same approach, but with a servlet filter.

这篇关于使用 Struts2 休眠 - 使用完整休眠插件或其他方法关闭会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆