Spring框架学习[Spring具体事务处理器的实现](八)
ngManagedTransaction() || (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 Connection 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, definition); //设置事务对象的事务隔离级别 txObject.setPreviousIsolationLevel(previousIsolationLevel); } //不允许为JDBC连接改成事务设置 else { //如果事务隔离级别不是默认事务隔离级别 if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { throw new InvalidIsolationLevelException( "HibernateTransactionManager is not allowed to support custom isolation levels: " + "make sure that its 'prepareConnection' flag is on (the default) and that the " + "Hibernate connection release mode is set to 'on_close' (SpringTransactionFactory's default). " + "Make sure that your LocalSessionFactoryBean actually uses SpringTransactionFactory: Your " + "Hibernate properties should *not* include a 'hibernate.transaction.factory_class' property!"); } if (logger.isDebugEnabled()) { logger.debug( "Not preparing JDBC Connection of Hibernate Session [" + SessionFactoryUtils.toString(session) + "]"); } } //如果事务是只读,且事务对象是新的Hibernate Session if (definition.isReadOnly() && txObject.isNewSession()) { //设置Hibernate Session刷新模式为手动 session.setFlushMode(FlushMode.MANUAL); } //如果事务是非只读的,且事务对象不是新Hibernate Session if (!definition.isReadOnly() && !txObject.isNewSession()) { //或者Hibernate的刷新模式 FlushMode flushMode = session.getFlushMode(); //设置Session的刷新模式 if (flushMode.lessTha