Spring 與hibernate 的結合

O/R工具出現之後,簡化了許多複雜的信息持久化的開發。Spring應用開發者可以通過Spring提 供的O/R方案更方便的使用各種持久化工具,比如Hibernate;下面我們就Spring+Hibernate中的Spring實現做一個簡單的剖 析。 Spring對Hinberanate的配置是通過LocalSessionFactoryBean來完成的,這是一個工廠Bean的實現,在基類 AbstractSessionFactoryBean中:
  • /**
  • * 這是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);

我們先看看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
  •                 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); 
  •             } 
  •         } 
  •         if (this.configLocations != null) { 
  •             for (int i = 0; i < this.configLocations.length; i++) { 
  •                 // Load Hibernate configuration from given location.
  •                 config.configure(this.configLocations.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.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.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
  •                 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.getFile(); 
  •                 if (!file.isDirectory()) { 
  •                     throw new IllegalArgumentException( 
  •                             "Mapping directory location [" + this.mappingDirectoryLocations
  •                             "] 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(); 

所以我們這裏看到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)); 
  •     } 

攔截器的實現如下:

  • 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 != sessionHolder) { 
  •                 TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); 
  •             } 
  •         } 
  •         else { 
  •             // No Spring transaction management active -> try JTA transaction synchronization.
  •             registerJtaSynchronization(session, sessionFactory, jdbcExceptionTranslator, sessionHolder); 
  •         } 
  •         // Check whether we are allowed to return the Session.
  •         if (!allowCreate && !isSessionTransactional(session, sessionFactory)) { 
  •             closeSession(session); 
  •             throw new IllegalStateException("No Hibernate Session bound to thread, " + 
  •                 "and configuration does not allow creation of non-transactional one here"); 
  •         } 
  •         return session; 
  •     } 

這裏就是在Spring中爲使用Hiberante的SessionFactory以 及Session做的準備工作,在這個基礎上,用戶可以通過使用 HibernateTemplate來使用Hibernate的O/R功能,和以前看到的一樣這是一個execute的回調:

  • public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException { 
  •     Assert.notNull(action, "Callback object must not be null"); 
  •     //這裏得到配置好的Hibernate的Session
  •     Session session = getSession(); 
  •     boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory()); 
  •     if (existingTransaction) { 
  •         logger.debug("Found thread-bound Session for HibernateTemplate"); 
  •     } 
  •     FlushMode previousFlushMode = null; 
  •     try { 
  •         previousFlushMode = applyFlushMode(session, existingTransaction); 
  •         enableFilters(session); 
  •         Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session)); 
  •         //這裏是回調的入口
  •         Object result = action.doInHibernate(sessionToExpose); 
  •         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 { 
  •         //如果這個調用的方法在一個事務當中,
  •         if (existingTransaction) { 
  •             logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate"); 
  •             disableFilters(session); 
  •             if (previousFlushMode != null) { 
  •                 session.setFlushMode(previousFlushMode); 
  •             } 
  •         } //否則把Session關閉
  •         else { 
  •             // Never use deferred close for an explicitly new Session.
  •             if (isAlwaysUseNewSession()) { 
  •                 SessionFactoryUtils.closeSession(session); 
  •             } 
  •             else { 
  •                 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory()); 
  •             } 
  •         } 
  •     } 

我們看看怎樣得到對應的Session的,仍然使用了SessionFactoryUtils的方法doGetSession:

  • protected Session getSession() { 
  •     if (isAlwaysUseNewSession()) { 
  •         return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor()); 
  •     } 
  •     else if (!isAllowCreate()) { 
  •         return SessionFactoryUtils.getSession(getSessionFactory(), false); 
  •     } 
  •     else { 
  •         return SessionFactoryUtils.getSession( 
  •                 getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator()); 
  •     } 

這樣我們就可以和其他的Template那樣使用Hibernate的基本功能了,使用的時候Spring已經爲我們對Session的獲取和關閉,事務處理的綁定做好了封裝 - 從這個角度看也大大方便了用戶的使用。

Spring的事務管理器簡介
Spring並不直接管理事務,事實上,它是提供事務的多方選擇。你能委託事務的職責給一個特定的平臺實現,比如用JTA或者是別的持久機制。Spring的事務管理器可以用下表表示:

 

事務管理器的實例

目標

Org.springframwork.jdbc.datasource.DataSourceTransactionManager

用DBC的 DataSource 去管理事務

Org.springframwork.orm.hibernate.HibernateTransactionManager

當持久機制是Hibernate時的事務管理器

Org.springframework.orm.jdoTransactionManager

當持久機制是JDO時的事務管理器

Org.springframework.transaction.jta.JtaTransactionManager

用JTA管理事務

Org.springframwork.orm.ojb.PersistenceBrokerTransactionManager

當持久機制是apache的OJB時的事務管理器

所有的事務管理器就像是特別平臺的代理商。爲了使用事務管理器,我們應該在上下文中申明它。現在,我們將會看看如何去申明這些事務管理器,以DataSourceTransactionManager爲例開始
3.1  JDBC事務
如果你直接使用JDBC作爲持久方案,DataSourceTransactionManager將會爲你管理 事務的邊界。爲了能使用DataSourceTransactionManager,我們把下面的XML加到上下文的定義中: <bean id="transactionManager" class="org.springframework.jdbc.    Datasource.DataSourceTransactionManager">    <property name="dataSource">         <ref bean="dataSource"/>    </property> </bean> 注意:dataSource的屬性設置引用一個叫dataSource的BEAN,dataSource可能是一個 javax.sql.DataSource bean,它在上下文中已經定義好了。
3.2 Hibernate事務
如果你使用Hibernate作爲持久層,你將會使用 HibernateTransactionManager,請在應用的上下文申明如下: <bean id="transactionManaager" class="org.springframework. Orm.hibernate.HibernateTransactionManager">   <property name="sessionFactory">        <ref bean="sessionFactory"/>   </property> </bean> sessionFacoty 的屬性和Hibernate的SessionFactory組合在一塊兒。HibernateTransactionManager委託事務管理給一個 net.sf.hibernate.Transactioin事例,它從Hibernate的會話中獲得。當一個事務成功結束 時,HibernateTransactionManager會調用Transaction的commit()方法。相似的,一個失敗的事務將會調用 rollback()去回滾。
3.3 JDO事務
可能你不喜歡JDBC 和Hibernate,你可以選擇JDO去實現你的持久層。如果是這樣的話,你可以用JdoTransctionManager,你可以在你的上下文中如 下申明: <bean id="transactionManager" Class="org.springframework.orm.jdo.JdoTransactioinManager:>    <property name="persistenceManagerFactory">        <ref bean="persistenceManagerFactory"/>    </property> </bean> 爲了使用JdoTransctionManager,你需要組合一個具有persistenceManagerFactory 屬性的javax.jdo.PersistenceManagerFactory。其實,JdoTransactionManager和事務對象一起工 作,它是從JDO的持久管理器中獲得。一個成功的事務會調用commit()方法,而一個失敗的事務會調用rollback()方法。
3.4  JTA事務:
如果你上面所有的事務管理器都不符合你的需要,,或者你的應用是多資源交錯的事務,那麼.你可以使用JTA的事 務管理器 <bean id="transactionManager" class="org.springframewrok..      Transaction.jtaTransactionmanager">     <property name="transactionManager">          <value>java:/TransactionManager</valure>     </property> </bean> JtaTransactionManager 委託事務管理給JTA的實現.JTA定義了一系列的標準接口;能協調事務,應用和一個或多個數據源. 在這之中, transactioinManageName屬性指向JTA事務管理器. 這個事務管理器能在JNDI中找到. JtaTransactionManager, javax.transaction.UserTransaction和javax.transaction.TransactionManager對象 在一起工作. 所有的事務管理委託給它們完成. 一個成功的事務將會被提交,通過調用UserTransaction.commit()方法,相反,一個失敗的事務會調用UserTransaction 的rollback()方法.
現在.我們能找到合適的spring的事務管理器給我們的應用需要.並在配置文件中配置.現在就讓我們的事務管理器工作.在下面的章節中,我會介紹如何用申明式事務.

在Liferay中,涉及到的配置文件就是data-source-spring.xml 中的bean liferaySessionFactory,對應的class爲 com.liferay.portal.spring.hibernate.HibernateConfiguration

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章