Spring框架学习[Spring具体事务处理器的实现](十三)

2014-11-24 03:00:34 · 作者: · 浏览: 25
} } catch (Throwable ex2) { logger.debug("Could not rollback Session after failed transaction begin", ex); } finally { //关闭Session SessionFactoryUtils.closeSession(session); } } throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex); } } //事务挂起 protected Object doSuspend(Object transaction) { HibernateTransactionObject txObject = (HibernateTransactionObject) transaction; //把当前的SessionHolder从线程中和事务对象中释放 txObject.setSessionHolder(null); //解析SessionHolder和线程的绑定 SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory()); txObject.setConnectionHolder(null); ConnectionHolder connectionHolder = null; //解除数据源和线程的绑定 if (getDataSource() != null) { connectionHolder = (ConnectionHolder) TransactionSynchronizationManager.unbindResource(getDataSource()); } return new SuspendedResourcesHolder(sessionHolder, connectionHolder); } //事务恢复 protected void doResume(Object transaction, Object suspendedResources) { SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources; //如果事务管理器中有SessionFactory if (TransactionSynchronizationManager.hasResource(getSessionFactory())) { //解除SessionFactory和当前线程的绑定 TransactionSynchronizationManager.unbindResource(getSessionFactory()); } //如果事务管理器中没有SessionFactory,则将Session和当前线程绑定 TransactionSynchronizationManager.bindResource(getSessionFactory(), resourcesHolder.getSessionHolder()); if (getDataSource() != null) { TransactionSynchronizationMan
ager.bindResource(getDataSource(), resourcesHolder.getConnectionHolder()); } } //准备提交 protected void prepareForCommit(DefaultTransactionStatus status) { //如果事务配置为FlushBeforeCommit,并且是新事务 if (this.earlyFlushBeforeCommit && status.isNewTransaction()) { //获取事务对象 HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction(); //回去事务对象中的Session Session session = txObject.getSessionHolder().getSession(); //如果Session的刷新模式不低于COMMIT if (!session.getFlushMode().lessThan(FlushMode.COMMIT)) { logger.debug("Performing an early flush for Hibernate transaction"); try { //刷新Session session.flush(); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } finally { //把Session的刷新模式设置为MANUAL session.setFlushMode(FlushMode.MANUAL); } } } } //提交处理 protected void doCommit(DefaultTransactionStatus status) { //获取当前的Hibernate事务对象 HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Committing Hibernate transaction on Session [" +SessionFactoryUtils.toString(txObject.getSessionHolder().getSession()) + "]"); } try { //通过Hibernate事务完成提交 txObject.getSessionHolder().getTransaction().commit(); } catch (org.hibernate.TransactionException ex) { throw new TransactionSystemException("Could not commit Hibernate transaction", ex); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } } //回滚处理 protected void doRollback(DefaultTransactionStatus status) {