用Struts2+Hibernate开发Web程序
发现网站老是一动不动,在DAO中用 Statistics stats = HibernateUtil.getSessionFactory().getStatistics();
查看了统计结果,发现打开50个Session只有20个关闭了.
看结构也没有什么问题,我使用两个ThreadLocal 存放Session 和Transaction,再用一个Filter去关闭,各个配置表面上也是没有问题,可是做测试的时候才发现关闭Session的Filter根本就是没有运行,或是有时运行,有时不运行,
仔细想了想,调整了一下sessionclosefilter和Struts2的filter的顺序如下,终于可以关闭了
<filter>
<filter-name>HibernateSessionCloseFiler</filter-name>
<filter-class>com.hitecin.struts2.filter.HibernateSessionClose</filter-class>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>HibernateSessionCloseFiler</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>