Spring源代码解析(八):Spring驱动Hibernate的实现
Spring源代码解析(八):Spring驱动Hibernate的实现
<script type="text/JavaScript"> alimama_pid="mm_10312158_369697_2344403"; alimama_titlecolor="3F4E5A"; alimama_descolor ="000000"; alimama_bgcolor="FFFFFF"; alimama_bordercolor="FFFFFF"; alimama_linkcolor="003366"; alimama_bottomcolor="FFFFFF"; alimama_anglesize="10"; alimama_bgpic="0"; alimama_icon="1"; alimama_sizecode="32"; alimama_width=250; alimama_height=300; alimama_type=2; </script><script src="http://a.alimama.cn/inf.js" type="text/javascript"> </script>
O/R工具出现之后,简化了许多复杂的信息持久化的开发。Spring应用开发者可以通过Spring提供的O/R方案更方便的使用各种持久化工具,比如Hibernate;下面我们就Spring+Hibernate中的Spring实现做一个简单的剖析。 Spring对Hinberanate的配置是通过LocalSessionFactoryBean来完成的,这是一个工厂Bean的实现,在基类AbstractSessionFactoryBean中:
- /**
- * 这是FactoryBean需要实现的接口方法,直接取得当前的sessionFactory的值
- */
- public Object getObject() {
- return this.sessionFactory;
- }
- public void afterPropertiesSet() throws Exception {
- //这个buildSessionFactory是通过配置信息得到SessionFactory的地方
- SessionFactory rawSf = buildSessionFactory();
- //这里使用了Proxy方法插入对getCurrentSession的拦截,得到和事务相关的session
- this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf);
- }
- 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;
- }
- protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {
- return config.buildSessionFactory();
- }
- //这里先根据当前的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 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;
- }
- 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());
- }
- }
- }
- }
- 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());
- }
- }
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 ,嘿嘿^_^