spring 多数据库(数据源) JTA

凯哥Java 2011-04-12

原创spring多数据库(数据源)JTA事务2收藏

注:本文引自http://malaqu.com/?p=542

最近一个项目要跨多数据,配多数据源的,其中就用到了事务,毫无疑问我选择的是Spring的声明式JTA事务。我的环境是JBOSS+ORACLE9I

自己私下做了些实验,不过还是成功了

实验一:MySQL5.0

viewplaincopytoclipboardprint?

1.<?xmlversion=”1.0″encoding=”UTF-8″?>

2.<beansxmlns=”http://www.springframework.org/schema/beans”

3.xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xmlns:aop=”http://www.springframework.org/schema/aop”

4.xmlns:tx=”http://www.springframework.org/schema/tx”

5.xsi:schemaLocation=”http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd

6.http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd

7.http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd”>

8.

9.<beanid=”dataSource”class=”com.atomikos.jdbc.SimpleDataSourceBean”

10.init-method=”init”destroy-method=”close”>

11.<propertyname=”uniqueResourceName”>

12.<value>mysql/main</value>

13.</property>

14.<propertyname=”xaDataSourceClassName”>

15.<!–使用MysqlXADataSource(mysql>=5.0,Connector/J>=5.0才可以支持XADatasource)–>

16.<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>

17.</property>

18.<propertyname=”xaDataSourceProperties”>

19.<value>URL=jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8;user=root;password=root</value>

20.</property>

21.<propertyname=”exclusiveConnectionMode”>

22.<value>true</value>

23.</property>

24.<propertyname=”connectionPoolSize”>

25.<value>3</value>

26.</property>

27.<propertyname=”validatingQuery”>

28.<value>SELECT1</value>

29.</property>

30.</bean>

31.<!–第二个数据库–>

32.<beanid=”dataSourceB”class=”com.atomikos.jdbc.SimpleDataSourceBean”

33.init-method=”init”destroy-method=”close”>

34.<propertyname=”uniqueResourceName”>

35.<value>mysql/news</value>

36.</property>

37.<propertyname=”xaDataSourceClassName”>

38.<!–

39.使用MysqlXADataSource(mysql>=5.0,Connector/J>=5.0才可以支持XADatasource)

40.–>

41.<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>

42.</property>

43.<propertyname=”xaDataSourceProperties”>

44.<value>URL=jdbc:mysql://localhost:3306/crm2?useUnicode=true&characterEncoding=UTF-8;user=root;password=root</value>

45.</property>

46.<propertyname=”exclusiveConnectionMode”>

47.<value>true</value>

48.</property>

49.<propertyname=”connectionPoolSize”>

50.<value>3</value>

51.</property>

52.<propertyname=”validatingQuery”>

53.<value>SELECT1</value>

54.</property>

55.</bean>

56.

57.<beanid=”lobHandler”class=”org.springframework.jdbc.support.lob.DefaultLobHandler”/>

58.

59.<!–第一个数据库的sqlMapClient–>

60.<beanid=”sqlMapClient1″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>

61.<propertyname=”configLocation”>

62.<!–包含第一个数据库表的map–>

63.<value>classpath:SqlMapConfig.xml</value>

64.</property>

65.<propertyname=”dataSource”ref=”dataSource”/>

66.<propertyname=”lobHandler”ref=”lobHandler”/>

67.</bean>

68.<!–第二个数据库的sqlMapClient–>

69.<beanid=”sqlMapClient2″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>

70.<propertyname=”configLocation”>

71.<!–包含第一个数据库表的map–>

72.<value>classpath:SqlMapConfig2.xml</value>

73.</property>

74.<propertyname=”dataSource”ref=”dataSourceB”/>

75.<propertyname=”lobHandler”ref=”lobHandler”/>

76.</bean>

77.

78.<!–Optional:addalogadministrator–>

79.<beanid=”localLogAdministrator”

