- 浏览: 185997 次
- 性别:
- 来自: 成都
最新评论
-
ls_autoction:
非常谢谢!:oops:
webservice注解 -
ganbo:
helloword
hibernate之openSession和getCurrentSession -
ganbo:
...
hibernate之openSession和getCurrentSession -
zizhengwang:
文章介绍的很全面,帮助我解决了问题,谢谢
hibernate之级联cascade和关系维持inverse -
yccn1984:
/** * 子类进行注入 * ...
DAO和Service公共抽象接口实现
从上篇文章分析得出,hibernate初始化时,flushBeforeCompletion、autoCloseSession默认为false
releaseMode为auto。还有hibernate.jdbc.batch_size默认为0等。那现在来分析openSession和getCurrentSession的区别就不难了。
首先进入sessionFactory的实现类SessionFactoryImpl,接到上篇文章继续分析:
上篇文章的configuration最后调用代码如下:
return new SessionFactoryImpl( this, mapping, settings, getInitializedEventListeners(), sessionFactoryObserver );
public SessionFactoryImpl( Configuration cfg, Mapping mapping, Settings settings, EventListeners listeners, SessionFactoryObserver observer) throws HibernateException { //省略 currentSessionContext = buildCurrentSessionContext(); //省略 } private CurrentSessionContext buildCurrentSessionContext() { String impl = properties.getProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS ); // for backward-compatability if ( impl == null && transactionManager != null ) { impl = "jta"; } if ( impl == null ) { return null; } else if ( "jta".equals( impl ) ) { if ( settings.getTransactionFactory().areCallbacksLocalToHibernateTransactions() ) { log.warn( "JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()" ); } return new JTASessionContext( this ); } else if ( "thread".equals( impl ) ) { return new ThreadLocalSessionContext( this ); } else if ( "managed".equals( impl ) ) { return new ManagedSessionContext( this ); } else { try { Class implClass = ReflectHelper.classForName( impl ); return ( CurrentSessionContext ) implClass .getConstructor( new Class[] { SessionFactoryImplementor.class } ) .newInstance( new Object[] { this } ); } catch( Throwable t ) { log.error( "Unable to construct current session context [" + impl + "]", t ); return null; } } }
默认会采用jta事物
那么现在调用openSession继续追踪
public org.hibernate.classic.Session openSession() throws HibernateException { return openSession(interceptor); } public org.hibernate.classic.Session openSession(Interceptor sessionLocalInterceptor)throws HibernateException { long timestamp = settings.getRegionFactory().nextTimestamp(); return openSession( null, true, timestamp, sessionLocalInterceptor ); } private SessionImpl openSession( Connection connection, boolean autoClose, long timestamp, Interceptor sessionLocalInterceptor ) { return new SessionImpl( connection, this, autoClose, timestamp, sessionLocalInterceptor == null ? interceptor : sessionLocalInterceptor, settings.getDefaultEntityMode(), settings.isFlushBeforeCompletionEnabled(), settings.isAutoCloseSessionEnabled(), settings.getConnectionReleaseMode() ); }
发现通过openSession开启的Session有以下状态:flushBeforeCompletionEnabled=false,autoCloseSessionEnabled=false,connectionReleaseMode=true
调用getCurrentSession进行追踪:
public org.hibernate.classic.Session getCurrentSession() throws HibernateException { if ( currentSessionContext == null ) { throw new HibernateException( "No CurrentSessionContext configured!" ); } return currentSessionContext.currentSession(); }
进入ThreadLocalSessionContext继续追踪
public final Session currentSession() throws HibernateException { Session current = existingSession( factory ); if (current == null) { current = buildOrObtainSession(); // register a cleanup synch current.getTransaction().registerSynchronization( buildCleanupSynch() ); // wrap the session in the transaction-protection proxy if ( needsWrapping( current ) ) { current = wrap( current ); } // then bind it doBind( current, factory ); } return current; }
protected Session buildOrObtainSession() { return factory.openSession( null, isAutoFlushEnabled(), isAutoCloseEnabled(), getConnectionReleaseMode() ); } protected boolean isAutoFlushEnabled() { return true; } protected boolean isAutoCloseEnabled() { return true; }
可看出getCurrentSession的状态
flushBeforeCompletionEnabled=true,autoCloseSessionEnabled=true,connectionReleaseMode=true
现在两种方式都是最终调用同一个方法,继续跟踪
public org.hibernate.classic.Session openSession( final Connection connection, final boolean flushBeforeCompletionEnabled, final boolean autoCloseSessionEnabled, final ConnectionReleaseMode connectionReleaseMode) throws HibernateException { return new SessionImpl( connection, this, true, settings.getRegionFactory().nextTimestamp(), interceptor, settings.getDefaultEntityMode(), flushBeforeCompletionEnabled, autoCloseSessionEnabled, connectionReleaseMode ); }
SessionImpl( final Connection connection, final SessionFactoryImpl factory, final boolean autoclose, final long timestamp, final Interceptor interceptor, final EntityMode entityMode, final boolean flushBeforeCompletionEnabled, final boolean autoCloseSessionEnabled, final ConnectionReleaseMode connectionReleaseMode) { super( factory ); this.rootSession = null; this.timestamp = timestamp; this.entityMode = entityMode; this.interceptor = interceptor; this.listeners = factory.getEventListeners(); this.actionQueue = new ActionQueue( this ); this.persistenceContext = new StatefulPersistenceContext( this ); this.flushBeforeCompletionEnabled = flushBeforeCompletionEnabled; this.autoCloseSessionEnabled = autoCloseSessionEnabled; this.connectionReleaseMode = connectionReleaseMode; this.jdbcContext = new JDBCContext( this, connection, interceptor ); loadQueryInfluencers = new LoadQueryInfluencers( factory ); if ( factory.getStatistics().isStatisticsEnabled() ) { factory.getStatisticsImplementor().openSession(); } if ( log.isDebugEnabled() ) { log.debug( "opened session at timestamp: " + timestamp ); } }
继续跟踪this.jdbcContext = new JDBCContext( this, connection, interceptor );session和collection的关联过程
public JDBCContext(Context owner, Connection connection, Interceptor interceptor) { this.owner = owner; this.connectionManager = new ConnectionManager( owner.getFactory(), this, owner.getConnectionReleaseMode(), connection, interceptor ); final boolean registerSynchronization = owner.isAutoCloseSessionEnabled() || owner.isFlushBeforeCompletionEnabled() || owner.getConnectionReleaseMode() == ConnectionReleaseMode.AFTER_TRANSACTION; if ( registerSynchronization ) { registerSynchronizationIfPossible(); } }
继续跟踪ConnectionManager的创建过程
public ConnectionManager( SessionFactoryImplementor factory, Callback callback, ConnectionReleaseMode releaseMode, Connection connection, Interceptor interceptor) { this.factory = factory; this.callback = callback; this.interceptor = interceptor; this.batcher = factory.getSettings().getBatcherFactory().createBatcher( this, interceptor ); this.connection = connection; wasConnectionSupplied = ( connection != null ); this.releaseMode = wasConnectionSupplied ? ConnectionReleaseMode.ON_CLOSE : releaseMode; }
public Connection getConnection() throws HibernateException { if ( isClosed ) { throw new HibernateException( "connection manager has been closed" ); } if ( connection == null ) { openConnection(); } return connection; }
private void openConnection() throws HibernateException { if ( connection != null ) { return; } log.debug("opening JDBC connection"); try { connection = factory.getConnectionProvider().getConnection(); } catch (SQLException sqle) { throw JDBCExceptionHelper.convert( factory.getSQLExceptionConverter(), sqle, "Cannot open connection" ); } callback.connectionOpened(); // register synch; stats.connect() }
发表评论
-
spring-mvc属性编辑器绑定和传文件
2011-10-27 09:58 1322@InitBinder public void i ... -
spring jms
2011-10-25 17:53 1674简介 ActiveMQ 是 ... -
tomcat集群 部署rest服务
2011-10-23 13:24 4932一、软件准备 Apache 2.2 : http ... -
通过cxf提供rest服务
2011-10-23 13:10 4557rest接口 import javax.ws.rs.D ... -
hibernate+memcached一种orm+cached解决方案
2011-10-22 14:04 26851、首先配置hibernate,使之支持二级缓存: @ ... -
spring +jotm分布式数据库
2011-10-20 22:28 5963首先引入jotm和xpool包,通过maven引入 & ... -
spring通过jndi创建数据源
2011-10-20 19:43 1196<bean id="jndiDS&quo ... -
spring、hibernate源码分析一
2011-10-19 09:11 1998<bean id="dataSource ... -
hibernate之openSession和getCurrentSession
2011-10-18 14:54 3109hibernate通过sessionFactory有两种方式获 ... -
hibernate之ManyToMany
2011-10-18 11:43 1135一个老师有多个学生,同样一个学生有多个老师,配置如下: ... -
hibernate之级联cascade和关系维持inverse
2011-10-18 11:11 21761hibernate的关联关系,重点在理解级联cascade和i ... -
持久层开发,基于lucene,hibernate-search
2011-10-17 15:19 17731、pojo类编写 import javax.pers ... -
spring和hibernate集成,基于注解的配置
2011-10-17 15:17 1066<?xml version="1.0&q ...
相关推荐
Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务处理...
Spring源代码解析(二):ioc容器在Web容器中的启动 3.Spring源代码解析(三):Spring JDBC 4.Spring源代码解析(四):Spring MVC 5.Spring源代码解析(五):Spring AOP获取Proxy 6. Spring源代码解析(六):Spring...
一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 1.2.3 业务逻辑组件 1.3 J2EE...
一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 1.2.3 业务逻辑组件 1.3 J2EE...
Strutrs2 Spring2 Hibernate3的整合源码与开发过程分析
一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 1.2.3 业务逻辑组件 1.3 J2EE...
SSH配置源码分析,总结,面试常问到,配置详解,原理,机制。
基于Struts2+Hibernate+Spring+MySQL的信通B2C网站管理系统信通购物网 采用当前最流行的技术平台,Struts2+Hibernate+Spring+MySQL。 1、首页:增加动态效果,产品分类栏目,首页广告,网站公告; 2、会员功能:...
本书由希赛IT教育研发中心组织编写,是关于当前流行的开源框架技术Struts 2、Hibernate 3和Spring 2的整合书籍。本书详尽分析了这3种技术,并通过实例讲解这3种技术的整合。 本书分为5篇共21章,包括环境准备篇、...
用spring+hibernate+struts+freemarker做的个人博客源码 里边包含了数据库模块,配置即可用,可分析源码做为学习
本书由希赛IT教育研发中心组织编写,是关于当前流行的开源框架技术Struts 2、Hibernate 3和Spring 2的整合书籍。本书详尽分析了这3种技术,并通过实例讲解这3种技术的整合。 本书分为5篇共21章,包括环境准备篇、...
Spring源代码解析(二):ioc容器在Web容器中的启动.doc Spring源代码分析(三):Spring JDBC.doc Spring源代码解析(四):Spring MVC.doc Spring源代码解析(五):Spring AOP获取Proxy.doc Spring源代码解析(六):...
内容概要:这个项目是一个基于Spring + Spring MVC + Hibernate的在线招标系统,旨在为用户提供一个方便快捷的招标流程管理平台。系统包括了招标信息发布、投标管理、中标结果确认等功能,旨在简化招标过程,提高...
Struts2 Spring Hibernate 框架 整合源码 huangtOA简单系统源码
本文针对教育工作的具体需求,用struts + spring + hibernate搭建的框架为设计平台,以B/S(Browser/Server)模式开发与设计题库及试卷管理模块。本设计为Smart系统的一部分。主要设计和实现了学校考试的出卷方式与...
Spring整合Struts2 Spring整合Hibernate Struts2是一个web层框架 用于和数据库进行交互 Spring框架用来处理业务层 包括IOC容器 和Aop功能 Hibernate是一个持久化层框架 用于操作数据库
Spring源代码解析(二):IoC容器在Web容器中的启动 Spring源代码解析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务...
这个名为“基于JSP的博客系统(struts+...通过分析和使用这些源码,开发者可以学习到如何在现实项目中实现安全的用户认证、内容管理、数据持久化和多层架构设计等关键功能。无论是作为教育工具还是作为实际项目
【源码】基于JSP的车辆管理系统(Struts+Hibernate+Spring+Oracle)是一个功能强大且技术先进的车辆管理解决方案源码包。该源码包融合了多种主流技术,包括JSP、Struts、Hibernate、Spring和Oracle数据库,以提供...