- 浏览: 755060 次
- 性别:
- 来自: 郑州
文章分类
- 全部博客 (396)
- JAVA (50)
- ORACLE (22)
- HIBERNATE (1)
- SPRING (26)
- STRUTS (4)
- OTHERS (0)
- MYSQL (11)
- Struts2 (16)
- JS (33)
- Tomcat (6)
- DWR (1)
- JQuery (26)
- JBoss (0)
- SQL SERVER (0)
- XML (10)
- 生活 (3)
- JSP (11)
- CSS (5)
- word (1)
- MyEclipse (7)
- JSTL (1)
- JEECMS (2)
- Freemarker (8)
- 页面特效 (1)
- EXT (2)
- Web前端 js库 (2)
- JSON http://www.json.org (3)
- 代码收集 (1)
- 电脑常识 (6)
- MD5加密 (0)
- Axis (0)
- Grails (1)
- 浏览器 (1)
- js调试工具 (1)
- WEB前端 (5)
- JDBC (2)
- PowerDesigner (1)
- OperaMasks (1)
- CMS (1)
- Java开源大全 (2)
- 分页 (28)
- Eclipse插件 (1)
- Proxool (1)
- Jad (1)
- Java反编译 (2)
- 报表 (6)
- JSON (14)
- FCKeditor (9)
- SVN (1)
- ACCESS (1)
- 正则表达式 (3)
- 数据库 (1)
- Flex (3)
- pinyin4j (2)
- IBATIS (3)
- probe (1)
- JSP & Servlet (1)
- 飞信 (0)
- AjaxSwing (0)
- AjaxSwing (0)
- Grid相关 (1)
- HTML (5)
- Guice (4)
- Warp framework (1)
- warp-persist (1)
- 服务器推送 (3)
- eclipse (1)
- JForum (5)
- 工具 (1)
- Python (1)
- Ruby (1)
- SVG (3)
- Joda-Time日期时间工具 (1)
- JDK (3)
- Pushlet (2)
- JSP & Servlet & FTP (1)
- FTP (6)
- 时间与效率 (4)
- 二维码 (1)
- 条码/二维码 (1)
最新评论
-
ctrlc:
你这是从web服务器上传到FTP服务器上的吧,能从用户电脑上上 ...
jsp 往 FTP 上传文件问题 -
annybz:
说的好抽象 为什么代码都有两遍。这个感觉没有第一篇 和第二篇 ...
Spring源代码解析(三):Spring JDBC -
annybz:
...
Spring源代码解析(一):IOC容器 -
jie_20:
你确定你有这样配置做过测试? 请不要转载一些自己没有测试的文档 ...
Spring2.0集成iReport报表技术概述 -
asd51731:
大哥,limit传-1时出错啊,怎么修改啊?
mysql limit 使用方法
O/R工具出现之后,简化了许多复杂的信息持久化的开发。Spring应用开发者可以通过Spring提供的O/R方案更方便的使用各种持久化工具,比如
Hibernate;下面我们就Spring+Hibernate中的Spring实现做一个简单的剖析。
Spring对
Hinberanate的配置是通过LocalSessionFactoryBean来完成的,这是一个工厂Bean的实现,在基类
AbstractSessionFactoryBean中:
- /**
- * 这是FactoryBean需要实现的接口方法,直接取得当前的 sessionFactory的值
- */
- public Object getObject() {
- return this .sessionFactory;
- }
/** * 这是FactoryBean需要实现的接口方法,直接取得当前的sessionFactory的值 */ public Object getObject() { return this.sessionFactory; }
这个值在afterPropertySet中定义:
- public void afterPropertiesSet() throws Exception {
- //这个buildSessionFactory是通过配置信 息得到SessionFactory的地方
- SessionFactory rawSf = buildSessionFactory();
- //这里使用了Proxy方法插入对 getCurrentSession的拦截,得到和事务相关的session
- this .sessionFactory = wrapSessionFactoryIfNecessary(rawSf);
- }
public void afterPropertiesSet() throws Exception { //这个buildSessionFactory是通过配置信息得到SessionFactory的地方 SessionFactory rawSf = buildSessionFactory(); //这里使用了Proxy方法插入对getCurrentSession的拦截,得到和事务相关的session this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf); }
我们先看看SessionFactory是怎样创建的,这个方法很长,包含了创建Hibernate的SessionFactory的详尽步骤:
- protected SessionFactory buildSessionFactory() throws Exception {
- SessionFactory sf = null ;
- // Create Configuration instance.
- Configuration config = newConfiguration();
- //这里配置数据源,事务管理器,LobHander到 Holder中,这个Holder是一个ThreadLocal变量,这样这些资源就和线程绑定了
- if ( this .dataSource != null ) {
- // Make given DataSource available for SessionFactory configuration.
- configTimeDataSourceHolder.set( this .dataSource);
- }
- if ( this .jtaTransactionManager != null ) {
- // Make Spring-provided JTA TransactionManager available.
- configTimeTransactionManagerHolder.set( this .jtaTransactionManager);
- }
- if ( this .lobHandler != null ) {
- // Make given LobHandler available for SessionFactory configuration.
- // Do early because because mapping resource might refer to custom types.
- configTimeLobHandlerHolder.set( this .lobHandler);
- }
- //这里是使用Hibernate的各个属性的配置,这里使用 了Configuration类来抽象这些数据
- try {
- // Set connection release mode "on_close" as default.
- // This was the case for Hibernate 3.0; Hibernate 3.1 changed
- // it to "auto" (i.e. "after_statement" or "after_transaction").
- // However, for Spring's resource management (in particular for
- // HibernateTransactionManager), "on_close" is the better default.
- config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString());
- if (!isExposeTransactionAwareSessionFactory()) {
- // Not exposing a SessionFactory proxy with transaction-aware
- // getCurrentSession() method -> set Hibernate 3.1 CurrentSessionContext
- // implementation instead, providing the Spring-managed Session that way.
- // Can be overridden by a custom value for corresponding Hibernate property.
- config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS,
- "org.springframework.orm.hibernate3.SpringSessionContext" );
- }
- if ( this .entityInterceptor != null ) {
- // Set given entity interceptor at SessionFactory level.
- config.setInterceptor( this .entityInterceptor);
- }
- if ( this .namingStrategy != null ) {
- // Pass given naming strategy to Hibernate Configuration.
- config.setNamingStrategy( this .namingStrategy);
- }
- if ( this .typeDefinitions != null ) {
- // Register specified Hibernate type definitions.
- Mappings mappings = config.createMappings();
- for ( int i = 0 ; i < this .typeDefinitions.length; i++) {
- TypeDefinitionBean typeDef = this .typeDefinitions[i];
- mappings.addTypeDef(typeDef.getTypeName(), typeDef.getTypeClass(), typeDef.getParameters());
- }
- }
- if ( this .filterDefinitions != null ) {
- // Register specified Hibernate FilterDefinitions.
- for ( int i = 0 ; i < this .filterDefinitions.length; i++) {
- config.addFilterDefinition( this .filterDefinitions[i]);
- }
- }
- if ( this .configLocations != null ) {
- for ( int i = 0 ; i < this .configLocations.length; i++) {
- // Load Hibernate configuration from given location.
- config.configure( this .configLocations[i].getURL());
- }
- }
- if ( this .hibernateProperties != null ) {
- // Add given Hibernate properties to Configuration.
- config.addProperties( this .hibernateProperties);
- }
- if ( this .dataSource != null ) {
- boolean actuallyTransactionAware =
- ( this .useTransactionAwareDataSource || this .dataSource instanceof TransactionAwareDataSourceProxy);
- // Set Spring-provided DataSource as Hibernate ConnectionProvider.
- config.setProperty(Environment.CONNECTION_PROVIDER,
- actuallyTransactionAware ?
- TransactionAwareDataSourceConnectionProvider. class .getName() :
- LocalDataSourceConnectionProvider. class .getName());
- }
- if ( this .jtaTransactionManager != null ) {
- // Set Spring-provided JTA TransactionManager as Hibernate property.
- config.setProperty(
- Environment.TRANSACTION_MANAGER_STRATEGY, LocalTransactionManagerLookup. class .getName());
- }
- if ( this .mappingLocations != null ) {
- // Register given Hibernate mapping definitions, contained in resource files.
- for ( int i = 0 ; i < this .mappingLocations.length; i++) {
- config.addInputStream( this .mappingLocations[i].getInputStream());
- }
- }
- if ( this .cacheableMappingLocations != null ) {
- // Register given cacheable Hibernate mapping definitions, read from the file system.
- for ( int i = 0 ; i < this .cacheableMappingLocations.length; i++) {
- config.addCacheableFile( this .cacheableMappingLocations[i].getFile());
- }
- }
- if ( this .mappingJarLocations != null ) {
- // Register given Hibernate mapping definitions, contained in jar files.
- for ( int i = 0 ; i < this .mappingJarLocations.length; i++) {
- Resource resource = this .mappingJarLocations[i];
- config.addJar(resource.getFile());
- }
- }
- if ( this .mappingDirectoryLocations != null ) {
- // Register all Hibernate mapping definitions in the given directories.
- for ( int i = 0 ; i < this .mappingDirectoryLocations.length; i++) {
- File file = this .mappingDirectoryLocations[i].getFile();
- if (!file.isDirectory()) {
- throw new IllegalArgumentException(
- "Mapping directory location [" + this .mappingDirectoryLocations[i] +
- "] does not denote a directory" );
- }
- config.addDirectory(file);
- }
- }
- if ( this .entityCacheStrategies != null ) {
- // Register cache strategies for mapped entities.
- for (Enumeration classNames = this .entityCacheStrategies.propertyNames(); classNames.hasMoreElements();) {
- String className = (String) classNames.nextElement();
- String[] strategyAndRegion =
- StringUtils.commaDelimitedListToStringArray( this .entityCacheStrategies.getProperty(className));
- if (strategyAndRegion.length > 1 ) {
- config.setCacheConcurrencyStrategy(className, strategyAndRegion[ 0 ], strategyAndRegion[ 1 ]);
- }
- else if (strategyAndRegion.length > 0 ) {
- config.setCacheConcurrencyStrategy(className, strategyAndRegion[ 0 ]);
- }
- }
- }
- if ( this .collectionCacheStrategies != null ) {
- // Register cache strategies for mapped collections.
- for (Enumeration collRoles = this .collectionCacheStrategies.propertyNames(); collRoles.hasMoreElements();) {
- String collRole = (String) collRoles.nextElement();
- String[] strategyAndRegion =
- StringUtils.commaDelimitedListToStringArray( this .collectionCacheStrategies.getProperty(collRole));
- if (strategyAndRegion.length > 1 ) {
- config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[ 0 ], strategyAndRegion[ 1 ]);
- }
- else if (strategyAndRegion.length > 0 ) {
- config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[ 0 ]);
- }
- }
- }
- if ( this .eventListeners != null ) {
- // Register specified Hibernate event listeners.
- for (Iterator it = this .eventListeners.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- Assert.isTrue(entry.getKey() instanceof String, "Event listener key needs to be of type String" );
- String listenerType = (String) entry.getKey();
- Object listenerObject = entry.getValue();
- if (listenerObject instanceof Collection) {
- Collection listeners = (Collection) listenerObject;
- EventListeners listenerRegistry = config.getEventListeners();
- Object[] listenerArray =
- (Object[]) Array.newInstance(listenerRegistry.getListenerClassFor(listenerType), listeners.size());
- listenerArray = listeners.toArray(listenerArray);
- config.setListeners(listenerType, listenerArray);
- }
- else {
- config.setListener(listenerType, listenerObject);
- }
- }
- }
- // Perform custom post-processing in subclasses.
- postProcessConfiguration(config);
- // 这里是根据Configuration配置创建 SessionFactory的地方
- logger.info( "Building new Hibernate SessionFactory" );
- this .configuration = config;
- sf = newSessionFactory(config);
- }
- //最后把和线程绑定的资源清空
- finally {
- if ( this .dataSource != null ) {
- // Reset DataSource holder.
- configTimeDataSourceHolder.set( null );
- }
- if ( this .jtaTransactionManager != null ) {
- // Reset TransactionManager holder.
- configTimeTransactionManagerHolder.set( null );
- }
- if ( this .lobHandler != null ) {
- // Reset LobHandler holder.
- configTimeLobHandlerHolder.set( null );
- }
- }
- // Execute schema update if requested.
- if ( this .schemaUpdate) {
- updateDatabaseSchema();
- }
- return sf;
- }
protected SessionFactory buildSessionFactory() throws Exception { SessionFactory sf = null; // Create Configuration instance. Configuration config = newConfiguration(); //这里配置数据源,事务管理器,LobHander到Holder中,这个Holder是一个ThreadLocal变量,这样这些资源就和线程绑定了 if (this.dataSource != null) { // Make given DataSource available for SessionFactory configuration. configTimeDataSourceHolder.set(this.dataSource); } if (this.jtaTransactionManager != null) { // Make Spring-provided JTA TransactionManager available. configTimeTransactionManagerHolder.set(this.jtaTransactionManager); } if (this.lobHandler != null) { // Make given LobHandler available for SessionFactory configuration. // Do early because because mapping resource might refer to custom types. configTimeLobHandlerHolder.set(this.lobHandler); } //这里是使用Hibernate的各个属性的配置,这里使用了Configuration类来抽象这些数据 try { // Set connection release mode "on_close" as default. // This was the case for Hibernate 3.0; Hibernate 3.1 changed // it to "auto" (i.e. "after_statement" or "after_transaction"). // However, for Spring's resource management (in particular for // HibernateTransactionManager), "on_close" is the better default. config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString()); if (!isExposeTransactionAwareSessionFactory()) { // Not exposing a SessionFactory proxy with transaction-aware // getCurrentSession() method -> set Hibernate 3.1 CurrentSessionContext // implementation instead, providing the Spring-managed Session that way. // Can be overridden by a custom value for corresponding Hibernate property. config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "org.springframework.orm.hibernate3.SpringSessionContext"); } if (this.entityInterceptor != null) { // Set given entity interceptor at SessionFactory level. config.setInterceptor(this.entityInterceptor); } if (this.namingStrategy != null) { // Pass given naming strategy to Hibernate Configuration. config.setNamingStrategy(this.namingStrategy); } if (this.typeDefinitions != null) { // Register specified Hibernate type definitions. Mappings mappings = config.createMappings(); for (int i = 0; i < this.typeDefinitions.length; i++) { TypeDefinitionBean typeDef = this.typeDefinitions[i]; mappings.addTypeDef(typeDef.getTypeName(), typeDef.getTypeClass(), typeDef.getParameters()); } } if (this.filterDefinitions != null) { // Register specified Hibernate FilterDefinitions. for (int i = 0; i < this.filterDefinitions.length; i++) { config.addFilterDefinition(this.filterDefinitions[i]); } } if (this.configLocations != null) { for (int i = 0; i < this.configLocations.length; i++) { // Load Hibernate configuration from given location. config.configure(this.configLocations[i].getURL()); } } if (this.hibernateProperties != null) { // Add given Hibernate properties to Configuration. config.addProperties(this.hibernateProperties); } if (this.dataSource != null) { boolean actuallyTransactionAware = (this.useTransactionAwareDataSource || this.dataSource instanceof TransactionAwareDataSourceProxy); // Set Spring-provided DataSource as Hibernate ConnectionProvider. config.setProperty(Environment.CONNECTION_PROVIDER, actuallyTransactionAware ? TransactionAwareDataSourceConnectionProvider.class.getName() : LocalDataSourceConnectionProvider.class.getName()); } if (this.jtaTransactionManager != null) { // Set Spring-provided JTA TransactionManager as Hibernate property. config.setProperty( Environment.TRANSACTION_MANAGER_STRATEGY, LocalTransactionManagerLookup.class.getName()); } if (this.mappingLocations != null) { // Register given Hibernate mapping definitions, contained in resource files. for (int i = 0; i < this.mappingLocations.length; i++) { config.addInputStream(this.mappingLocations[i].getInputStream()); } } if (this.cacheableMappingLocations != null) { // Register given cacheable Hibernate mapping definitions, read from the file system. for (int i = 0; i < this.cacheableMappingLocations.length; i++) { config.addCacheableFile(this.cacheableMappingLocations[i].getFile()); } } if (this.mappingJarLocations != null) { // Register given Hibernate mapping definitions, contained in jar files. for (int i = 0; i < this.mappingJarLocations.length; i++) { Resource resource = this.mappingJarLocations[i]; config.addJar(resource.getFile()); } } if (this.mappingDirectoryLocations != null) { // Register all Hibernate mapping definitions in the given directories. for (int i = 0; i < this.mappingDirectoryLocations.length; i++) { File file = this.mappingDirectoryLocations[i].getFile(); if (!file.isDirectory()) { throw new IllegalArgumentException( "Mapping directory location [" + this.mappingDirectoryLocations[i] + "] does not denote a directory"); } config.addDirectory(file); } } if (this.entityCacheStrategies != null) { // Register cache strategies for mapped entities. for (Enumeration classNames = this.entityCacheStrategies.propertyNames(); classNames.hasMoreElements();) { String className = (String) classNames.nextElement(); String[] strategyAndRegion = StringUtils.commaDelimitedListToStringArray(this.entityCacheStrategies.getProperty(className)); if (strategyAndRegion.length > 1) { config.setCacheConcurrencyStrategy(className, strategyAndRegion[0], strategyAndRegion[1]); } else if (strategyAndRegion.length > 0) { config.setCacheConcurrencyStrategy(className, strategyAndRegion[0]); } } } if (this.collectionCacheStrategies != null) { // Register cache strategies for mapped collections. for (Enumeration collRoles = this.collectionCacheStrategies.propertyNames(); collRoles.hasMoreElements();) { String collRole = (String) collRoles.nextElement(); String[] strategyAndRegion = StringUtils.commaDelimitedListToStringArray(this.collectionCacheStrategies.getProperty(collRole)); if (strategyAndRegion.length > 1) { config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0], strategyAndRegion[1]); } else if (strategyAndRegion.length > 0) { config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0]); } } } if (this.eventListeners != null) { // Register specified Hibernate event listeners. for (Iterator it = this.eventListeners.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); Assert.isTrue(entry.getKey() instanceof String, "Event listener key needs to be of type String"); String listenerType = (String) entry.getKey(); Object listenerObject = entry.getValue(); if (listenerObject instanceof Collection) { Collection listeners = (Collection) listenerObject; EventListeners listenerRegistry = config.getEventListeners(); Object[] listenerArray = (Object[]) Array.newInstance(listenerRegistry.getListenerClassFor(listenerType), listeners.size()); listenerArray = listeners.toArray(listenerArray); config.setListeners(listenerType, listenerArray); } else { config.setListener(listenerType, listenerObject); } } } // Perform custom post-processing in subclasses. postProcessConfiguration(config); // 这里是根据Configuration配置创建SessionFactory的地方 logger.info("Building new Hibernate SessionFactory"); this.configuration = config; sf = newSessionFactory(config); } //最后把和线程绑定的资源清空 finally { if (this.dataSource != null) { // Reset DataSource holder. configTimeDataSourceHolder.set(null); } if (this.jtaTransactionManager != null) { // Reset TransactionManager holder. configTimeTransactionManagerHolder.set(null); } if (this.lobHandler != null) { // Reset LobHandler holder. configTimeLobHandlerHolder.set(null); } } // Execute schema update if requested. if (this.schemaUpdate) { updateDatabaseSchema(); } return sf; }
而直接调用org.hibernate.cfg.Configuration来得到需要的SessionFactory:
- protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {
- return config.buildSessionFactory();
- }
protected SessionFactory newSessionFactory(Configuration config) throws HibernateException { return config.buildSessionFactory(); }
所以我们这里看到LocalSessionFactory大致起到的一个读取资源配置然后生成SessionFactory的作用;当然这里在得
到 SessionFactory之后,还需要对session的事务管理作一些处理 -
使用了一个Proxy模式对getCurrentSession方法进行了拦截;
- //这里先根据当前的SessionFactory的类型得到Proxy,然后插入 Spring定义好的getCurrentSession拦截器
- protected SessionFactory getTransactionAwareSessionFactoryProxy(SessionFactory target) {
- Class sfInterface = SessionFactory. class ;
- if (target instanceof SessionFactoryImplementor) {
- sfInterface = SessionFactoryImplementor. class ;
- }
- return (SessionFactory) Proxy.newProxyInstance(sfInterface.getClassLoader(),
- new Class[] {sfInterface}, new TransactionAwareInvocationHandler(target));
- }
//这里先根据当前的SessionFactory的类型得到Proxy,然后插入Spring定义好的getCurrentSession拦截器 protected SessionFactory getTransactionAwareSessionFactoryProxy(SessionFactory target) { Class sfInterface = SessionFactory.class; if (target instanceof SessionFactoryImplementor) { sfInterface = SessionFactoryImplementor.class; } return (SessionFactory) Proxy.newProxyInstance(sfInterface.getClassLoader(), new Class[] {sfInterface}, new TransactionAwareInvocationHandler(target)); }
拦截器的实现如下:
- private static class TransactionAwareInvocationHandler implements InvocationHandler {
- private final SessionFactory target;
- public TransactionAwareInvocationHandler(SessionFactory target) {
- this .target = target;
- }
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- // Invocation on SessionFactory/SessionFactoryImplementor interface coming in...
- // 这里对getCurrentSession方法 进行拦截,得到一个和当前事务绑定的session交给用户
- if (method.getName().equals( "getCurrentSession" )) {
- // Handle getCurrentSession method: return transactional Session, if any.
- try {
- return SessionFactoryUtils.doGetSession((SessionFactory) proxy, false );
- }
- catch (IllegalStateException ex) {
- throw new HibernateException(ex.getMessage());
- }
- }
- else if (method.getName().equals( "equals" )) {
- // Only consider equal when proxies are identical.
- return (proxy == args[ 0 ] ? Boolean.TRUE : Boolean.FALSE);
- }
- else if (method.getName().equals( "hashCode" )) {
- // Use hashCode of SessionFactory proxy.
- return new Integer(hashCode());
- }
- // 这里是需要运行的SessionFactory 的目标方法
- try {
- return method.invoke( this .target, args);
- }
- catch (InvocationTargetException ex) {
- throw ex.getTargetException();
- }
- }
- }
private static class TransactionAwareInvocationHandler implements InvocationHandler { private final SessionFactory target; public TransactionAwareInvocationHandler(SessionFactory target) { this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Invocation on SessionFactory/SessionFactoryImplementor interface coming in... // 这里对getCurrentSession方法进行拦截,得到一个和当前事务绑定的session交给用户 if (method.getName().equals("getCurrentSession")) { // Handle getCurrentSession method: return transactional Session, if any. try { return SessionFactoryUtils.doGetSession((SessionFactory) proxy, false); } catch (IllegalStateException ex) { throw new HibernateException(ex.getMessage()); } } else if (method.getName().equals("equals")) { // Only consider equal when proxies are identical. return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); } else if (method.getName().equals("hashCode")) { // Use hashCode of SessionFactory proxy. return new Integer(hashCode()); } // 这里是需要运行的SessionFactory的目标方法 try { return method.invoke(this.target, args); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } } }
我们看看getCurrentSession的实现,在SessionFactoryUtils中:
- private static Session doGetSession(
- SessionFactory sessionFactory, Interceptor entityInterceptor,
- SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)
- throws HibernateException, IllegalStateException {
- Assert.notNull(sessionFactory, "No SessionFactory specified" );
- //这个 TransactionSynchronizationManager的Resource是一个ThreadLocal变 量,sessionFactory是一个单例,但ThreadLocal是和线程绑定的
- //这样就实现了Hiberante中常用的通过 ThreadLocal的session管理机制
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
- if (sessionHolder != null && !sessionHolder.isEmpty()) {
- // pre-bound Hibernate Session
- Session session = null ;
- if (TransactionSynchronizationManager.isSynchronizationActive() &&
- sessionHolder.doesNotHoldNonDefaultSession()) {
- // Spring transaction management is active ->
- // register pre-bound Session with it for transactional flushing.
- session = sessionHolder.getValidatedSession();
- if (session != null && !sessionHolder.isSynchronizedWithTransaction()) {
- logger.debug( "Registering Spring transaction synchronization for existing Hibernate Session" );
- TransactionSynchronizationManager.registerSynchronization(
- new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false ));
- sessionHolder.setSynchronizedWithTransaction( true );
- // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
- // with FlushMode.NEVER, which needs to allow flushing within the transaction.
- FlushMode flushMode = session.getFlushMode();
- if (flushMode.lessThan(FlushMode.COMMIT) &&
- !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
- session.setFlushMode(FlushMode.AUTO);
- sessionHolder.setPreviousFlushMode(flushMode);
- }
- }
- }
- else {
- // No Spring transaction management active -> try JTA transaction synchronization.
- session = getJtaSynchronizedSession(sessionHolder, sessionFactory, jdbcExceptionTranslator);
- }
- if (session != null ) {
- return session;
- }
- }
- //这里直接打开一个Session
- logger.debug( "Opening Hibernate Session" );
- Session session = (entityInterceptor != null ?
- sessionFactory.openSession(entityInterceptor) : sessionFactory.openSession());
- // Use same Session for further Hibernate actions within the transaction.
- // Thread object will get removed by synchronization at transaction completion.
- // 把新打开的Session放到 SessionHolder,然后放到ThreadLocal里面去和线程绑定起来,这个ThreadLocal是 在 TransactionSynchronizationManager中配置好的,可以根据sessionFactory来索取
- // 同时根据事务处理的状态来配置session的 属性,比如把FlushMode设置为Never,同时把session和事务处理关联起来
- if (TransactionSynchronizationManager.isSynchronizationActive()) {
- // We're within a Spring-managed transaction, possibly from JtaTransactionManager.
- logger.debug( "Registering Spring transaction synchronization for new Hibernate Session" );
- SessionHolder holderToUse = sessionHolder;
- if (holderToUse == null ) {
- holderToUse = new SessionHolder(session);
- }
- else {
- holderToUse.addSession(session);
- }
- if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
- session.setFlushMode(FlushMode.NEVER);
- }
- TransactionSynchronizationManager.registerSynchronization(
- new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true ));
- holderToUse.setSynchronizedWithTransaction( true );
-
if
(holderToUse != se
发表评论
-
Spring--quartz中cronExpression配置说明
2011-12-02 18:28 0quartz中cronExpression配置说明 字段 ... -
使用Spring的jdbcTemplate进一步简化JDBC操作
2011-12-02 09:20 1266先看applicationContext.xml配置文件: ... -
Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
2011-12-01 11:26 967实现一个控制器ShirdrnCon ... -
最简单的Spring MVC入门示例
2010-05-19 14:29 1529应一位朋友的要求,写一个最简单的spring示例,使用s ... -
Spring源代码解析(十):Spring Acegi框架授权的实现
2010-03-18 12:48 1525我们从FilterSecurityIntercep ... -
Spring源代码解析(九):Spring Acegi框架鉴权的实现
2010-03-18 12:47 1507简单分析一下Spring Acegi的源代码实现: Ser ... -
Spring源代码解析(七):Spring AOP中对拦截器调用的实现
2010-03-18 12:40 1422前面我们分析了Spring AOP实现中得到Proxy对象的过 ... -
Spring源代码解析(六):Spring声明式事务处理
2010-03-18 12:37 1098我们看看Spring中的事务处理的代码,使用Spring管理事 ... -
Spring源代码解析(五):Spring AOP获取Proxy
2010-03-18 12:36 1324下面我们来看看Spring的AOP的一些相关代码是怎么得到Pr ... -
Spring源代码解析(四):Spring MVC
2010-03-18 12:35 7744下面我们对Spring MVC框架代码进行分析,对于web ... -
Spring源代码解析(三):Spring JDBC
2010-03-18 12:33 1698下面我们看看Spring JDBC相关的实现, 在Spri ... -
Spring源代码解析(二):IoC容器在Web容器中的启动
2010-03-18 12:32 1449上面我们分析了IOC容器本身的实现,下面我们看看在典型的web ... -
Spring源代码解析(一):IOC容器
2010-03-18 12:30 2673在Spring中,IOC容器的重要地位我们就不多说了,对于Sp ... -
使用Spring的JdbcTemplate和BeanPropertyRowMapper完成的JDBC
2010-03-18 12:08 2248先道要加上两个包:Spring2.5下面的: spring. ... -
使用Spring的SimpleJdbcTemplate完成DAO操作
2010-03-18 12:06 1511l SimpleJdbcTemplate内部包含了 ... -
使用Spring的NamedParameterJdbcTemplate完成DAO操作
2010-03-18 12:05 1428NamedParameterJdbcTemplate内部包含了 ... -
Spring in Action 学习笔记—第四章 征服数据库(转)
2010-03-18 12:03 1255Spring2.0正式版(http://www.springf ... -
Spring管理JDBC连接
2010-03-18 11:59 1692在Spring中,JdbcTemplate是经常被使用的类来帮 ... -
Spring JDBC数据库操作类
2010-03-18 09:26 16461.JdbcTemplate 在Spring中, ... -
Spring JdbcTemplate 批量插入或更新操作
2010-03-18 09:19 5284用 JdbcTemplate 进行批量插入或更新操作 ...
相关推荐
Spring源代码解析1:IOC容器.doc ...Spring源代码解析8:Spring驱动Hibernate的实现.doc Spring源代码解析9:Spring Acegi框架鉴权的实现.doc Spring源代码解析10:Spring Acegi框架授权的实现.doc
Spring源代码解析7:Spring AOP中对拦截器调用的实现 Spring源代码解析8:Spring驱动Hibernate的实现;Spring源代码解析9:Spring Acegi框架鉴权的实现 Spring源代码解析10:Spring Acegi框架授权的实现
Spring源代码解析(一):IOC容器 ...Spring源代码解析(八):Spring驱动Hibernate的实现 Spring源代码解析(九):Spring Acegi框架鉴权的实现 Spring源代码解析(十):Spring Acegi框架授权的实现
总的来说,Spring通过`LocalSessionFactoryBean`实现对Hibernate的驱动,它将数据源、事务管理和配置信息集成在一起,创建出适应Spring管理的`SessionFactory`。这种集成方式使得开发者无需过多关注底层细节,可以...
pring源代码解析1:IOC容器;Spring源代码解析2:IoC容器在Web容器中的启动;Spring源代码解析3:... Spring源代码解析7:Spring AOP中对拦截器调用的实现 Spring源代码解析8:Spring驱动Hibernate的实现;Spring源代
6. **Spring与Hibernate集成**:"spring源代码解析(八):spring驱动Hibernate的实现.doc"探讨了Spring如何与ORM框架Hibernate协同工作,包括SessionFactory的创建、Session管理以及事务策略的配置。 7. **Spring ...
Spring源代码解析(一)Spring中的事务...Spring源代码解析(八):Spring驱动Hibernate的实现.doc Spring源代码解析(九):Spring Acegi框架鉴权的实现.doc Spring源代码解析(十):Spring Acegi框架授权的实现.doc
精通Java EE:Eclipse Struts2 Hibernate Spring整合应用案例代码和数据库压缩包6
《Spring源代码解析》 Spring框架作为Java领域最流行的开源框架之一,它的设计思想和实现方式一直是广大开发者关注的焦点。深入理解Spring的源代码,能够帮助我们更好地掌握其工作原理,提高我们的开发效率和代码...
JavaEE源代码 spring-hibernate3JavaEE源代码 spring-hibernate3JavaEE源代码 spring-hibernate3JavaEE源代码 spring-hibernate3JavaEE源代码 spring-hibernate3JavaEE源代码 spring-hibernate3JavaEE源代码 spring-...
收集的Java Web整合开发实战:基于Struts 2+Hibernate+Spring-源代码,看到其他人下载币要的太多,给大家分享一下。 不是很全,但可以拿来参考了。 由于大小限制,还有另外一个包······
这个“Spring+hibernate整合源代码”应该包含了实现上述整合步骤的示例代码,可以作为学习和参考的资源。通过学习和实践这些代码,你可以更好地理解和掌握 Spring 和 Hibernate 整合的细节,提升你的 Java Web 开发...
这个“Spring源代码解析”压缩包文件很可能是对Spring框架内部实现原理的详细分析,帮助开发者深入理解其工作机制。 在Spring框架中,依赖注入是核心概念之一,它允许开发者在运行时通过容器来管理对象的创建和依赖...
【hibernate和spring源代码】的分析与详解 Hibernate是一个强大的对象关系映射(ORM)框架,它在Java开发中被广泛使用,为开发者提供了便捷的数据持久化服务。3.3.2.GA版本是Hibernate的一个稳定版本,它包含了对...
源代码分析有助于深入理解Spring的工作原理,提升编程技能,并且能够帮助开发者在遇到问题时进行调试和优化。 1. **Spring IoC容器**: Spring的核心是IoC容器,它负责管理对象的生命周期和依赖关系。通过XML配置...
《精通Spring源代码》是罗时飞先生关于Spring框架深入解析的一部著作,旨在帮助开发者更深入地理解Spring的工作原理,提升对Java企业级应用开发的掌控能力。本压缩包包含的文件名为“精通Spring源码”,这通常是一个...
《Spring实战》第五版的源代码压缩包"spring实战全部源代码.zip"包含了全面的示例项目,旨在帮助读者深入理解和应用Spring框架。这个压缩包中的"spring-in-action-5-samples-master"目录揭示了书中的各个实战案例,...