80.class=”com.atomikos.icatch.admin.imp.LocalLogAdministrator”/>

81.

82.<beanid=”userTransactionService”

83.class=”com.atomikos.icatch.config.UserTransactionServiceImp”

84.init-method=”init”destroy-method=”shutdownForce”>

85.<constructor-arg>

86.<!–IMPORTANT:specifyallAtomikospropertieshere–>

87.<props>

88.<propkey=”com.atomikos.icatch.service”>com.atomikos.icatch.standalone.UserTransactionServiceFactory</prop>

89.</props>

90.</constructor-arg>

91.<propertyname=”initialLogAdministrators”>

92.<list>

93.<refbean=”localLogAdministrator”/>

94.</list>

95.</property>

96.</bean>

97.<!–ConstructAtomikosUserTransactionManager,neededtoconfigureSpring–>

98.<beanid=”AtomikosTransactionManager”

99.class=”com.atomikos.icatch.jta.UserTransactionManager”

100.init-method=”init”destroy-method=”close”

101.depends-on=”userTransactionService”>

102.<!–whencloseiscalled,shouldweforcetransactionstoterminateornot?–>

103.<propertyname=”forceShutdown”value=”false”/>

104.</bean>

105.<!–AlsouseAtomikosUserTransactionImp,neededtoconfigureSpring–>

106.<beanid=”AtomikosUserTransaction”

107.class=”com.atomikos.icatch.jta.UserTransactionImp”

108.depends-on=”userTransactionService”>

109.<propertyname=”transactionTimeout”value=”300″/>

110.</bean>

111.<!–ConfiguretheSpringframeworktouseJTAtransactionsfromAtomikos–>

112.<beanid=”JtaTransactionManager”

113.class=”org.springframework.transaction.jta.JtaTransactionManager”

114.depends-on=”userTransactionService”>

115.<propertyname=”transactionManager”ref=”AtomikosTransactionManager”/>

116.<propertyname=”userTransaction”ref=”AtomikosUserTransaction”/>

117.</bean>

118.

119.<beanid=”user1Dao”class=”com.crm.code.dao.impl.User1DaoImpl”>

120.<propertyname=”sqlMapClient”>

121.<refbean=”sqlMapClient1″/>

122.</property>

123.</bean>

124.<beanid=”user2Dao”class=”com.crm.code.dao.impl.User2DaoImpl”>

125.<propertyname=”sqlMapClient”>

126.<refbean=”sqlMapClient2″/>

127.</property>

128.</bean>

129.<beanid=”user12Service”class=”com.crm.code.service.impl.User12ServiceImpl”>

130.<propertyname=”user1Dao”>

131.<refbean=”user1Dao”/>

132.</property>

133.<propertyname=”user2Dao”>

134.<refbean=”user2Dao”/>

135.</property>

136.</bean>

137.

138.</beans>

