DataSourceTransactionManager 总结

DataSourceTransactionManager 总结

  • 1.Spring框架配置事务
    • 1.1基于schema的自动代理
    • 1.2使用@transactional注解
  • 2.SpringBoot框架配置事务
    • 2.1@Transactional注解
      • 2.1.1SpringBoot自动装配事务管理器
    • 2.2SpringBoot多数据源配置
  • 2.2.1给数据源指定事务管理器
      • 2.2.2注解上指定事务管理器

1.Spring框架配置事务

1.1基于schema的自动代理



    
    

    
    

    
    
        
        
        
        
    
    
    
        
            
            
            
            
            
            
        
    

查看tx:advice标签,可以追踪到TransactionInterceptor类,其实现了MethodInterceptor接口,会在目标方法前后实施增强(即使用txManager,在方法执行前关闭Connection对象自动提交,方法执行完成后执行Connection对象提交方法,异常时执行Connection对象回滚方法)。

1.2使用@transactional注解



    
    

    
    

    
    

2.SpringBoot框架配置事务

2.1@Transactional注解

2.1.1SpringBoot自动装配事务管理器

package org.springframework.boot.autoconfigure.jdbc;

import javax.sql.DataSource;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.TransactionManager;

@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnClass({JdbcTemplate.class, TransactionManager.class})
@AutoConfigureOrder(2147483647)
@EnableConfigurationProperties({DataSourceProperties.class})
public class DataSourceTransactionManagerAutoConfiguration {
	public DataSourceTransactionManagerAutoConfiguration() {
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnSingleCandidate(DataSource.class)
    static class JdbcTransactionManagerConfiguration {
        JdbcTransactionManagerConfiguration() {
        }

        @Bean
        @ConditionalOnMissingBean({TransactionManager.class})
        DataSourceTransactionManager transactionManager(Environment environment, DataSource dataSource, ObjectProvider transactionManagerCustomizers) {
            DataSourceTransactionManager transactionManager = this.createTransactionManager(environment, dataSource);
            transactionManagerCustomizers.ifAvailable((customizers) -> {
                customizers.customize(transactionManager);
            });
            return transactionManager;
        }

        private DataSourceTransactionManager createTransactionManager(Environment environment, DataSource dataSource) {
            return (DataSourceTransactionManager)((Boolean)environment.getProperty("spring.dao.exceptiontranslation.enabled", Boolean.class, Boolean.TRUE) ? new JdbcTransactionManager(dataSource) : new DataSourceTransactionManager(dataSource));
        }
    }
}

2.2SpringBoot多数据源配置

2.2.1给数据源指定事务管理器

   @Bean("db1TxManager")
   public DataSourceTransactionManager db1TxManager(@Qualifier("db1DataSource") DataSource dataSource ){
       DataSourceTransactionManager txManager = new DataSourceTransactionManager();
       txManager.setDataSource(dataSource);
       return txManager;
   }
   @Bean("db2TxManager")
   public DataSourceTransactionManager db2TxManager(@Qualifier("db2DataSource") DataSource dataSource ){
       DataSourceTransactionManager txManager = new DataSourceTransactionManager();
       txManager.setDataSource(dataSource);
       return txManager;
   }

2.2.2注解上指定事务管理器

@Service("baseService1")
@Transactional("db1TxManager")
public class BaseService1 {
...
}

你可能感兴趣的:(java,spring,spring,elasticsearch,mysql)