ServletFilter中管理Hibernate Session
来源:互联网 发布:逆战刷枪软件辅助 编辑:程序博客网 时间:2024/04/29 10:01
web.xml
<filter>
<filter-name>HelloHibernateFilter</filter-name>
<filter-class>com.xanada.HelloHibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HelloHibernateFilter</filter-name>
<url-pattern>/*</url-pattern> //Filter对所有的请求都响应
</filter-mapping>
<filter-name>HelloHibernateFilter</filter-name>
<filter-class>com.xanada.HelloHibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HelloHibernateFilter</filter-name>
<url-pattern>/*</url-pattern> //Filter对所有的请求都响应
</filter-mapping>
HelloHibernateFilter.java
//init在整个servlet的生命周期中只做一次,在这里它初始化了SessionFactory,从而保证了SessionFactory在整个进程中只有一个实例
public void init(FilterConfig filterConfig)
throws ServletException ...{
try ...{
HibernateSessionFactory.init();
} catch (HibernateException e) ...{
e.printStackTrace();
}
}
//session是从DAO中得到的,但是做完一个operation后并不close,而是在整个request完成后在doFilter的finally里面close
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws ServletException, IOException ...{
try ...{
chain.doFilter(req, res);
} finally ...{
try ...{
HibernateSessionFactory.closeSession();
} catch (HibernateException e) ...{
e.printStackTrace();
}
}
}
public void init(FilterConfig filterConfig)
throws ServletException ...{
try ...{
HibernateSessionFactory.init();
} catch (HibernateException e) ...{
e.printStackTrace();
}
}
//session是从DAO中得到的,但是做完一个operation后并不close,而是在整个request完成后在doFilter的finally里面close
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws ServletException, IOException ...{
try ...{
chain.doFilter(req, res);
} finally ...{
try ...{
HibernateSessionFactory.closeSession();
} catch (HibernateException e) ...{
e.printStackTrace();
}
}
}
其实你若不愿用filter,完全可以把这两段代码放在相应的servlet.init()和servlet.execute()的finally里。
HibernateSessionFactory (可使用hibernate官方的HibernateUtils)
//sessionFactory是一个singleton
private static SessionFactory sessionFactory;
//init在filter.init()中被调用
public static void init() throws HibernateException
//currentSession被DAO中被调用
public static Session currentSession() throws HibernateException
//closeSession在filter.doFilter()的finally中被调用
public static void closeSession() throws HibernateException
private static SessionFactory sessionFactory;
//init在filter.init()中被调用
public static void init() throws HibernateException
//currentSession被DAO中被调用
public static Session currentSession() throws HibernateException
//closeSession在filter.doFilter()的finally中被调用
public static void closeSession() throws HibernateException
CatDAOImpl
//注意:这里的session用完之后并不close,而只flush一下
public void delCat(String strCatId) ...{
try ...{
Session s = HibernateSessionFactory.currentSession();
Object cat = s.load(Cat.class, strCatId);
s.delete(cat);
s.flush();
} catch (HibernateException e) ...{
e.printStackTrace();
}
}
public void delCat(String strCatId) ...{
try ...{
Session s = HibernateSessionFactory.currentSession();
Object cat = s.load(Cat.class, strCatId);
s.delete(cat);
s.flush();
} catch (HibernateException e) ...{
e.printStackTrace();
}
}
ServletFilter中管理Session
//对Cat的CRUD的封装
public interface CatDAO {
public void creatCat(Cat cat);
public List readCats();
public void updateCat(Cat cat);
public void delCat(String strCatId);
}
public interface CatDAO {
public void creatCat(Cat cat);
public List readCats();
public void updateCat(Cat cat);
public void delCat(String strCatId);
}
CatDAO
- ServletFilter中管理Hibernate Session
- Hibernate中对Session管理
- Hibernate中Session的管理
- Cognos中加入ServletFilter层
- Hibernate中对Session管理[转载]
- 【Hibernate】项目中session的管理方式
- Hibernate-struts 项目中Session的管理
- Hibernate中的Session管理
- Hibernate中的Session管理
- Hibernate 管理session
- hibernate session管理
- Hibernate session管理
- Hibernate 的 session 管理
- Hibernate(十三)Session管理
- Hibernate管理Session
- spring 管理hibernate session
- Hibernate 管理 Session
- Hibernate的session管理
- 使用MySql Connector/Net(后续)
- Java中关于包的一个问题
- 2007-11-21
- java 的 import
- Windows2003下搭建VPN服务器初体验
- ServletFilter中管理Hibernate Session
- 开博
- Ubuntu Server 安装 Desktop
- 有信念是好事,也是坏事
- 为Struts 2.0做好准备2007-05-20 16:57 |
- 山东本科硕士年薪差距3700元
- firefox下的eval与ie下有什么不同
- cewolf学习笔记
- 如何循序渐进向DotNet架构师发展