<?xmlversion=”1.0″encoding=”UTF-8″?><beansxmlns=”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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd”><beanid=”dataSource”class=”com.atomikos.jdbc.SimpleDataSourceBean”init-method=”init”destroy-method=”close”><propertyname=”uniqueResourceName”><value>mysql/main</value></property><propertyname=”xaDataSourceClassName”><!–使用MysqlXADataSource(mysql>=5.0,Connector/J>=5.0才可以支持XADatasource)–><value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value></property><propertyname=”xaDataSourceProperties”><value>URL=jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8;user=root;password=root</value></property><propertyname=”exclusiveConnectionMode”><value>true</value></property><propertyname=”connectionPoolSize”><value>3</value></property><propertyname=”validatingQuery”><value>SELECT1</value></property></bean><!–第二个数据库–><beanid=”dataSourceB”class=”com.atomikos.jdbc.SimpleDataSourceBean”init-method=”init”destroy-method=”close”><propertyname=”uniqueResourceName”><value>mysql/news</value></property><propertyname=”xaDataSourceClassName”><!–使用MysqlXADataSource(mysql>=5.0,Connector/J>=5.0才可以支持XADatasource)–><value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value></property><propertyname=”xaDataSourceProperties”><value>URL=jdbc:mysql://localhost:3306/crm2?useUnicode=true&characterEncoding=UTF-8;user=root;password=root</value></property><propertyname=”exclusiveConnectionMode”><value>true</value></property><propertyname=”connectionPoolSize”><value>3</value></property><propertyname=”validatingQuery”><value>SELECT1</value></property></bean><beanid=”lobHandler”class=”org.springframework.jdbc.support.lob.DefaultLobHandler”/><!–第一个数据库的sqlMapClient–><beanid=”sqlMapClient1″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”><propertyname=”configLocation”><!–包含第一个数据库表的map–><value>classpath:SqlMapConfig.xml</value></property><propertyname=”dataSource”ref=”dataSource”/><propertyname=”lobHandler”ref=”lobHandler”/></bean><!–第二个数据库的sqlMapClient–><beanid=”sqlMapClient2″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”><propertyname=”configLocation”><!–包含第一个数据库表的map–><value>classpath:SqlMapConfig2.xml</value></property><propertyname=”dataSource”ref=”dataSourceB”/><propertyname=”lobHandler”ref=”lobHandler”/></bean><!–Optional:addalogadministrator–><beanid=”localLogAdministrator”class=”com.atomikos.icatch.admin.imp.LocalLogAdministrator”/><beanid=”userTransactionService”class=”com.atomikos.icatch.config.UserTransactionServiceImp”init-method=”init”destroy-method=”shutdownForce”><constructor-arg><!–IMPORTANT:specifyallAtomikospropertieshere–><props><propkey=”com.atomikos.icatch.service”>com.atomikos.icatch.standalone.UserTransactionServiceFactory</prop></props></constructor-arg><propertyname=”initialLogAdministrators”><list><refbean=”localLogAdministrator”/></list></property></bean><!–ConstructAtomikosUserTransactionManager,neededtoconfigureSpring–><beanid=”AtomikosTransactionManager”class=”com.atomikos.icatch.jta.UserTransactionManager”init-method=”init”destroy-method=”close”depends-on=”userTransactionService”><!–whencloseiscalled,shouldweforcetransactionstoterminateornot?–><propertyname=”forceShutdown”value=”false”/></bean><!–AlsouseAtomikosUserTransactionImp,neededtoconfigureSpring–><beanid=”AtomikosUserTransaction”class=”com.atomikos.icatch.jta.UserTransactionImp”depends-on=”userTransactionService”><propertyname=”transactionTimeout”value=”300″/></bean><!–ConfiguretheSpringframeworktouseJTAtransactionsfromAtomikos–><beanid=”JtaTransactionManager”class=”org.springframework.transaction.jta.JtaTransactionManager”depends-on=”userTransactionService”><propertyname=”transactionManager”ref=”AtomikosTransactionManager”/><propertyname=”userTransaction”ref=”AtomikosUserTransaction”/></bean><beanid=”user1Dao”class=”com.crm.code.dao.impl.User1DaoImpl”><propertyname=”sqlMapClient”><refbean=”sqlMapClient1″/></property></bean><beanid=”user2Dao”class=”com.crm.code.dao.impl.User2DaoImpl”><propertyname=”sqlMapClient”><refbean=”sqlMapClient2″/></property></bean><beanid=”user12Service”class=”com.crm.code.service.impl.User12ServiceImpl”><propertyname=”user1Dao”><refbean=”user1Dao”/></property><propertyname=”user2Dao”><refbean=”user2Dao”/></property></bean></beans>

这样是成功的可是切换oracle9i时悲剧发生了

—Cause:com.atomikos.datasource.ResourceException:resumeforXIDoracle.jdbc.xa.OracleXid@145f939raised-3:theXAresourcedetectedaninternalerror

