Spring框架学习[Spring具体事务处理器的实现](十一)
ion Session session = getSessionFactory().getCurrentSession(); if (logger.isDebugEnabled()) { logger.debug("Found Hibernate-managed Session [" + SessionFactoryUtils.toString(session) + "] for Spring-managed transaction"); } //设置Hibernate事务对象已经存在指定的Session txObject.setExistingSession(session); } catch (HibernateException ex) { throw new DataAccessResourceFailureException( "Could not obtain Hibernate-managed Session for Spring-managed transaction", ex); } } //如果获取到的数据源不为null if (getDataSource() != null) { //将获取到的数据源和当前线程绑定 ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(getDataSource()); txObject.setConnectionHolder(conHolder); } return txObject; } //是否已存在事务 protected boolean isExistingTransaction(Object transaction) { HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;//根据事务对象是否存在Spring管理的事务,或者通过判断是否存在Hibernate //Session或者事务对象中有被Hibernate管理的事务 return (txObject.hasSpringManagedTransaction() || (this.hibernateManagedSession && txObject.hasHibernateManagedTransaction())); } //处理事务开始 protected void doBegin(Object transaction, TransactionDefinition definition) { //获取事务对象 HibernateTransactionObject txObject = (HibernateTransactionObject) transaction; //如果事务对象有ConnectionHolder,且事务对象的数据库连接不是事务同步的 if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) { throw new IllegalTransactionStateException( "Pre-bound JDBC Conn
ection found! HibernateTransactionManager does not support " + "running within DataSourceTransactionManager if told to manage the DataSource itself. " + "It is recommended to use a single HibernateTransactionManager for all transactions " + "on a single DataSource, no matter whether Hibernate or JDBC access."); } Session session = null; try { //如果事务对象的SessionHolder为null,或者事务对象Hibernate //Session是事务同步的 if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) { //获取Hibernate事务处理器中的实体拦截器 Interceptor entityInterceptor = getEntityInterceptor(); //获取Hibernate Session,如果实体拦截器不为null,则打开指定//实体拦截器的Session,如果实体拦截器为null,则打开新Session Session newSession = (entityInterceptor != null getSessionFactory().openSession(entityInterceptor) : getSessionFactory().openSession()); if (logger.isDebugEnabled()) { logger.debug("Opened new Session [" + SessionFactoryUtils.toString(newSession) + "] for Hibernate transaction"); } //将获取的Hibernate Session设置到事务对象中 txObject.setSession(newSession); }//如果Hibernate事务处理器中的SessionHolder不为null,则//获取SessionHolder中已有的Hibernate Session session = txObject.getSessionHolder().getSession(); //允许为JDBC连接改变事务设置 if (this.prepareConnection && isSameConnectionForEntireSession(session)) { if (logger.isDebugEnabled()) { logger.debug( "Preparing JDBC Connection of Hibernate Session [" + SessionFactoryUtils.toString(session) + "]"); } //获取Session连接 Connection con = session.connection(); //获取事务的隔离级别 Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definiti