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

2014-11-24 03:00:34 · 作者: · 浏览: 15
(), 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) { //获取Hibernate事务对象 HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction(); if (status.isDebug()) { logger.debug("Rolling back Hibernate transaction on Session ["+SessionFactoryUtils.toString(txObject.getSessionHolder().getSession()) + "]"); } try { //通过Hibernate事务执行回滚操作 txObject.getSessionHolder().getTransaction().rollback(); } catch (org.hibernate.TransactionException ex) { throw new TransactionSystemException("Could not roll back Hibernate transaction", ex); } catch (HibernateException ex) { throw convertHibernateAccessException(ex); } finally { if (!txObject.isNewSession() && !this.hibernateManagedSession) { //清除事务对象中的Hibernate Session txObject.getSessionHolder().getSession().clear(); } } } …… }