Spring transaction processing multiple data sources and more

Due to the project business you need to connect two databases, and the need transaction support, reference information on the Internet have tried to achieve spring interfaces to automatically switch the data source, but the transaction is only one data source can be used, so the following way configuration, scan all map files by annotation class distinction over which data source is used to determine by configuring @Transactional (value = "name Services") in which transaction service method used.
A data source configured

1, adding the two data sources

<bean id="ADataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306?useUnicode=true&characterEncoding=UTF-8" />
    <property name="username" value="username" />
    <property name="password" value="password" />
    <!-- 初始化连接大小 -->
    <property name="initialSize" value="1"></property>
    <!-- 连接池最大数量 -->
    <property name="maxActive" value="20"></property>
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="1"></property>
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="30000"></property>
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="300000" />

    <property name="validationQuery" value="SELECT 'x'" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <!-- 配置监控统计拦截的filters -->
    <!-- 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: -->
    <!-- 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall -->
    <property name="filters" value="stat" />
</bean>
<bean id="BDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306?useUnicode=true&characterEncoding=UTF-8" />
    <property name="username" value="username" />
    <property name="password" value="password" />
    <!-- 初始化连接大小 -->
    <property name="initialSize" value="1"></property>
    <!-- 连接池最大数量 -->
    <property name="maxActive" value="20"></property>
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="1"></property>
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="30000"></property>
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="300000" />

    <property name="validationQuery" value="show status like '%Service_Status%';" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <!-- 配置监控统计拦截的filters -->
    <!-- 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: -->
    <!-- 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall -->
    <property name="filters" value="stat" />
</bean>

2, the configuration of two different plants point datasource

<bean id="ASqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="ADataSource" />
</bean>
<bean id="BSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="BDataSource" />
</bean>

3, the corresponding mapper scan configuration, wherein the configuration attributes annotationClass custom class for which annotation used to distinguish mapper A data source, which data source B using the mapper, and configure the corresponding annotation mapper interface class corresponding to the above

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- [basePackage]属性指定的包及其子包下面的Mapper接口都将被搜索到 -->
    <property name="basePackage" value="com.yww.distribution.report.**.dao.**" />
    <property name="annotationClass" value="com.yww.distribution.report.base.datasource.ADataSource"></property>
    <property name="sqlSessionFactoryBeanName" value="ASqlSessionFactory"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- [basePackage]属性指定的包及其子包下面的Mapper接口都将被搜索到 -->
    <property name="basePackage" value="com.yww.distribution.report.**.dao.**" />
    <property name="annotationClass" value="com.yww.distribution.report.base.datasource.ADataSource"></property>
    <property name="sqlSessionFactoryBeanName" value="BSqlSessionFactory"></property>
</bean>

4, annotation type of content

@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ADataSource {
}

@Target (ElementType.TYPE {})
@Retention (RetentionPolicy.RUNTIME)
public @interface BDataSource {
}
Second, the configuration transaction

1, adding two transactions, each transaction directed to different data sources

 <!-- 开启事务控制的注解支持 -->
<tx:annotation-driven />

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="ATransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="ADataSource" />
</bean>
<bean id="BTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="BDataSource" />
</bean>

2, multi-point data source configuration has been completed, only need to add annotations on the various methods used in the service can be a

@Transactional(“ADataSource”)
public void add() {
//TODO
}

References: https://blog.csdn.net/finnson/article/details/81061834

Guess you like

Origin blog.csdn.net/qqxm1/article/details/92760507