整体框架层:表示层, 业务层,持久层和数据库层
泛型DAO模式Hibernate实现
OpenSessionInView模式
在hibernate中使用load方法时,并未把数据真正获取时就关闭了session,当我们真正想获取数据时会迫使load加载数据,而此时session已关闭,所以就会出现异常。 比较典型的是在MVC模式中,我们在M层调用持久层获取数据时(持久层用的是load方法加载数据),当这一调用结束时,session随之关闭,而我们希望在V层使用这些数据,这时才会迫使load加载数据,我们就希望这时的session是open着得,这就是所谓的Open Session In view 。 我们可以filter来达到此目的。下面的代码初步解决了此问题:
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain){
try{
Session session = …SessionFactory.getCurrentSeesion(); //得到session对象
tx = session.beginTransaction(); //开启事务
chain.doFliter(request,response); //传递给下一个
tx.commit(); //提交事务
}catch (Exception e){
//出现异常,回滚事务
}
}
简析:当请求到达时,会首先被此拦截器拦截,当数据经过获取并在V层显示完毕后,回到此Filter内部,此时提交事务–>关闭session。
所以解决办法如下所示:
1:把session实例绑定在当前线程中
<!-- 把session附加在当前线程上 -->
<property name="hibernate.current_session_context_class">thread</property>
2:用servlet过滤器在请求到达之前打开session,在响应返回前关闭session,这个过滤器代码如下:
/**
* ClassName: OpenSessionInViewFilter.java
* created on 3:59:53 PM
* Copyrights 2008 qjyong All rights reserved.
* EMail: qjyong@gmail.com
*/
package com.qiujy.web.common;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.qiujy.common.HibernateUtil;
/**
* OSIV模式的Filter
* @author qiujy
*/
public class OpenSessionInViewFilter implements Filter {
private SessionFactory sf;
private Log log;
public void destroy() {}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
log.debug("请求到达OSIV的filter了。。。");
Session session = null;
try{
log.debug("启动事务");
//请求到达时,打开Session并启动事务
session = sf.getCurrentSession();
session.getTransaction().begin();
//执行请求处理链
arg2.doFilter(arg0, arg1);
//返回响应时,提交事务,关闭session
session.getTransaction().commit();
log.debug("提交事务");
}catch(RuntimeException re){
log.error("回滚事务", re);
//执行过程,如果有异常,则必须回滚事务
if(session.getTransaction().isActive()){
session.getTransaction().rollback();
}
}
}
public void init(FilterConfig arg0) throws ServletException {
sf = HibernateUtil.getSessionFactory();
log = LogFactory.getLog(this.getClass());
}
}
3:在web应用中的web.xml中配置这个过滤器所要过滤的url代码,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- osiv模式的过滤器 -->
<filter>
<filter-name>osiv</filter-name>
<filter-class>com.qiujy.web.common.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>osiv</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>