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

2014-11-24 03:00:34 · 作者: · 浏览: 13
n(FlushMode.COMMIT)) { session.setFlushMode(FlushMode.AUTO); //为事务对象设置刷新模式 txObject.getSessionHolder().setPreviousFlushMode(flushMode); } } Transaction hibTx; //获取事务超时时长 int timeout = determineTimeout(definition); //如果事务配置的超时时长不是事务默认超时时长 if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) { //获取Hibernate Session事务 hibTx = session.getTransaction(); //为事务对象设置超时时长 hibTx.setTimeout(timeout); //开启事务 hibTx.begin(); } //如果事务配置的超时时长是默认超时时长 else { //通过Hibernate Session直接开启事务 hibTx = session.beginTransaction(); } //把事务设置到事务对象的SessionHolder中,并且线程绑定 txObject.getSessionHolder().setTransaction(hibTx); //如果数据源不为null,即设置了数据源 if (getDataSource() != null) { //使用Hibernate Session打开数据库连接 Connection con = session.connection(); //创建ConnectionHolder ConnectionHolder conHolder = new ConnectionHolder(con); //设置超时时长 if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) { conHolder.setTimeoutInSeconds(timeout); } if (logger.isDebugEnabled()) { logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]"); } //将数据源和JDBC ConnectionHolder绑定到当前线程 TransactionSynchronizationManager.bindResource(getDataSource(), conHolder); //将创建的JDBC ConnectionHolder设置到事务对象中 txObject.setConnectionHolder(conHolder); } //如果事务对象中的SessionHolder是新的 if (txObject.isNewSessionHolder()) { //当SessionHolder和当前线程绑定起来 TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder()); } //设置事务对象中的SessionHolder是事务同步的 txObject.getSessionHolder().setSynchronizedWithTransaction(true); } /
/事务开启过程中异常处理 catch (Exception ex) { if (txObject.isNewSession()) { try { //如果Session的事务上激活的,回滚Session的事务 if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } 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) { TransactionSynchronizationManager.bindResource(getDataSource