Causedby:com.ibatis.common.jdbc.exception.NestedSQLException:

—Theerroroccurredinibatis/Product1.xml.

—Theerroroccurredwhileexecutingupdate.

—Checktheinsertintoboss_product(PROD_ID,PARENT_ID,APP_ID,PROD_NAME,PROD_CODE,DEFAULT_VER_PROD_ID,DATA_PATH,GMT_CREATED,GMT_MODIFIED,CREATOR,MODIFIER,IS_DELETED)values(seq_boss_product.nextval,1,88,?,?,10,‘aaa’,sysdate,sysdate,‘aavv’,‘aacb’,‘n’)

官方说oracle连接问题哎。。。无语了

换了一种JTA事务机制通过JOTM

viewplaincopytoclipboardprint?

1.<?xmlversion=”1.0″encoding=”UTF-8″?>

2.<beansxmlns=”http://www.springframework.org/schema/beans”

3.xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xmlns:aop=”http://www.springframework.org/schema/aop”

4.xmlns:tx=”http://www.springframework.org/schema/tx”

5.xsi:schemaLocation=”http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd

6.http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd

7.http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd”>

8.

9.<beanid=”jotm”class=”org.springframework.transaction.jta.JotmFactoryBean”/>

10.<beanid=”txManager”class=”org.springframework.transaction.jta.JtaTransactionManager”>

11.<propertyname=”userTransaction”ref=”jotm”/>

12.</bean>

13.

14.<beanid=”dataSourceA”class=”org.enhydra.jdbc.pool.StandardXAPoolDataSource”

15.destroy-method=”shutdown”>

16.<propertyname=”dataSource”>

17.<beanclass=”org.enhydra.jdbc.standard.StandardXADataSource”destroy-method=”shutdown”>

18.<propertyname=”transactionManager”ref=”jotm”/>

19.<propertyname=”driverName”value=”oracle.jdbc.driver.OracleDriver”/>

20.<propertyname=”url”value=”jdbc:oracle:thin:@10.2.224.44:1521:trade”/>

21.</bean>

22.</property>

23.<propertyname=”user”value=”crm_aep”/>

24.<propertyname=”password”value=”crm_aep”/>

25.</bean>

26.

27.<beanid=”dataSourceB”class=”org.enhydra.jdbc.pool.StandardXAPoolDataSource”

28.destroy-method=”shutdown”>

29.<propertyname=”dataSource”>

30.<beanclass=”org.enhydra.jdbc.standard.StandardXADataSource”destroy-method=”shutdown”>

31.<propertyname=”transactionManager”ref=”jotm”/>

32.<propertyname=”driverName”value=”oracle.jdbc.driver.OracleDriver”/>

33.<propertyname=”url”value=”jdbc:oracle:thin:@10.2.226.24:1521:voucher”/>

34.</bean>

35.</property>

36.<propertyname=”user”value=”boss”/>

37.<propertyname=”password”value=”boss”/>

38.</bean>

39.

40.<tx:annotation-driventransaction-manager=”txManager”proxy-target-class=”true”/>

41.

42.<!–第一个数据库的sqlMapClient–>

43.<beanid=”sqlMapClient1″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>

44.<propertyname=”configLocation”>

45.<!–包含第一个数据库表的map–>

46.<value>classpath:SqlMapConfig_ora1.xml</value>

47.</property>

48.<propertyname=”dataSource”ref=”dataSourceA”/>

49.</bean>

50.<!–第二个数据库的sqlMapClient–>

51.<beanid=”sqlMapClient2″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>

52.<propertyname=”configLocation”>

53.<!–包含第一个数据库表的map–>

54.<value>classpath:SqlMapConfig_ora2.xml</value>

55.</property>

56.<propertyname=”dataSource”ref=”dataSourceB”/>

57.</bean>

58.

59.<beanid=”product1Dao”class=”com.crm.code.dao.impl.Product1DaoImpl”>

