我从未使用过hibernate插件,但我鼓励您采用 View中 的 Open Session 模式。您肯定要关闭会话。
解决此问题的最常用方法之一是在请求开始时创建会话,将其存储在本地线程中,然后在请求结束时关闭会话。这可以通过Struts拦截器或Servlet过滤器完成。基本上:
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过滤器。



