Hiberate with Struts2 - Use Full Hibernate Plugin or another method to close Sessions?

前端 未结 1 1478
广开言路
广开言路 2020-12-22 06:04

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.

I\'m having issues where my Hibe

1条回答
  •  眼角桃花
    2020-12-22 06:48

    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.

    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
            }
        }
    }
    

    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.

    0 讨论(0)
提交回复
热议问题