接着我们重点再回头看一下createTransactionIfNecessary方法里的这一句:
txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));
接着我们就应该去看看这个getTransaction方法了,假设我们是使用hibernate3,其他类似。看getTransaction之前我们来看一下这两类和一个接口
接口PlatformTransactionManager
抽象类public abstract class AbstractPlatformTransactionManager implements PlatformTransactionManager
类public class HibernateTransactionManager extends AbstractPlatformTransactionManager,很明显,这里有一个方法模板模式。
那我们看一下AbstractPlatformTransactionManager中得getTransaction方法:
代码
- public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
- Object transaction = doGetTransaction();//抽象方法,也需要子类实现,这个方法同样很重要
- // Cache debug flag to avoid repeated checks.
- boolean debugEnabled = logger.isDebugEnabled();
- if (debugEnabled) {
- logger.debug("Using transaction object [" + transaction + "]");
- }
- if (definition == null) {
- // Use defaults if no transaction definition given.
- definition = new DefaultTransactionDefinition();
- }
- 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 behave.
- if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
- throw new IllegalTransactionStateException(
- "Transaction propagation 'mandatory' but no existing transaction found");
- }
- else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||
- definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
- definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
- if (debugEnabled) {
- logger.debug("Creating new transaction with name [" + definition.getName() + "]");
- }
- doBegin(transaction, definition);
- boolean newSynchronization = (this.transactionSynchronization != SYNCHRONIZATION_NEVER);
- return newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);
- }
- else {
- // Create "empty" transaction: no actual transaction, but potentially synchronization.
- boolean newSynchronization = (this.transactionSynchronization == SYNCHRONIZATION_ALWAYS);
- return newTransactionStatus(definition, null, false, newSynchronization, debugEnabled, null);
- }
- }
具体依赖于抽象,这个是对方法模板模式的一个概括。),前面讲到我们假设是使用hibernate,那么就看看HibernateTransactionManager这个类吧,doBegin里的参数1,transaction其实是HibernateTransactionObject的一个实例,这个实例里主要存放的就是sessionholder,sessionholder里存放的就是开始事务的session和transaction对象,如果之前没有sessionholder存放到线程中,那么这个HibernateTransactionObject的实例的属性其实是空的,这一点可以在doBegin方法的实现中看出来
代码
- protected void doBegin(Object transaction, TransactionDefinition definition) {
- if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) {
- 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 {
- HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
- if (txObject.getSessionHolder() == null) {
- Interceptor entityInterceptor = getEntityInterceptor();
- Session newSession = (entityInterceptor != null ?
- getSessionFactory().openSession(entityInterceptor) : getSessionFactory().openSession());
- if (logger.isDebugEnabled()) {
- logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
- }
- txObject.setSessionHolder(new SessionHolder(newSession), true);
代码