- 浏览: 118337 次
- 性别:
- 来自: 广东.佛山.南海
文章分类
最新评论
-
softor:
刚测试了一下,博主的第一种复制到剪贴板的方法在Chrome下不 ...
JS操作剪贴板 -
what866:
哥们太给力了。顶!!!
使用filter解决中文乱码问题 -
tinyxinxin:
对一些概念解释得相当清晰明了,灰常不错
Hibernate上手指南 -
java_4_ever:
ahcen001 写道如果不用这个方式,还有其他的解决lazy ...
OpenSessionInViewFilter配置说明 -
king520:
...
JXL操作
OpenSessionInView
Hibernate的Lazy初始化1:n关系时,你必须保证是在同一个Session内部使用这个关系集合,不然Hiernate将抛出例外。
另外,你不愿意你的DAO测试代码每次都打开关系Session,因此,我们一般会采用OpenSessionInView模式。
OpenSessionInViewFilter解决Web应用程序的问题
如果程序是在正常的Web程序中运行,那么Spring的OpenSessionInViewFilter能够解决问题,它:
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
SessionFactory sessionFactory = lookupSessionFactory();
logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
Session session = getSession(sessionFactory);
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(session));
try {
filterChain.doFilter(request, response);
}
finally {
TransactionSynchronizationManager.unbindResource(sessionFactory);
logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
closeSession(session, sessionFactory);
}
}
可以看到,这个Filter在request开始之前,把sessionFactory绑定到TransactionSynchronizationManager,和这个SessionHolder相关。这个意味着所有request执行过程中将使用这个session。而在请求结束后,将和这个sessionFactory对应的session解绑,并且关闭Session。
为什么绑定以后,就可以防止每次不会新开一个Session呢?看看HibernateDaoSupport的情况:
public final void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
protected final HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
我们的DAO将使用这个template进行操作:
public abstract class BaseHibernateObjectDao
extends HibernateDaoSupport
implements BaseObjectDao {
protected BaseEntityObject getByClassId(final long id) {
BaseEntityObject obj =
(BaseEntityObject) getHibernateTemplate()
.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
return session.get(getPersistentClass(),
new Long(id));
}
});
return obj;
}
public void save(BaseEntityObject entity) {
getHibernateTemplate().saveOrUpdate(entity);
}
public void remove(BaseEntityObject entity) {
try {
getHibernateTemplate().delete(entity);
} catch (Exception e) {
throw new FlexEnterpriseDataAccessException(e);
}
}
public void refresh(final BaseEntityObject entity) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
session.refresh(entity);
return null;
}
});
}
public void replicate(final Object entity) {
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
session.replicate(entity,
ReplicationMode.OVERWRITE);
return null;
}
});
}
而HibernateTemplate试图每次在execute之前去获得Session,执行完就力争关闭Session
public Object execute(HibernateCallback action) throws DataAccessException {
Session session = (!this.allowCreate ?
SessionFactoryUtils.getSession(getSessionFactory(),
false) :
SessionFactoryUtils.getSession(getSessionFactory(),
getEntityInterceptor(),
getJdbcExceptionTranslator()));
boolean existingTransaction =
TransactionSynchronizationManager.hasResource(getSessionFactory());
if (!existingTransaction && getFlushMode() == FLUSH_NEVER) {
session.setFlushMode(FlushMode.NEVER);
}
try {
Object result = action.doInHibernate(session);
flushIfNecessary(session, existingTransaction);
return result;
}
catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}
catch (SQLException ex) {
throw convertJdbcAccessException(ex);
}
catch (RuntimeException ex) {
// callback code threw application exception
throw ex;
}
finally {
SessionFactoryUtils.closeSessionIfNecessary(
session, getSessionFactory());
}
}
而这个SessionFactoryUtils能否得到当前的session以及closeSessionIfNecessary是否真正关闭session,端取决于这个session是否用sessionHolder和这个sessionFactory在我们最开始提到的TransactionSynchronizationManager绑定。
public static void closeSessionIfNecessary(Session session,
SessionFactory sessionFactory)
throws CleanupFailureDataAccessException {
if (session == null ||
TransactionSynchronizationManager.hasResource(sessionFactory)) {
return;
}
logger.debug("Closing Hibernate session");
try {
session.close();
}
catch (JDBCException ex) {
// SQLException underneath
throw new CleanupFailureDataAccessException(
"Cannot close Hibernate session", ex.getSQLException());
}
catch (HibernateException ex) {
throw new CleanupFailureDataAccessException(
"Cannot close Hibernate session", ex);
}
}
HibernateInterceptor和OpenSessionInViewInterceptor的问题
使用同样的方法,这两个Interceptor可以用来解决问题。但是关键的不同之处在于,它们的力度只能定义在DAO或业务方法上,而不是在我们的Test方法上,除非我们把它们应用到TestCase的方法上,但你不大可能为TestCase去定义一个接口,然后把Interceptor应用到这个接口的某些方法上。直接使用HibernateTransactionManager也是一样的。因此,如果我们有这样的测试:
Category parentCategory = new Category ();
parentCategory.setName("parent");
dao.save(parentCategory);
Category childCategory = new Category();
childCategory.setName("child");
parentCategory.addChild(childCategory);
dao.save(childCategory);
Category savedParent = dao.getCategory("parent");
Category savedChild = (Category ) savedParent.getChildren().get(0);
assertEquals(savedChild, childCategory);
将意味着两件事情:
• 每次DAO执行都会启动一个session和关闭一个session
• 如果我们定义了一个lazy的关系,那么最后的Category savedChild = (Category ) savedParent.getChildren().get(0);将会让hibernate报错。
解决方案
一种方法是对TestCase应用Interceptor或者TransactionManager,但这个恐怕会造成很多麻烦。除非是使用增强方式的AOP.我前期采用这种方法(Aspectwerkz),在Eclipse里面也跑得含好。
另一种方法是在TestCase的setup和teardown里面实现和Filter完全一样的处理,其他的TestCase都从这个TestCase继承,这种方法是我目前所使用的。
posted on 2004-11-18 15:11 逃离开发 阅读(1155) 评论(1) 编辑 收藏 收藏至365Key
评论
# re: 关于OpenSessionInViewFilter
转自:Karl Baum's Weblog
Karl Baum's Weblog
All | General | Java
Thursday July 08, 2004
Lazy Initialization and the DAO pattern with Hibernate and Spring
Hibernate and Lazy Initialization
Hibernate object relational mapping offers both lazy and non-lazy modes of object initialization. Non-lazy initialization retrieves an object and all of its related objects at load time. This can result in hundreds if not thousands of select statements when retrieving one entity. The problem is compounded when bi-directional relationships are used, often causing entire databases to be loaded during the initial request. Of course one could tediously examine each object relationship and manually remove those most costly, but in the end, we may be losing the ease of use benefit sought in using the ORM tool.
The obvious solution is to employ the lazy loading mechanism provided by hibernate. This initialization strategy only loads an object's one-to-many and many-to-many relationships when these fields are accessed. The scenario is practically transparent to the developer and a minimum amount of database requests are made, resulting in major performance gains. One drawback to this technique is that lazy loading requires the Hibernate session to remain open while the data object is in use. This causes a major problem when trying to abstract the persistence layer via the Data Access Object pattern. In order to fully abstract the persistence mechanism, all database logic, including opening and closing sessions, must not be performed in the application layer. Most often, this logic is concealed behind the DAO implementation classes which implement interface stubs. The quick and dirty solution is to forget the DAO pattern and include database connection logic in the application layer. This works for small applications but in large systems this can prove to be a major design flaw, hindering application extensibility.
Being Lazy in the Web Layer
Fortunately for us, the Spring Framework has developed an out of box web solution for using the DAO pattern in combination with Hibernate lazy loading. For anyone not familiar with using the Spring Framework in combination with Hibernate, I will not go into the details here, but I encourage you to read Hibernate Data Access with the Spring Framework. In the case of a web application, Spring comes with both the OpenSessionInViewFilter and the OpenSessionInViewInterceptor. One can use either one interchangeably as both serve the same function. The only difference between the two is the interceptor runs within the Spring container and is configured within the web application context while the Filter runs in front of Spring and is configured within the web.xml. Regardless of which one is used, they both open the hibernate session during the request binding this session to the current thread. Once bound to the thread, the open hibernate session can transparently be used within the DAO implementation classes. The session will remain open for the view allowing lazy access the database value objects. Once the view logic is complete, the hibernate session is closed either in the Filter doFilter method or the Interceptor postHandle method. Below is an example of the configuration of each component:
Interceptor Configuration
<beans>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor"/>
</list>
</property>
<property name="mappings">
...
</bean>
...
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
</beans>
Filter Configuration
<web-app>
...
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate.support.OpenSessionInViewFilter
</filter-class>
</filter>
...
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.spring</url-pattern>
</filter-mapping>
...
</web-app>
Implementing the Hibernate DAO's to use the open session is simple. In fact, if you are already using the Spring Framework to implement your Hibernate DAO's, most likely you will not have to change a thing. The DAO's must access Hibernate through the convenient HibernateTemplate utility, which makes database access a piece of cake. Below is an example DAO.
Example DAO
public class HibernateProductDAO extends HibernateDaoSupport implements ProductDAO {
public Product getProduct(Integer productId) {
return (Product)getHibernateTemplate().load(Product.class, productId);
}
public Integer saveProduct(Product product) {
return (Integer) getHibernateTemplate().save(product);
}
public void updateProduct(Product product) {
getHibernateTemplate().update(product);
}
}
Being Lazy in the Business Layer
Even outside the view, the Spring Framework makes it easy to use lazy load initialization, through the AOP interceptor HibernateInterceptor. The hibernate interceptor transparently intercepts calls to any business object configured in the Spring application context, opening a hibernate session before the call, and closing the session afterward. Let's run through a quick example. Suppose we have an interface BusinessObject:
public interface BusinessObject {
public void doSomethingThatInvolvesDaos();
}
The class BusinessObjectImpl implements BusinessObject:
public class BusinessObjectImpl implements BusinessObject {
public void doSomethingThatInvolvesDaos() {
// lots of logic that calls
// DAO classes Which access
// data objects lazily
}
}
Through some configurations in the Spring application context, we can instruct the HibernateInterceptor to intercept calls to the BusinessObjectImpl allowing it's methods to lazily access data objects. Take a look at the fragment below:
<beans>
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="businessObjectTarget" class="com.acompany.BusinessObjectImpl">
<property name="someDAO"><ref bean="someDAO"/></property>
</bean>
<bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target"><ref bean="businessObjectTarget"/></property>
<property name="proxyInterfaces">
<value>com.acompany.BusinessObject</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>
</beans>
When the businessObject bean is referenced, the HibernateInterceptor opens a hibernate session and passes the call onto the BusinessObjectImpl. When the BusinessObjectImpl has finished executing, the HibernateInterceptor transparently closes the session. The application code has no knowledge of any persistence logic, yet it is still able to lazily access data objects.
Being Lazy in your Unit Tests
Last but not least, we'll need the ability to test our lazy application from J-Unit. This is easily done by overriding the setUp and tearDown methods of the TestCase class. I prefer to keep this code in a convenient abstract TestCase class for all of my tests to extend.
public abstract class MyLazyTestCase extends TestCase {
private SessionFactory sessionFactory;
private Session session;
public void setUp() throws Exception {
super.setUp();
SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
Session s = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
}
protected Object getBean(String beanName) {
//Code to get objects from Spring application context
}
public void tearDown() throws Exception {
super.tearDown();
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory);
}
}
相关推荐
本文将详细地介绍如何从零开始搭建一个完整的SSH框架,并为初学者提供一套可参照的实践指南。 #### 二、准备工作 1. **安装Eclipse或IntelliJ IDEA**:作为开发环境的基础工具。 2. **安装JDK**:确保系统已经正确...
因此,以下将围绕这部分内容进行深入解析,详细介绍如何在Web项目中整合这三大框架。 ### Struts2、Spring、Hibernate集成 #### 1. 创建Web Project项目 首先,在开发环境中创建一个新的Web项目。这通常在IDE(如...
本文将详细介绍一个基于Struts2、Spring、Hibernate以及Hibernate Generic DAO构建的基础框架。该框架使用全注解的方式进行配置,并且提供了清晰的包结构和文档,便于理解和使用。 #### 技术栈与版本 - **Struts2*...
通过对上述六个关键类的详细介绍,我们不仅了解了它们的基本定义和作用,还深入探讨了它们在SSH框架整合过程中的具体应用场景。这些类对于搭建稳定可靠的SSH框架至关重要,希望本文能为读者提供有价值的参考。在实际...
本文将详细介绍如何在 Struts2+Spring+Hibernate 框架中搭建 Action 的单元测试环境。 首先,需要了解为什么需要对 Action 进行单元测试。在项目中,如果没有 Service 层,而是在 Action 中直接操作 Dao 层的函数,...
下面将详细介绍SSH整合的关键点,以及如何进行环境配置和搭建。 首先,SSH整合涉及到的版本选择对项目的稳定性和兼容性至关重要。在描述中提到的配置中,使用了Struts2.0.14、Spring2.5和Hibernate3.2。这些版本的...
本文将详细介绍如何在项目中集成这三大框架,并实现反向表结构。 #### 二、为项目添加Struts2支持 1. **依赖引入**:首先,确保项目中包含了Struts2的核心库以及其他必要的扩展库。对于Struts2.1版本,核心库通常...
本文将基于提供的部分内容,详细介绍如何搭建Struts2+Spring+Hibernate框架。 #### 一、Struts2、Spring与Hibernate简介 - **Struts2**:是一款基于MVC设计模式的Web应用框架,它提供了一套完整的机制来帮助开发者...
本文将详细介绍如何在MyEclipse 7.0环境下,基于Java EE 1.4标准和Tomcat 6.0.18服务器,实现Struts2 2.0.14版本、Spring 2.5系列以及Hibernate 3.2系列的整合,并探讨具体的配置方法。 #### 二、配置文件详解 **1...
接下来,我们逐个介绍SSH框架的导入步骤: 1. **Spring框架**:导入Spring的核心包、AOP包、ORM映射包以及Web支持包。Spring作为核心框架,提供依赖注入(DI)和面向切面编程(AOP),管理整个应用的组件。 2. **...
本文将详细介绍这三个框架如何进行整合集成。 1. **Spring框架**:Spring是核心的IoC(Inversion of Control,控制反转)和AOP(Aspect-Oriented Programming,面向切面编程)容器,用于管理对象的生命周期和依赖...
本文旨在详细介绍如何将Apache Shiro整合到基于SpringMVC和JPA(hibernate)的应用程序中,为系统提供安全控制机制。通过本教程,您将了解关键配置文件的作用及其配置细节,包括`web.xml`、`applicationContext.xml`...
本小册子将详细介绍如何进行SSH框架的整合,并指出在实际开发过程中需要注意的关键事项。 #### 二、准备工作与整体架构 ##### 1. 前期准备 - **环境配置**:首先确保已安装Java开发环境,JDK版本至少为1.8。 - **...
本笔记将详细介绍 Spring 的核心概念、配置和应用。 **面向接口(抽象)编程的概念与好处** 面向接口编程是一种设计原则,它提倡将实现细节隐藏在接口后面,让代码更加模块化,提高可维护性和可扩展性。在 Spring ...
本文将详细介绍如何在Java Web项目中搭建SSH框架,并进行相应的配置。 #### 二、Spring框架配置 Spring框架是整个SSH架构的核心,它提供了依赖注入(DI)和面向切面编程(AOP)的支持,能够帮助开发者更好地管理...
### SSH配置总结与部署步骤详解 #### 一、SSH框架简介 SSH框架是Java Web开发领域内非常流行的一种组合...以上就是关于SSH框架部署的基本步骤及注意事项的详细介绍。希望对正在学习或使用SSH框架的开发者有所帮助。
本文将详细介绍这三种通讯方式,并重点讨论如何在Java项目中进行相应的配置和整合。 #### Flex与Java通讯方式介绍 - **AMF**:Action Message Format,是一种轻量级的数据交换格式,主要用于Flex客户端与服务器端...
本大纲涵盖了从环境搭建到高级特性的详细讲解。 1. **全面阐释Spring及其功能** - Spring是一个开源的IoC和AOP框架,它通过提供容器管理和依赖注入来减少组件间的耦合。 - IoC使应用程序不再负责对象的创建和管理...
本文将详细介绍如何正确搭建Spring4.1 + Hibernate4 + Struts2的环境,并特别关注如何避免session数据为空的问题。 #### 二、环境搭建步骤 1. **创建Web项目**: - 使用Eclipse或IntelliJ IDEA等IDE工具创建一个新...
下面将基于这些信息详细阐述这些技术的关键知识点。 ### Struts2+Hibernate+Spring框架整合 #### 一、概述 Struts2、Hibernate与Spring(通常简称为SSH)是Java Web开发中的三个流行框架。它们的结合可以极大地...