atomikos配置多個資料來源JTA管理事務

來源:互聯網
上載者:User

標籤:

配置兩個dataSource

<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean">        <property name="uniqueResourceName"><value>yw</value></property>        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>          <property name="xaProperties">              <props>                  <prop key="URL">${jdbc.url}</prop>                  <prop key="user">${jdbc.username}</prop>                  <prop key="password">${jdbc.password}</prop>            </props>          </property>          <property name="poolSize"><value>1</value></property>            <property name="minPoolSize" value="2" />          <property name="maxPoolSize" value="10" />      </bean>        <!-- 測試 -->    <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean">        <property name="uniqueResourceName"><value>ds2</value></property>        <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>          <property name="xaProperties">              <props>                  <prop key="URL">${jdbc.url2}</prop>                  <prop key="user">${jdbc.username2}</prop>                  <prop key="password">${jdbc.password2}</prop>            </props>          </property>          <property name="poolSize"><value>1</value></property>            <property name="minPoolSize" value="2" />          <property name="maxPoolSize" value="10" />      </bean>

jdbctemplate配置

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource">            <ref local="dataSource" />        </property>    </bean>    <!-- 測試資料來源 -->    <bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource">            <ref local="dataSource2" />        </property>    </bean>    <bean id="baseDao" class="com.szy.base.BaseDao">        <property name="jdbcTemplate">  <ref bean="jdbcTemplate" />         </property>    </bean>

第二個資料來源可以用spring註解注入。

public class BaseDao2 {    @Resource(name = "jdbcTemplate2")     protected JdbcTemplate jdbcTemplate2=null;     private int total=0;    public void setTotal(int total) {        this.total = total; } }

配置交易管理

        <!-- 開始配置JTA事務  -->    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">        <property name="forceShutdown"><value>true</value></property>    </bean>        <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">        <property name="transactionTimeout"><value>300</value></property>    </bean>        <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">        <property name="transactionManager">            <ref local="atomikosTransactionManager"/>        </property>        <property name="userTransaction">            <ref local="atomikosUserTransaction"/>        </property>    </bean>        <!-- 使用annotation定義事務 -->    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

這樣就完成了。

僅以記錄遇到過的問題。

atomikos配置多個資料來源JTA管理事務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.