浏览 2193 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-06-07
JOTM (Java Open Transaction Manager)是由ObjectWeb协会开发的功能完整的且资源开放的独立的事务管理器。 它提供了 JAVA 应用程序的事务支持,而且与 JTA( JAVA 事务 API)兼容。您可以在JOTM home page 了解到更多的详细信息。
本文介绍如何在spring配置文件中加入jotm来实现多数据源事务控制。
先看一个没有加入jotm的例子 : <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" default-lazy-init="true"> <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>${datasource1.driver}</value> </property> <property name="url"> <value>${datasource1.url}</value> </property> <property name="username"> <value>${datasource1.username}</value> </property> <property name="password"> <value>${datasource1.password}</value> </property> <property name="initialSize"> <value>5</value> </property> <property name="maxActive"> <value>10</value> </property> </bean> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>${datasource2.driver}</value> </property> <property name="url"> <value>${datasource2.url}</value> </property> <property name="username"> <value>${datasource2.username}</value> </property> <property name="password"> <value>${datasource2.password}</value> </property> <property name="initialSize"> <value>5</value> </property> <property name="maxActive"> <value>10</value> </property> </bean> <bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource1" /> <bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource2" /> <aop:config proxy-target-class="true"> <aop:pointcut id="servicePoint1" expression="execution (* com.xxx.function.*.service.*Service*.* (..))" /> <aop:advisor pointcut-ref="servicePoint1" id="txAdvisor1" advice-ref="txAdvice1" /> </aop:config> <tx:advice id="txAdvice1" transaction-manager="transactionManager1"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="apply*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="handle*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> </tx:attributes> </tx:advice> <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource1" /> </bean> <aop:config proxy-target-class="true"> <aop:pointcut id="servicePoint2" expression="execution (* com.xxx.function.*.service.*Service*.* (..))" /> <aop:advisor pointcut-ref="servicePoint2" id="txAdvisor2" advice-ref="txAdvice2" /> </aop:config> <tx:advice id="txAdvice2" transaction-manager="transactionManager1"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="apply*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="handle*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> </tx:attributes> </tx:advice> <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource2" /> </bean> </beans> 这样的配置不能够将两个数据源放到同一个事务中,一个事务回滚不会使另一个事务同时回滚,这样就会造成脏数据。
spring本身提供了对jotm的支持,所以spring整合jotm很方便,下面看加入jotm的例子 : <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" default-lazy-init="true"> <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction" ref="jotm" /> </bean> <bean id="dataSource1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"> <property name="dataSource"> <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> <property name="transactionManager" ref="jotm" /> <property name="driverName" value="${datasource1.driver}" /> <property name="url" value="${datasource1.url}" /> </bean> </property> <property name="user" value="${datasource1.username}" /> <property name="password" value="${datasource1.password}" /> </bean> <bean id="dataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"> <property name="dataSource"> <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> <property name="transactionManager" ref="jotm" /> <property name="driverName" value="${datasource2.driver}" /> <property name="url" value="${datasource2.url}" /> </bean> </property> <property name="user" value="${datasource2.username}" /> <property name="password" value="${datasource2.password}" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource1" /> </bean> <bean id="jdbcTemplateDas" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource2" /> </bean> <aop:config proxy-target-class="true"> <aop:pointcut id="servicePoint" expression="execution (* com.xxx.function.*.service.*Service*.* (..))" /> <aop:advisor pointcut-ref="servicePoint" id="txAdvisor" advice-ref="defaultTxAdvice" /> </aop:config> <tx:advice id="defaultTxAdvice"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="apply*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="handle*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> </tx:attributes> </tx:advice> </beans> ok,这样两个数据源就会统一由jotm进行管理,代码层面不需要任何修改。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-11-09
大侠,配置jotm的jar包需要哪些啊,我找了几个jar包都不行,谢谢了
|
|
返回顶楼 | |