60.<propertyname=”sqlMapClient”>

61.<refbean=”sqlMapClient1″/>

62.</property>

63.</bean>

64.<beanid=”product2Dao”class=”com.crm.code.dao.impl.Product2DaoImpl”>

65.<propertyname=”sqlMapClient”>

66.<refbean=”sqlMapClient2″/>

67.</property>

68.</bean>

69.<beanid=”product12Service”class=”com.crm.code.service.impl.Product12ServiceImpl”>

70.<propertyname=”product1Dao”>

71.<refbean=”product1Dao”/>

72.</property>

73.<propertyname=”product2Dao”>

74.<refbean=”product2Dao”/>

75.</property>

76.</bean>

77.</beans>

<?xmlversion=”1.0″encoding=”UTF-8″?><beansxmlns=”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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd”><beanid=”jotm”class=”org.springframework.transaction.jta.JotmFactoryBean”/><beanid=”txManager”class=”org.springframework.transaction.jta.JtaTransactionManager”><propertyname=”userTransaction”ref=”jotm”/></bean><beanid=”dataSourceA”class=”org.enhydra.jdbc.pool.StandardXAPoolDataSource”destroy-method=”shutdown”><propertyname=”dataSource”><beanclass=”org.enhydra.jdbc.standard.StandardXADataSource”destroy-method=”shutdown”><propertyname=”transactionManager”ref=”jotm”/><propertyname=”driverName”value=”oracle.jdbc.driver.OracleDriver”/><propertyname=”url”value=”jdbc:oracle:thin:@10.2.224.44:1521:trade”/></bean></property><propertyname=”user”value=”crm_aep”/><propertyname=”password”value=”crm_aep”/></bean><beanid=”dataSourceB”class=”org.enhydra.jdbc.pool.StandardXAPoolDataSource”destroy-method=”shutdown”><propertyname=”dataSource”><beanclass=”org.enhydra.jdbc.standard.StandardXADataSource”destroy-method=”shutdown”><propertyname=”transactionManager”ref=”jotm”/><propertyname=”driverName”value=”oracle.jdbc.driver.OracleDriver”/><propertyname=”url”value=”jdbc:oracle:thin:@10.2.226.24:1521:voucher”/></bean></property><propertyname=”user”value=”boss”/><propertyname=”password”value=”boss”/></bean><tx:annotation-driventransaction-manager=”txManager”proxy-target-class=”true”/><!–第一个数据库的sqlMapClient–><beanid=”sqlMapClient1″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”><propertyname=”configLocation”><!–包含第一个数据库表的map–><value>classpath:SqlMapConfig_ora1.xml</value></property><propertyname=”dataSource”ref=”dataSourceA”/></bean><!–第二个数据库的sqlMapClient–><beanid=”sqlMapClient2″class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”><propertyname=”configLocation”><!–包含第一个数据库表的map–><value>classpath:SqlMapConfig_ora2.xml</value></property><propertyname=”dataSource”ref=”dataSourceB”/></bean><beanid=”product1Dao”class=”com.crm.code.dao.impl.Product1DaoImpl”><propertyname=”sqlMapClient”><refbean=”sqlMapClient1″/></property></bean><beanid=”product2Dao”class=”com.crm.code.dao.impl.Product2DaoImpl”><propertyname=”sqlMapClient”><refbean=”sqlMapClient2″/></property></bean><beanid=”product12Service”class=”com.crm.code.service.impl.Product12ServiceImpl”><propertyname=”product1Dao”><refbean=”product1Dao”/></property><propertyname=”product2Dao”><refbean=”product2Dao”/></property></bean></beans>

成功了。。。

很好很好哈哈哈

发表于@2010年05月05日 12:06:00|评论(0)|编辑|举报|收藏

旧一篇:spring多数据源JTA事务|新一篇:禁止页面的选择和复制功能

相关推荐