在运行时在多个事务管理器之间进行选择 [英] Choose between muliple transaction managers at runtime

查看:58
本文介绍了在运行时在多个事务管理器之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个客户端使用部署在 tomcat 中的相同基于 Spring 的 REST 应用程序.根据客户,我需要在数据源和事务管理器之间进行选择.如何在运行时选择使用哪个事务管理器?

I have 2 clients using same Spring based REST Application deployed in tomcat. Depending on client I need to choose between data sources and transaction manager. How do I choose at runtime, which transaction manager to use?

    <bean id="First_dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="url" value="${First_jdbc.url}" />
            <property name="driverClassName" value="${First_jdbc.driverClassName}" />
            <property name="username" value="${First_jdbc.username}" />
            <property name="password" value="${First_jdbc.password}" />
            <property name="removeAbandoned" value="true" />
            <property name="initialSize" value="20" />
            <property name="maxActive" value="30" />
            <!-- <property name="defaultAutoCommit" value="false" /> -->
   </bean>

    <bean id="Second_dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="url" value="${Second_jdbc.url}" />
            <property name="driverClassName" value="${Second_jdbc.driverClassName}" />
            <property name="username" value="${Second_jdbc.username}" />
            <property name="password" value="${Second_jdbc.password}" />
            <property name="removeAbandoned" value="true" />
            <property name="initialSize" value="20" />
            <property name="maxActive" value="30" />
            <!-- <property name="defaultAutoCommit" value="false" /> -->
   </bean>

<bean id="First_TransactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        scope="singleton">
        <property name="dataSource" ref="First_dataSource" />
        <qualifier value="SC_TM"></qualifier>
</bean>

<bean id="Second_TransactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          scope="singleton">
          <property name="dataSource" ref="Second_dataSource" />
          <qualifier value="Second_TM"></qualifier>
</bean>   

在代码中如何在运行时选择@Transactional("????").如果 org.springframework.jdbc.datasource.DataSourceTransactionManager 无法实现,还有其他方法吗?

In code how do choose @Transactional("????") at run time. If it is not possible with org.springframework.jdbc.datasource.DataSourceTransactionManager is there any other way of doing it?

推荐答案

考虑使用 Spring 提供的 AbstractRoutingDataSource 而不是在多个事务管理器之间进行选择.这将是一个更清洁的解决方案.

Consider using the Spring provided AbstractRoutingDataSource instead of going down the path of choosing between multiple transaction managers. It will be a much cleaner solution.

在这里查看我对类似问题的回答:
https://stackoverflow.com/a/44167079/2200690

See my answer to a similar question here :
https://stackoverflow.com/a/44167079/2200690

这篇关于在运行时在多个事务管理器之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