天天看点

spring+ibatis+Atomikos实现JTA事务

http://www.cnblogs.com/warison2008/archive/2010/10/15/1852338.html

Atomikos分两个:一个是开源的TransactionEssentials,一个是商业的ExtremeTransactions。

TransactionEssentials的主要特征:JTA/XA 事务管理 —— 提供事务管理和连接池不需要应用服务器 —— TransactionEssentials可以在任何Java EE应用服务器中运行,也就是不依赖于任何应用服务器开源 —— TransactionEssentials是遵守Apache版本2许可的开源软件专注于JDBC/JMS —— 支持所有XA资源,但是资源池和消息监听是专供JDBC和JMS的与Spring 和 Hibernate 集成 —— 提供了描述如何与Spring和Hibernate集成的文档

一、环境

spring 2

ibatis2

AtomikosTransactionsEssentials-3.7.0 下载地址:http://www.atomikos.com/Main/InstallingTransactionsEssentials

MySQL-5.1 :数据库引擎为InnoDB,只有这样才能支持事务

JDK1.6

Oracle10

二 jar包

Atomikos jar必须包 transactions-jdbc.jar

transactions-jta.jar

transactions.jar

atomikos-util.jar 

transactions-api.jar

ibatis,spring

只要符合二者集成jar组合即可,无额外jar

三、配置

Oracle用户授权:(视情况而定,如果程序出现如下异常则需要加上1672 [main] WARN atomikos - ERROR IN RECOVERY com.atomikos.datasource.ResourceException: Error in recovery)

grant select on sys.dba_pending_transactions to orcl;

grant select on sys.pending_trans$ to orcl;

grant select on sys.dba_2pc_pending to orcl;

grant execute on sys.dbms_system to orcl;

jta.properties 配置文件,即src/ jta.properties内容如下:

?

1 2 3 4 5

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory

com.atomikos.icatch.console_file_name = tm.out

com.atomikos.icatch.log_base_name = tmlog

com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm

com.atomikos.icatch.console_log_level = INFO   

spring配置文件

Atomikos分两个:一个是开源的TransactionEssentials,一个是商业的ExtremeTransactions。

TransactionEssentials的主要特征:JTA/XA 事务管理 —— 提供事务管理和连接池不需要应用服务器 —— TransactionEssentials可以在任何Java EE应用服务器中运行,也就是不依赖于任何应用服务器开源 —— TransactionEssentials是遵守Apache版本2许可的开源软件专注于JDBC/JMS —— 支持所有XA资源,但是资源池和消息监听是专供JDBC和JMS的与Spring 和 Hibernate 集成 —— 提供了描述如何与Spring和Hibernate集成的文档

一、环境

spring 2

ibatis2

AtomikosTransactionsEssentials-3.7.0 下载地址:http://www.atomikos.com/Main/InstallingTransactionsEssentials

MySQL-5.1 :数据库引擎为InnoDB,只有这样才能支持事务

JDK1.6

Oracle10

二 jar包

Atomikos jar必须包 transactions-jdbc.jar

transactions-jta.jar

transactions.jar

atomikos-util.jar 

transactions-api.jar

ibatis,spring

只要符合二者集成jar组合即可,无额外jar

三、配置

Oracle用户授权:(视情况而定,如果程序出现如下异常则需要加上1672 [main] WARN atomikos - ERROR IN RECOVERY com.atomikos.datasource.ResourceException: Error in recovery)

grant select on sys.dba_pending_transactions to orcl;

grant select on sys.pending_trans$ to orcl;

grant select on sys.dba_2pc_pending to orcl;

grant execute on sys.dbms_system to orcl;

jta.properties 配置文件,即src/ jta.properties内容如下:

?

1 2 3 4 5

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory

com.atomikos.icatch.console_file_name = tm.out

com.atomikos.icatch.log_base_name = tmlog

com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm

com.atomikos.icatch.console_log_level = INFO   

spring配置文件

代码

<?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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

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

  http://www.springframework.org/schema/aop

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

  http://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">

<!-- 事物管理器 -->

