接上节事务执行流程,这是第3点的解析。
创立事务次要两局部:

  1. 获取事务状态
  2. 构建事务信息

获取事务状态
代码如下:

@Override    public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {    //1.获取事务    Object transaction = doGetTransaction();    // Cache debug flag to avoid repeated checks.    boolean debugEnabled = logger.isDebugEnabled();    if (definition == null) {        // Use defaults if no transaction definition given.        definition = new DefaultTransactionDefinition();    }    //判断以后线程是否存在事务,判断根据为以后线程记录连贯不为空且连贯中的(connectionHolder)中的transactionActive属性不为空    if (isExistingTransaction(transaction)) {        // Existing transaction found -> check propagation behavior to find out how to behave.        return handleExistingTransaction(definition, transaction, debugEnabled);    }    // Check definition settings for new transaction.    //事务超时设置验证    if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {        throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());    }    // No existing transaction found -> check propagation behavior to find out how to proceed.    //如果以后线程不存在事务,然而@Transactional却申明事务为PROPAGATION_MANDATORY抛出异样    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {        throw new IllegalTransactionStateException(                "No existing transaction found for transaction marked with propagation 'mandatory'");    }    //如果以后线程不存在事务,PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED都得创立事务    else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||            definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||            definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {        //空挂起        SuspendedResourcesHolder suspendedResources = suspend(null);        if (debugEnabled) {            logger.debug("Creating new transaction with name [" + definition.getName() + "]: " + definition);        }        try {            //默认返回true            boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);            //构建事务状态            DefaultTransactionStatus status = newTransactionStatus(                    definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);            //结构transaction、包含设置connectionHolder、隔离级别、timeout            //如果是新事务,绑定到以后线程            doBegin(transaction, definition);            //新事务同步设置,针对以后线程            prepareSynchronization(status, definition);            return status;        }        catch (RuntimeException ex) {            resume(null, suspendedResources);            throw ex;        }        catch (Error err) {            resume(null, suspendedResources);            throw err;        }    }    else {        // Create "empty" transaction: no actual transaction, but potentially synchronization.        if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) {            logger.warn("Custom isolation level specified but no actual transaction initiated; " +                    "isolation level will effectively be ignored: " + definition);        }        //申明事务是PROPAGATION_SUPPORTS        boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);        return prepareTransactionStatus(definition, null, true, newSynchronization, debugEnabled, null);    }}

构建事务信息

  1. 获取事务,创立对应的事务实例,这里应用的是DataSourceTransactionManager中的doGetTransaction办法,创立基于JDBC的事务实例,如果以后线程中存在对于dataSoruce的连贯,那么间接应用。这里有一个对保留点的设置,是否开启容许保留点取决于是否设置了容许嵌入式事务。DataSourceTransactionManager默认是开启的。
  2. 如果当先线程存在事务,则转向嵌套的事务处理。是否存在事务在DataSourceTransactionManager的isExistingTransaction办法中
  3. 事务超时设置验证
  4. 事务PropagationBehavior属性的设置验证
  5. 构建DefaultTransactionStatus。
  6. 欠缺transaction,包含设置connectionHolder、隔离级别、timeout,如果是新事务,绑定到以后线程
  7. 将事务信息记录在以后线程中