1. 程式人生 > >spring3.0+Atomikos 構建jta的分布式事務

spring3.0+Atomikos 構建jta的分布式事務

.org const classname rec bili cglib als 相關 ns-3

摘自: http://gongjiayun.iteye.com/blog/1570111

spring3.0+Atomikos 構建jta的分布式事務

spring3.0已經不再支持jtom了,不過我們可以用第三方開源軟件atomikos(http://www.atomikos.com/)來實現.
Atomikos是目前在分布式事務管理中做得相當不錯的開源軟件。有10年以上的經驗,Atomikos保障您的關鍵事務和
防止昂貴的數據丟失在發生系統故障或事故中.Atomikos支持XA(全局事務)和NON-XA(非全局事務),NON-XA效率高
於XA.本文主要是講XA事件,因為要在不同的數據庫中操作多張表.

接下來說一下怎麽和spring3.0結合使用

首先要下載spring3.0的相關jar包.這個相信對大家說來不難

第二.下載Atomikos,需要以下這些包

atomikos-util-1.0.jar
cglib-nodep-2.2.2.jar
transactions-3.7.0.jar
transactions-api-3.7.0.jar
transactions-jdbc-3.7.0.jar
transactions-jta-3.7.0.jar

第三.配置
在applicationContext.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:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-lazy-init="true">

    <!-- spring atomikos 配置 開始-->
    <!-- mysql數據源 -->
    <bean id="mysqlDS" 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" />
        <property name="xaProperties">
            <props>
                <prop key="user">userName</prop>
                <prop key="password">password</prop>
                <prop key="URL">jdbc\:mysql\://127.0.0.1\:3306/dataBaseName?autoReconnect\=true</prop>
            </props>
        </property>
        <!-- 連接池裏面連接的個數? --> 
        <property name="poolSize" value="3"/> 
    </bean>

    <!-- oracle數據源 -->
    <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">userName</prop>
                <prop key="password">password</prop>
                <prop key="URL">jdbc\:oracle\:thin\:@127.0.0.1\:1521\:dataBaseName</prop>
            </props>
        </property>
        <!-- 連接池裏面連接的個數? --> 
        <property name="poolSize" value="3"/>
    </bean>

    <!-- atomikos事務管理器 -->
    <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="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>

    <!-- spring 事務管理器 -->
    <bean id="springTransactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <ref bean="atomikosTransactionManager" />
        </property>
        <property name="userTransaction">
            <ref bean="atomikosUserTransaction" />
        </property>
    </bean>

    <!-- spring 事務模板 我在項目當中用的是編程式事務-->
    <bean id="transactionTemplate"
        class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager">
            <ref bean="springTransactionManager" />
        </property>
    </bean>

    <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg>
            <ref bean="mysqlDS" />
        </constructor-arg>
    </bean>
    
    <bean id="simplejdbcTemplateOra" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg>
            <ref bean="oracleDS" />
        </constructor-arg>
    </bean>

    <!-- spring atomikos 配置 結束-->
    
    <!-- 接下來就是具體的Dao的配置 -->
    <bean id="oracleJtaDao" class="com.dao.TerminalOracleJtaDao">
        <property name="simplejdbcTemplateOra">
            <ref bean="simplejdbcTemplateOra" />
        </property>
        <property name="transactionTemplate">
            <ref bean="transactionTemplate" />
        </property>
    </bean>

    <bean id="myaqlJtaDao" class="com.dao.TerminalMyaqlJtaDao">
        <property name="simpleJdbcTemplate">
            <ref bean="simpleJdbcTemplate" />
        </property>
        <property name="transactionTemplate">
            <ref bean="transactionTemplate" />
        </property>
    </bean>
</beans>
技術分享圖片

第四.在src文件夾下面加入一個jta.properties文件.
文件內容如下,這個文件是必須的.主是是設置atomikos啟動的一些參數,比如日誌的輸出級別,日誌文件的名稱等.

技術分享圖片
# SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE
# THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER
# UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES;

# Required: factory implementation class of the transaction core.
# NOTE: there is no default for this, so it MUST be specified! 
# 
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory

        
# Set base name of file where messages are output 
# (also known as the ‘console file‘).
#
com.atomikos.icatch.console_file_name = tm.out

# Size limit (in bytes) for the console file;
# negative means unlimited.
#
# com.atomikos.icatch.console_file_limit=-1

# For size-limited console files, this option
# specifies a number of rotating files to 
# maintain.
#
# com.atomikos.icatch.console_file_count=1

# Set the number of log writes between checkpoints
#
# com.atomikos.icatch.checkpoint_interval=500

# Set output directory where console file and other files are to be put
# make sure this directory exists!
#
# com.atomikos.icatch.output_dir = ./

# Set directory of log files; make sure this directory exists!
#
# com.atomikos.icatch.log_base_dir = ./

# Set base name of log file
# this name will be  used as the first part of 
# the system-generated log file name
#
com.atomikos.icatch.log_base_name = tmlog

# Set the max number of active local transactions 
# or -1 for unlimited.
#
# com.atomikos.icatch.max_actives = 50

# Set the default timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.default_jta_timeout = 10000

# Set the max timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.max_timeout = 300000

# The globally unique name of this transaction manager process
# override this value with a globally unique name
#
com.atomikos.icatch.tm_unique_name = tm
    
# Do we want to use parallel subtransactions? JTA‘s default
# is NO for J2EE compatibility
#
# com.atomikos.icatch.serial_jta_transactions=true
                    
# If you want to do explicit resource registration then
# you need to set this value to false.
#
# com.atomikos.icatch.automatic_resource_registration=true  
    
# Set this to WARN, INFO or DEBUG to control the granularity
# of output to the console file.
#
com.atomikos.icatch.console_log_level=INFO
    
# Do you want transaction logging to be enabled or not?
# If set to false, then no logging overhead will be done
# at the risk of losing data after restart or crash.
#
# com.atomikos.icatch.enable_logging=true

# Should two-phase commit be done in (multi-)threaded mode or not?
# Set this to false if you want commits to be ordered according
# to the order in which resources are added to the transaction.
#
# NOTE: threads are reused on JDK 1.5 or higher. 
# For JDK 1.4, thread reuse is enabled as soon as the 
# concurrent backport is in the classpath - see 
# http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/
#
# com.atomikos.icatch.threaded_2pc=false

# Should shutdown of the VM trigger shutdown of the transaction core too?
#
# com.atomikos.icatch.force_shutdown_on_vm_exit=false
技術分享圖片

第五.具體應用
在需要分布事務處理的地方手動開啟事務

技術分享圖片
TransactionTemplate.getTransactionTemplate().execute(new TransactionCallback(){
    public Object doInTransaction(TransactionStatus status){
        boolean flag = true;
        try {

        } catche (Exception e){
            flag = false;
        } finally {
            if (!flag) {
                status.status.setRollbackOnly();    
            }
        }
    }
})
技術分享圖片

至於怎麽與hibernate,mybatis這些整合使用,應該也不難,把數據源換成atomikos數據源應該就差不多了.
如果想了解更多有關atomikos的用法,可以查看這個鏈接http://www.atomikos.com/Documentation/IntegratingTransactionEssentials

spring3.0+Atomikos 構建jta的分布式事務