<bean id="atomikosUserTransaction"

        class="com.atomikos.icatch.jta.UserTransactionImp">

        <description>UserTransactionImp</description>

        <property name="transactionTimeout" value="300"/>

    </bean>

    <bean id="atomikosTransactionManager"

        class="com.atomikos.icatch.jta.UserTransactionManager"

        init-method="init" destroy-method="close">

        <description>UserTransactionManager</description>

        <property name="forceShutdown">

            <value>true</value>

        </property>

    </bean>

    <bean id="transactionManager"

        class="org.springframework.transaction.jta.JtaTransactionManager">

        <description>JtaTransactionManager</description>

        <property name="transactionManager">

            <ref bean="atomikosTransactionManager" />

        </property>

        <property name="userTransaction">

            <ref bean="atomikosUserTransaction" />

        </property>

        <property name="allowCustomIsolationLevels" value="true"/> <!—必须设置,否则程序出现异常 JtaTransactionManager does not support custom isolation levels by default -->

<!—oracle数据源定义 -->

    </bean>

<bean id="oracleDS" class="com.atomikos.jdbc.AtomikosDataSourceBean"

        init-method="init" destroy-method="close">

        <description>oracle xa datasource</description>

        <property name="uniqueResourceName">

            <value>oracle_ds</value>

        </property>

        <property name="xaDataSourceClassName">

            <value>oracle.jdbc.xa.client.OracleXADataSource</value>

        </property>

        <property name="xaProperties">

            <props>

                <prop key="user">orcl</prop>

                <prop key="password">123</prop>

                <prop key="URL">jdbc:oracle:thin:@ localhost:1521:orcl</prop>

            </props>

        </property>

        <property name="testQuery">

        <value>select 1 from dual</value> <!—尽力加上,不然会出现告警 -->

        </property>

    </bean>

    <!— mysql数据源定义 -->

    <bean id="dataSourceoracle2" class="com.atomikos.jdbc.AtomikosDataSourceBean"

        init-method="init" destroy-method="close">

        <description>mysql xa datasource</description>

        <property name="uniqueResourceName">

            <value>mysql_ds</value>

        </property>

        <property name="xaDataSourceClassName">

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

        </property>

        <property name="xaProperties">

            <props>

                <prop key="user">root</prop>

                <prop key="password">123</prop>

                <prop key="URL">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8</prop>

            </props>

        </property>

        <property name="testQuery">

        <value>select 1</value><!—尽力加上,不然会出现告警 -->

        </property>

    </bean>

<!--  ibatis配置源 -->

    <bean id="sqlMapClient"

        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

        <property name="configLocation"

            value="classpath:sql-moredata-config.xml" />

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

    </bean>

    <!-- ibatis配置源2 -->

    <bean id="sqlMapClient2"

        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

        <property name="configLocation"

            value="classpath:sql-moredata2-config.xml" />

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

    </bean>

<bean id="testDaoR"

        class="com.test.moredata.TestDAOImpR">

        <property name="sqlMapClient" ref="sqlMapClient"></property>

    </bean>

<bean id="testDao"

        class="com.test.moredata.TestDAOImp">

        <property name="sqlMapClient" ref="sqlMapClient2"></property>

    </bean>

    <bean id="testService"

        class="com.test.moredata.TestService">

        <property name="testDao" ref="testDao"></property>

        <property name="testDaoR" ref="testDaoR"></property>

    </bean>

<!— aop配置 -->

    <tx:advice id="transactionManagerAdivice"

        transaction-manager="transactionManager">

        <tx:attributes>

            <tx:method name="*" isolation="READ_COMMITTED"

                propagation="REQUIRED" rollback-for="java.lang.RuntionException" />

        </tx:attributes>

    </tx:advice>

<aop:config>

            <aop:pointcut

            expression="execution(* com.test.moredata.TestService.*(..))"

            id="tsServicePc" />

            <aop:advisor advice-ref="transactionManagerAdivice"

            pointcut-ref="tsServicePc" />

    </aop:config>

继续阅读