[求助前辈高人]使用Filter实现线程生命周期内hibernate Session的管理

Session采用Threadlocal管理
这个过滤方法是http://hibernate.org/43.html看来的


public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
try {

HibernateSessionFactory.getCurrentSession().beginTransaction();

chain.doFilter(request, response);

HibernateSessionFactory.getCurrentSession().getTransaction().commit();

} catch (Throwable ex) {
HibernateSessionFactory.getCurrentSession().getTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}

我也用了Strust框架
以下是我的web.xml配置,过滤名为action的servlet


<filter>
<filter-name>Hibernate Session Request Filter</filter-name>
<filter-class>com.llw.filter.HibernateSessionRequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Hibernate Session Request Filter</filter-name>
<servlet-name> action </servlet-name>
</filter-mapping>

运行时,每个请求都能进行一次过滤,打开事务处理,提交,再关闭session
可是,我有的请求只是简单的页面跳转而已,它也过滤
这样无论什么请求都打开一次session,会不会浪费性能,或者发生其他什么问题
我在好多地方搜索过,可是实在不知道这个问题用什么关键字搜,怎么搜都没找到一点相关的
各为老大有什么办法能解决?

你这样从过滤器上控制事务不好,原因如下
1.每次都会打开关闭事务,浪费资源是肯定的
2.这样不能细粒度的控制事务,这样大的事务的情况下资源冲突机率会增加

解决方案
1.在用的地方使用事务
2.可以用spring的申明事务