日期:2014-05-16 浏览次数:20506 次
spring+jotm 多数据源事务管理系列
?
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="servicePo