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

  1. <!--第一个数据库-->
  2. < bean id = "dataSource" class = "com.atomikos.jdbc.SimpleDataSourceBean" init-method = "init" destroy-method = "close" >
  3. < property name = "uniqueResourceName" >
  4. < value > mysql/main </ value >
  5. </ property >
  6. < property name = "xaDataSourceClassName" >
  7. <!--使用MysqlXADataSource(mysql>=5.0,Connector/J>=5.0才可以支持XADatasource)-->
  8. < value > com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </ value >
  9. </ property >
  10. < property name = "xaDataSourceProperties" >
  11. < value > URL =${jdbc.url}; user =${jdbc.username}; password =${jdbc.password} </ value >
  12. </ property >
  13. < property name = "exclusiveConnectionMode" >
  14. < value > true </ value >
  15. </ property >
  16. < property name = "connectionPoolSize" >
  17. < value > 3 </ value >
  18. </ property >
  19. < property name = "validatingQuery" >
  20. < value > SELECT1 </ value >
  21. </ property >
  22. </ bean >
  23. <!--第二个数据库-->
  24. < bean id = "dataSourceB" class = "com.atomikos.jdbc.SimpleDataSourceBean" init-method = "init" destroy-method = "close" >
  25. < property name = "uniqueResourceName" >
  26. < value > mysql/news </ value >
  27. </ property >
  28. < property name = "xaDataSourceClassName" >
  29. <!--使用MysqlXADataSource(mysql>=5.0,Connector/J>=5.0才可以支持XADatasource)-->
  30. < value > com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </ value >
  31. </ property >
  32. < property name = "xaDataSourceProperties" >
  33. < value > URL =${jdbc.url.b}; user =${jdbc.username.b}; password =${jdbc.password.b} </ value >
  34. </ property >
  35. < property name = "exclusiveConnectionMode" >
  36. < value > true </ value >
  37. </ property >
  38. < property name = "connectionPoolSize" >
  39. < value > 3 </ value >
  40. </ property >
  41. < property name = "validatingQuery" >
  42. < value > SELECT1 </ value >
  43. </ property >
  44. </ bean >
  45. < bean id = "lobHandler" class = "org.springframework.jdbc.support.lob.DefaultLobHandler" />
  46. <!--第一个数据库的sqlMapClient-->
  47. < bean id = "sqlMapClient" class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
  48. < property name = "configLocation" >
  49. <!--包含第一个数据库表的map-->
  50. < value > classpath:/sqlmap-config.xml </ value >
  51. </ property >
  52. < property name = "dataSource" ref = "dataSource" />
  53. < property name = "lobHandler" ref = "lobHandler" />
  54. </ bean >
  55. <!--第二个数据库的sqlMapClient-->
  56. < bean id = "sqlMapClientB" class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
  57. < property name = "configLocation" >
  58. <!--包含第一个数据库表的map-->
  59. < value > classpath:/sqlmap-configb.xml </ value >
  60. </ property >
  61. < property name = "dataSource" ref = "dataSourceB" />
  62. < property name = "lobHandler" ref = "lobHandler" />
  63. </ bean >
  64. <!--ConstructAtomikosUserTransactionManager,neededtoconfigureSpring-->
  65. < bean id = "atomikosTransactionManager" class = "com.atomikos.icatch.jta.UserTransactionManager" init-method = "init"
  66. destroy-method = "close" >
  67. <!--whencloseiscalled,shouldweforcetransactionstoterminateornot?-->
  68. < property name = "forceShutdown" >
  69. < value > true </ value >
  70. </ property >
  71. </ bean >
  72. <!--AlsouseAtomikosUserTransactionImp,neededtoconfigureSpring-->
  73. < bean id = "atomikosUserTransaction" class = "com.atomikos.icatch.jta.UserTransactionImp" >
  74. < property name = "transactionTimeout" value = "240" />
  75. </ bean >
  76. <!--ConfiguretheSpringframeworktouseJTAtransactionsfromAtomikos-->
  77. < bean id = "transactionManager" class = "org.springframework.transaction.jta.JtaTransactionManager" >
  78. < property name = "transactionManager" >
  79. < ref bean = "atomikosTransactionManager" />
  80. </ property >
  81. < property name = "userTransaction" >
  82. < ref bean = "atomikosUserTransaction" />
  83. </ property >
  84. </ bean >



事务的配置, 使用了spring2.0的语法,所以将namesapce也帖出来了.

xml 代码
  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop = "http://www.springframework.org/schema/aop"
  5. xmlns:tx = "http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation ="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
  9. default-autowire = "byName" default-lazy-init = "true" >
  10. <!--支持@AspectJ标记-->
  11. < aop:aspectj-autoproxy />
  12. < aop:config proxy-target-class = "true" >
  13. < aop:advisor pointcut = "execution(**Facade.*(..))" advice-ref = "txAdvice" />
  14. < aop:advisor pointcut = "execution(**Manager.*(..))" advice-ref = "txAdvice" />
  15. </ aop:config >
  16. < tx:advice id = "txAdvice" >
  17. < tx:attributes >
  18. < tx:method name = "get*" read-only = "true" />
  19. < tx:method name = "find*" read-only = "true" />
  20. < tx:method name = "has*" read-only = "true" />
  21. < tx:method name = "locate*" read-only = "true" />
  22. < tx:method name = "*" />
  23. </ tx:attributes >
  24. </ tx:advice >
  25. </ beans >

你可能感兴趣的:(spring,AOP,bean,mysql,jdbc)