日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Atomikos Transactions Essentials实现多数据源JTA分布式事务--转载

發布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Atomikos Transactions Essentials实现多数据源JTA分布式事务--转载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文:http://www.ite/topic/122700

9.17 update:使用NonXADataSourceBean. Mysql在5.0版本和Connecter/J5.0版本后提供了XADatasource支持,如果使用了支持XADatasouce版本,可以參考2樓補充.?

最近做的project中遇到要將數據庫中的表分布到兩臺不同的服務器上的Mysql5.0中,project主要使用spring+ibatis。因此需要JTA的支持,但是tomcat不支持,所以就搜索開源的JTA實現。
最開始使用的是JOTM,但是使用中不能自動rollback,無論什么情況都commit。然后看到infoq上一篇文章提到Atomikos Transactions Essentials,Atomikos Transactions Essentials 3.0是Atomikos 開發的核心事務引擎,支持JDBC 以及JMS 的JTA/XA 事務。易于部署,輕量級,同時支持JDBC 以及JMS 。
Atomikos Transactions Essentials現在的版本是3.1.7,可以在http://www.atomikos.com/Main/TransactionsEssentialsDownloadForm?下載,在發布包里的examples文件夾下面有些例子,非常實用,我在使用中參考里面的例子很容易配置成功。先將發布包里面dist目錄下的atomikos-util.jar,transactions.jar,transactions-api.jar,transactions-jta.jar copy到項目lib里面, 如果使用hibernate則需要將另外兩個hibernate相關的jar頁copy到項目里面,spring配置文件如下:

xml 代碼
  • <!--?第一個數據庫?-->???
  • <?bean???id?=?"dataSource"???class?=?"com.atomikos.jdbc.SimpleDataSourceBean"???init-method?=?"init"??destroy-method?=?"close"?>???
  • ????<?property???name?=?"uniqueResourceName"?>???
  • ????????<?value?>?mysql/main?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"xaDataSourceClassName"?>???
  • ????????<!--?使用Mysql?XADataSource(mysql>=5.0,?Connector/J>=5.0才可以支持XADatasource)-->???
  • ????????<?value?>?com.mysql.jdbc.jdbc2.optional.MysqlXADataSource?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"xaDataSourceProperties"?>???
  • ????????<?value?>?URL?=${jdbc.url};?user?=${jdbc.username};?password?=${jdbc.password}?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"exclusiveConnectionMode"?>???
  • ????????<?value?>?true?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"connectionPoolSize"?>???
  • ????????<?value?>?3?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"validatingQuery"?>???
  • ????????<?value?>?SELECT?1?</?value?>???
  • ????</?property?>???
  • </?bean?>???
  • <!--?第二個數據庫?-->???
  • <?bean???id?=?"dataSourceB"???class?=?"com.atomikos.jdbc.SimpleDataSourceBean"???init-method?=?"init"???destroy-method?=?"close"?>???
  • ????<?property???name?=?"uniqueResourceName"?>???
  • ????????<?value?>?mysql/news?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"xaDataSourceClassName"?>???
  • ????????<!--?使用Mysql?XADataSource(mysql>=5.0,?Connector/J>=5.0才可以支持XADatasource)-->???
  • ????????<?value?>?com.mysql.jdbc.jdbc2.optional.MysqlXADataSource?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"xaDataSourceProperties"?>???
  • ????????<?value?>?URL?=${jdbc.url.b};?user?=${jdbc.username.b};?password?=${jdbc.password.b}?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"exclusiveConnectionMode"?>???
  • ????????<?value?>?true?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"connectionPoolSize"?>???
  • ????????<?value?>?3?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"validatingQuery"?>???
  • ????????<?value?>?SELECT?1?</?value?>???
  • ????</?property?>???
  • </?bean?>???
  • ??
  • ??
  • <?bean???id?=?"lobHandler"???class?=?"org.springframework.jdbc.support.lob.DefaultLobHandler"???/>???
  • ??
  • <!--?第一個數據庫的sqlMapClient?-->???
  • <?bean???id?=?"sqlMapClient"???class?=?"org.springframework.orm.ibatis.SqlMapClientFactoryBean"?>???
  • ????<?property???name?=?"configLocation"?>???
  • ????????<!--?包含第一個數據庫表的map?-->???
  • ????????<?value?>?classpath:/sqlmap-config.xml?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"dataSource"???ref?=?"dataSource"???/>???
  • ????<?property???name?=?"lobHandler"???ref?=?"lobHandler"???/>???
  • </?bean?>???
  • <!--?第二個數據庫的sqlMapClient?-->???
  • <?bean???id?=?"sqlMapClientB"???class?=?"org.springframework.orm.ibatis.SqlMapClientFactoryBean"?>???
  • ????<?property???name?=?"configLocation"?>???
  • ????????<!--?包含第一個數據庫表的map?-->???
  • ????????<?value?>?classpath:/sqlmap-configb.xml?</?value?>???
  • ????</?property?>???
  • ????<?property???name?=?"dataSource"???ref?=?"dataSourceB"???/>???
  • ????<?property???name?=?"lobHandler"???ref?=?"lobHandler"???/>???
  • </?bean?>???
  • ??
  • <!--?Construct?Atomikos?UserTransactionManager,?needed?to?configure?Spring?-->???
  • <?bean???id?=?"atomikosTransactionManager"???class?=?"com.atomikos.icatch.jta.UserTransactionManager"???init-method?=?"init"???
  • ????destroy-method?=?"close"?>???
  • ????<!--??when?close?is?called,?should?we?force?transactions?to?terminate?or?not??-->???
  • ????<?property???name?=?"forceShutdown"?>???
  • ????????<?value?>?true?</?value?>???
  • ????</?property?>???
  • </?bean?>???
  • ??
  • <!--?Also?use?Atomikos?UserTransactionImp,?needed?to?configure?Spring??-->???
  • <?bean???id?=?"atomikosUserTransaction"???class?=?"com.atomikos.icatch.jta.UserTransactionImp"?>???
  • ????<?property???name?=?"transactionTimeout"???value?=?"240"???/>???
  • </?bean?>???
  • ??
  • <!--?Configure?the?Spring?framework?to?use?JTA?transactions?from?Atomikos?-->???
  • <?bean???id?=?"transactionManager"???class?=?"org.springframework.transaction.jta.JtaTransactionManager"?>???
  • ????<?property???name?=?"transactionManager"?>???
  • ????????<?ref???bean?=?"atomikosTransactionManager"???/>???
  • ????</?property?>???
  • ????<?property???name?=?"userTransaction"?>???
  • ????????<?ref???bean?=?"atomikosUserTransaction"???/>???
  • ????</?property?>???
  • </?bean?>???


  • 事務的配置, 使用了spring2.0的語法,所以將namesapce也帖出來了.

    xml 代碼
  • <??xml???version?=?"1.0"???encoding?=?"UTF-8"??>???
  • <?beans???xmlns?=?"http://www.springframework.org/schema/beans"???
  • ???????xmlns:xsi?=?"http://www.w3.org/2001/XMLSchema-instance"???
  • ???????xmlns:aop?=?"http://www.springframework.org/schema/aop"???
  • ???????xmlns:tx?=?"http://www.springframework.org/schema/tx"???
  • ???????xsi:schemaLocation?="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd??
  • ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd??
  • ???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"??
  • ???????default-autowire?=?"byName"???default-lazy-init?=?"true"?>???
  • ????<!--?支持?@AspectJ?標記-->???
  • ????<?aop:aspectj-autoproxy?/>???
  • ??
  • ????<?aop:config???proxy-target-class?=?"true"?>???
  • ????????<?aop:advisor???pointcut?=?"execution(*?*Facade.*(..))"???advice-ref?=?"txAdvice"?/>???
  • ????????<?aop:advisor???pointcut?=?"execution(*?*Manager.*(..))"???advice-ref?=?"txAdvice"?/>???
  • ????</?aop:config?>???
  • ??
  • ????<?tx:advice???id?=?"txAdvice"?>???
  • ????????<?tx:attributes?>???
  • ????????????<?tx:method???name?=?"get*"???read-only?=?"true"?/>???
  • ????????????<?tx:method???name?=?"find*"???read-only?=?"true"?/>???
  • ????????????<?tx:method???name?=?"has*"???read-only?=?"true"?/>???
  • ????????????<?tx:method???name?=?"locate*"???read-only?=?"true"?/>???
  • ????????????<?tx:method???name?=?"*"?/>???
  • ????????</?tx:attributes?>???
  • ????</?tx:advice?>???
  • </?beans?>???

  • 這樣配置以后就可以使用分布式事務,測試中出現異常時事務也自動提交。和JOTM相比Atomikos Transactions Essentials更加穩定,它原來是商業項目,現在開源了。象mysql一樣賣服務支持的。而且論壇頁比較活躍,有問題很快可以解決。

    轉載于:https://www.cnblogs.com/davidwang456/p/4353209.html

    總結

    以上是生活随笔為你收集整理的使用Atomikos Transactions Essentials实现多数据源JTA分布式事务--转载的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。