利用Jotm 2使Tomcat 6具有分布式事务管理功能

来源:互联网 发布:php存储json 编辑:程序博客网 时间:2024/06/06 14:17

使用Spring 2.5 Hibernate 3.5 Tomcat 6 Jotm 2配置分布式事务管理,多数据源事务管理。

因为Spring自己带了org.springframework.transaction.jta.JotmFactoryBean所以决定用Jotm2.1.9来做分布式事务管理。

运行环境为:

JDK 1_5_0_22

apache-tomcat-6.0.20

ow2-jotm-dist-2.1.9-bin/ow2-jotm-dist-2.1.9/lib/*.jar

ojdbc14.jar

jtds-1.2.5-dist/jtds-1.2.5.jar

hibernate-distribution-3.5.5-Final/hibernate3.jar

hibernate-distribution-3.5.5-Final/lib/required/*.jar

spring-framework-2.5.6.SEC02-with-docs/spring-framework-2.5.6.SEC02/dist/spring.jar

MS SQL Server 2000 + SP4数据库

Oracle 10g数据库

下面是Application-jta.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"

      xsi:schemaLocation="

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd

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

">

<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="oracleDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">

      <property name="transactionManager" ref="jotm"/>

      <property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>

      <property name="url" value="jdbc:oracle:thin:@192.168.1.3:1521:orcl"/>

      <property name="user" value="system"/>

      <property name="password" value="123456789"/>

</bean>

<bean id="firstdataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">

      <property name="dataSource" ref="oracleDataSource"/>

      <property name="user" value="system"/>

      <property name="password" value="123456789"/>

      <property name="maxSize" value="5"/>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

      <property name="dataSource" ref="firstdataSource"/>

      <property name="mappingDirectoryLocations">

      <list>

<value>classpath:/com/huangcheng/pojo</value>

      </list>

      </property>

      <property name="hibernateProperties">

      <value>

           hibernate.format_sql=true

           hibernate.show_sql=true

           hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

           hibernate.query.substitutions=true1, false 0

           hibernate.jdbc.batch_size=20

           hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

           hibernate.cache.provider_configuration_file_resource_path=/ehcache-hibernate.xml

      </value>

</property>

</bean>

<bean id="mssqlDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">

      <property name="transactionManager" ref="jotm"/>

      <property name="driverName" value="net.sourceforge.jtds.jdbc.Driver"/>

      <property name="url" value="jdbc:jtds:sqlserver://127.0.0.1:1433/master"/>

      <property name="user" value="sa"/>

      <property name="password" value="sa"/>

</bean>

<bean id="secondDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">

      <property name="dataSource" ref="mssqlDataSource"/>

      <property name="user" value="sa"/>

      <property name="password" value="sa"/>

      <property name="maxSize" value="5"/>

</bean>

<bean id="sessionFactoryMSSQL2000" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

      <property name="dataSource" ref="secondDataSource"/>

      <property name="mappingDirectoryLocations">

      <list>

           <value>classpath:/com/huangcheng/mssql/entity</value>

      </list>

      </property>

      <property name="hibernateProperties">

            <value>

                 hibernate.format_sql=true

                 hibernate.show_sql=true

                 hibernate.dialect=org.hibernate.dialect.SQLServerDialect

                 hibernate.query.substitutions=true1, false 0

                 hibernate.jdbc.batch_size=20

                 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

                 hibernate.cache.provider_configuration_file_resource_path=/ehcache-hibernate.xml

           </value>

      </property>

</bean>

<aop:config>

      <aop:pointcut id="commonPointcut" expression="execution(* *..manager.*Manager.*(..))"/>       

      <aop:advisor id="managerTx" advice-ref="txAdvice"pointcut-ref="commonPointcut" order="0"/>

</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">

      <tx:attributes>

            <tx:method name="*"rollback-for="Exception"/>

      </tx:attributes>

</tx:advice>

</beans>