简体   繁体   English

在动态数据源的情况下如何使用Spring Transaction Manager?

[英]How to use spring transaction manager in case of dynamic data source?

I have a scenario where datasource are dynamically created using Datasource Factory . 我有一个使用Datasource Factory动态创建Datasource Factory的方案。 So depending upon who accesses the system, DatasourceFacory(custom datasource factory) returns a corresponding datasource . 因此,根据谁访问系统,DatasourceFacory(自定义数据源工厂)将返回相应的datasource

Now, with this strategy, how do I maintain Spring Transaction? 现在,通过这种策略,如何维护Spring Transaction? Using a @Transactional annotation needs a fixed datasource with transaction manager configured. 使用@Transactional批注需要配置了事务管理器的固定数据源。

I would like to continue to use @Transactional in a service method and not worry about having to maintain transaction myself. 我想继续在服务方法中使用@Transactional ,而不用担心自己必须维护事务。

I would think that I would have to extend some spring class and inject the datasource when the system starts. 我认为我将不得不扩展一些spring类并在系统启动时注入数据源。

I am using Spring and JdbcTemplate in my project. 我在项目中使用Spring和JdbcTemplate No Hibernate. 没有冬眠。 Any help would be appreciated. 任何帮助,将不胜感激。

You have to use AbstractRoutingDataSource to link it with your transaction manager, this kind of datasource allows you use different datasources choosing them in runtime. 您必须使用AbstractRoutingDataSource将其与事务管理器链接,这种数据源允许您使用不同的数据源在运行时选择它们。

After that spring handles the transaction properly. 之后,Spring会正确处理事务。 You can check this kind of DataSource in the Spring Documentation. 您可以在Spring文档中检查这种数据源。

The other option is has different transactionManagers. 另一个选项是具有不同的transactionManagers。

For no-xml lovers 对于无XML爱好者

@Bean(name = "mysqldatasource1")
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(classname);
    dataSource.setUrl(url);
    dataSource.setUsername(usernmae);
    dataSource.setPassword(password);
    return dataSource;
}

@Bean(name = "mySqlJdbcTemplate1")
public JdbcTemplate jdbcTemplate() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource());
    return jdbcTemplate;
}

@Bean(name="mysqlTransaction1") 
@Autowired
DataSourceTransactionManager tm1(@Qualifier ("mysqldatasource") DataSource datasource) {
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;
}
@Bean(name = "mysqldatasource2")
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(classname);
    dataSource.setUrl(url);
    dataSource.setUsername(usernmae);
    dataSource.setPassword(password);
    return dataSource;
}

@Bean(name = "mySqlJdbcTemplate2")
public JdbcTemplate jdbcTemplate() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource());
    return jdbcTemplate;
}

@Bean(name="mysqlTransaction2") 
@Autowired
DataSourceTransactionManager tm1(@Qualifier ("mysqldatasource2") DataSource datasource) {
    DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
    return txm;
}

You can create multiple Transaction Managers with different datasources and then access them in @Transactional annotation as follows: 您可以使用不同的数据源创建多个事务管理器,然后在@Transactional批注中访问它们,如下所示:

 <bean id="txManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1"/>
    <qualifier value="txManager1"/>
 </bean>

Access it as: 以以下方式访问它:

 @Transactional("txManager1")

Similarly, 同样的,

 <bean id="txManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource2"/>
    <qualifier value="txManager2"/>
 </bean>

Access it as: 以以下方式访问它:

 @Transactional("txManager2")

As there can be only one annotation-driven TxManager, do not forget to remove this: 由于只能有一个由注释驱动的TxManager,因此请不要忘记删除以下内容:

<tx:annotation-driven transaction-manager="transactionManager" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM