`
5xnl
  • 浏览: 23643 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

非web环境spring管理hibernate,延迟加载问题

阅读更多
/*
 * @(#)HibernateLazyResolver.java
 *
 * Copyright 2009 Xinhua Online, Inc. All rights reserved.
 */

package com.winxuan.framework.util.hibernate;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * 仿照openSessionInViewFilter,完成在非web环境下session打开
 * @version 1.0,2009-6-9
 */


public class HibernateLazyResolver implements Serializable{

	private static final long serialVersionUID = 4728620183698481396L;

	private static final Log LOG = LogFactory.getLog(HibernateLazyResolver.class);;

	private boolean singleSession = true; 

	private SessionFactory sessionFactory;

	boolean participate = false;

	protected Session session = null;
	   
	public final void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory=sessionFactory;
	}

	/**
	* Set whether to use a single session for each request. Default is true.
	* <p>If set to false, each data access operation or transaction will use
	* its own session (like without Open Session in View);. Each of those
	* sessions will be registered for deferred close, though, actually
	* processed at request completion.
	* @see SessionFactoryUtils#initDeferredClose
	* @see SessionFactoryUtils#processDeferredClose
	*/
	public void setSingleSession(boolean singleSession) {
		this.singleSession = singleSession;
	}

	/**
	* Return whether to use a single session for each request.
	*/
	protected boolean isSingleSession() {
		return singleSession;
	}

	/**
	 * 初始化 session, 在需要 lazy 的开始处调用
	 *
	 */
	public void openSession() {
		if (isSingleSession()) {
			// single session mode
			if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
				// Do not modify the Session: just set the participate flag.
				participate = true;
			}
			else {
				LOG.debug("Opening single Hibernate Session in HibernateLazyResolver");
				session = getSession(sessionFactory);
				TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
			}
		}
		else {
			// deferred close mode
			if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
				// Do not modify deferred close: just set the participate flag.
				participate = true;
			}
			else {
				SessionFactoryUtils.initDeferredClose(sessionFactory);
			}
		}
		
	}

	/**
	 * 释放 session, 在 lazy 的结束处调用
	 *
	 */
	public void releaseSession() {
		if (!participate){
			if (isSingleSession()) {
				// single session mode
				SessionHolder sessionHolder =
						(SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
				LOG.debug("Closing single Hibernate Session in HibernateLazyResolver");
				closeSession(sessionHolder.getSession(), sessionFactory);
			}
			else {
				// deferred close mode
				SessionFactoryUtils.processDeferredClose(sessionFactory);
			}
		}
	}
		
	/**
	 * Get a Session for the SessionFactory that this filter uses.
	 * Note that this just applies in single session mode!
	 * <p>The default implementation delegates to SessionFactoryUtils'
	 * getSession method and sets the Session's flushMode to NEVER.
	 * <p>Can be overridden in subclasses for creating a Session with a custom
	 * entity interceptor or JDBC exception translator.
	 * @param sessionFactory the SessionFactory that this filter uses
	 * @return the Session to use
	 * @throws DataAccessResourceFailureException if the Session could not be created
	 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean);
	 * @see org.hibernate.FlushMode#NEVER
	 */
	protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
		Session session = SessionFactoryUtils.getSession(sessionFactory, true);
		session.setFlushMode(FlushMode.AUTO);
		return session;
	}

	/**
	 * Close the given Session.
	 * Note that this just applies in single session mode!
	 * <p>Can be overridden in subclasses, e.g. for flushing the Session before
	 * closing it. See class-level javadoc for a discussion of flush handling.
	 * Note that you should also override getSession accordingly, to set
	 * the flush mode to something else than NEVER.
	 * @param session the Session used for filtering
	 * @param sessionFactory the SessionFactory that this filter uses
	 */
	protected void closeSession(Session session, SessionFactory sessionFactory) {
		session.flush();
		SessionFactoryUtils.releaseSession(session, sessionFactory);
	}
	
	
}



分享到:
评论

相关推荐

    Spring.3.x企业应用开发实战(完整版).part2

    12.2.8 延迟加载的问题 12.3 在Spring中使用myBatis 12.3.1 配置SqlMapClient 12.3.2 在Spring配置myBatis 12.3.3 编写myBatis的DAO 12.5 DAO层设计 12.5.1 DAO基类的设计 12.5.2 查询接口方法的设计 12.5.3 分页...

    jtechlog-lazy:JPA延迟加载

    提供了EclipseLink和Hibernate持久性提供程序的JPA延迟加载。 具有JPA持久层和Spring MVC Web层的双层应用程序。 可以用Maven构建,下载后mvn jetty:run命令mvn jetty:run 。 它默认设置为 EclipseLink,您需要...

    Spring中文帮助文档

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring API

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring3.x企业应用开发实战(完整版) part1

    12.2.8 延迟加载的问题 12.3 在Spring中使用myBatis 12.3.1 配置SqlMapClient 12.3.2 在Spring配置myBatis 12.3.3 编写myBatis的DAO 12.5 DAO层设计 12.5.1 DAO基类的设计 12.5.2 查询接口方法的设计 12.5.3 分页...

    Spring面试题

    1. Hibernate2延迟加载实现:a)实体对象 b)集合(Collection) 2. Hibernate3 提供了属性的延迟加载功能 当Hibernate在查询数据的时候,数据并没有存在与内存中,当程序真正对数据的操作时,对象才存在与内存中,...

    Grails 中文参考手册

    5.3.4 立即加载和延迟加载 5.3.4 悲观锁和乐观锁 5.4 GORM查询 5.4.1 动态查找器 5.4.2 条件查询 5.4.3 Hibernate查询语言 5.5 高级GORM特性 5.5.1 事件和自动实现时间戳 5.5.2 自定义ORM映射 5.5.2.1 表名和列名 ...

    java面试题库2021.pdf

    ②延迟加载、 性能优化 ③HQL 查询、 条件查询、 SQL 查询 ④二级缓存与查询缓存 3、 Struts ①MVC 模式与 Struts 体系 4、 mybatis 5、 MVC 框架 6、 各框架对比与项目优化 7、 JPA ①EJB 三、 Java web 开发核心...

    Java常见面试题208道.docx

    面试题包括以下十九部分:Java 基础、容器、多线程、反射、对象拷贝、Java Web 模块、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、Mybatis、RabbitMQ、Kafka、Zookeeper、MySql...

    java面试题

    71.7. Hibernate是如何延迟加载? 60 71.8. Hibernate中怎样实现类之间的关系?(如:一对多、多对多的关系) 60 71.9. 说下Hibernate的缓存机制 60 71.10. Hibernate的查询方式 60 71.11. 如何优化Hibernate? 61 71.12...

    千方百计笔试题大全

    97、Hibernate是如何延迟加载? 22 98、Hibernate中怎样实现类之间的关系?(如:一对多、多对多的关系) 22 99、说下Hibernate的缓存机制 22 100、Hibernate的查询方式 23 101、如何优化Hibernate? 23 102、Struts工作...

    java面试宝典

    97、Hibernate是如何延迟加载? 22 98、Hibernate中怎样实现类之间的关系?(如:一对多、多对多的关系) 22 99、说下Hibernate的缓存机制 22 100、Hibernate的查询方式 23 101、如何优化Hibernate? 23 102、Struts工作...

    iBATIS实战

    3.3.1 针对延迟加载的字节码增强 48 3.3.2 Jakarta Commons数据库连接池 49 3.3.3 分布式高速缓存 49 3.4 将iBATIS添加到应用程序中 49 3.4.1 在独立应用程序中使用iBATIS 50 3.4.2 在Web应用程序中使用iBATIS 50 ...

    JAVA上百实例源码以及开源项目

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...

    java开源包1

    Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解的依赖注入和RPC机制。 Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java...

    JAVA上百实例源码以及开源项目源代码

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...

    java开源包11

    Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解的依赖注入和RPC机制。 Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java...

Global site tag (gtag.js) - Google Analytics