Spring3.0+atomikos building a JTA Distributed transaction

Source: Internet
Author: User

Excerpt from: http://gongjiayun.iteye.com/blog/1570111

Spring3.0+atomikos building a JTA Distributed transaction

spring3.0 no longer supports jtom, but we can use third-party open source software Atomikos (http://www.atomikos.com/) to achieve it.
Atomikos is an open source software that is currently doing quite well in distributed transaction management. With more than 10 years of experience, Atomikos guarantees your critical business and
Prevent costly data loss in the event of a system failure or accident. Atomikos Support for XA (global Transactions) and NON-XA (non-global transactions), Non-xa high efficiency
To XA. This article is mainly about XA events, because you want to manipulate multiple tables in different databases.

Let's talk about how to use it with spring3.0.

The first thing to download is the relevant jar package for spring3.0. It's not hard for everyone to believe.

Second To download Atomikos, the following packages are required

Atomikos-util-1.0.jarcglib-nodep-2.2.2.jartransactions-3.7.0.jartransactions-api-3.7.0.jartransactions-jdbc-3.7.0.jartran Sactions-jta-3.7.0.jar

Third Configuration
Make the following configuration in the Applicationcontext.xml file

<?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 configuration Start-up <!--mysql data source- <bean id= "Mysqlds" class= "Com.atomikos.jdbc.AtomikosDataSourceBean" init-method= "Init" destroy-method= "CLose "> <description>mysql xa datasource</description> <property name=" UniqueResourceName "&            Gt <value>mysql_ds</value> </property> <property name= "Xadatasourceclassname" value= "COM.M                Ysql.jdbc.jdbc2.optional.MysqlXADataSource "/> <property name=" xaproperties "> <props>                <prop key= "User" >userName</prop> <prop key= "password" >password</prop>            <prop key= "URL" >jdbc\:mysql\://\:3306/dataBaseName?autoReconnect\=true</prop> </props> </property> <!--the number of connections in the connection pool? --<property name= "Poolsize" value= "3"/> </bean> <!--Oracle Data Source--<bean id= "O Racleds "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> &LT;PR Op key= "URL" >jdbc\:oracle\:thin\:@\:1521\:d atabasename</prop> </props> </prop Erty> <!--the number of connections inside the connection pool? --<property name= "Poolsize" value= "3"/> </bean> <!--atomikos transaction Manager--<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= "a Tomikosusertransaction "class=" com.atomikos.icatch.jta.UserTransactionImp "> <property name=" TransactionTimeout "value="/> </bean> <!--Spring transaction Manager--<bean id= "Springtransactionman Ager "class=" Org.springframework.transaction.jta.JtaTransactionManager "> <property name=" Transactionma Nager "> <ref bean=" Atomikostransactionmanager "/> </property> <property name=" u Sertransaction "> <ref bean=" atomikosusertransaction "/> </property> </bean> & lt;! --Spring Transaction template I'm using programmatic transactions in my project---<bean id= "transactiontemplate" class= "Org.springframework.transaction.sup Port. Transactiontemplate "> <property name=" TransactionManager "> <ref bean=" Springtransactionmanag Er "/> &LT;/PROPERTY&GT </bean> <bean id= "simplejdbctemplate" class= "Org.springframework.jdbc.core.simple.SimpleJdbcTemplate" > <constructor-arg> <ref bean= "Mysqlds"/> </constructor-arg> &LT;/BEAN&G        T <bean id= "Simplejdbctemplateora" class= "Org.springframework.jdbc.core.simple.SimpleJdbcTemplate" > < constructor-arg> <ref bean= "oracleds"/> </constructor-arg> </bean> <!--s Pring Atomikos Configuration Ends-<!--next is the specific DAO configuration--<bean id= "Oraclejtadao" class= "Com.dao.TerminalOracl        Ejtadao "> <property name=" Simplejdbctemplateora "> <ref bean=" Simplejdbctemplateora "/> </property> <property name= "Transactiontemplate" > <ref bean= "transactiontemplate"/&        Gt </property> </bean> <bean id= "Myaqljtadao" class= "Com.dao.TerminalMyaqlJtaDao" > <propert Y name= "SimplEjdbctemplate "> <ref bean=" simplejdbctemplate "/> </property> <property name=" t Ransactiontemplate "> <ref bean=" transactiontemplate "/> </property> </bean></ Beans>

iv Add a jta.properties file below the src folder.
The contents of the file are as follows and this file is required. The main is to set some parameters for Atomikos startup, such as the output level of the log, the name of the log file, and so on.

# SAMPLE PROPERTIES FILE for the TRANSACTION service# this file illustrates the DIFFERENT SETTINGS for the TRANSACTION Mans ager# uncomment the ASSIGNMENTS to OVERRIDE DEFAULT values;# Required:factory Implementation class of the transaction cor e.# 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 is 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= # Set the number of log writes between checkpoints## com.atomikos.icatch.checkpoint_interval=500# Set output directory WH Ere console file and other files is to is 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 the log file# this name would be us  Ed 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 M illiseconds) for local transactions## com.atomikos.icatch.default_jta_timeout = 10000# Set the max timeout (in millisecond  s) 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 # Does we want to use P Arallel subtransactions?  JTA ' s default# is NO for EE compatibility## com.atomikos.icatch.serial_jta_transactions=true # If You Want to does explicit resource registration then# you nEed to set this value to false.## Com.atomikos.icatch.automatic_resource_registration=true # Set the this to WARN, INFO O R DEBUG to control the granularity# of output to the console file. #com. Atomikos.icatch.console_log_level=info # do Want transaction logging to is enabled or not?# If set to False, then no logging overhead would be done# at the risk of Los ing data after restart or crash.## com.atomikos.icatch.enable_logging=true# should two-phase commit is done in (multi-) thr eaded mode or not?# Set this to False if your want commits to being ordered according# to the order in which resources is add Ed to the transaction.## note:threads is reused on JDK 1.5 or higher. # for JDK 1.4, thread reuse is enabled as soon as the # concurrent backport are in the Classpath-see # Http://mirrors.ibi blio.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 

V Specific applications
Manually open transactions where transactional processing is required

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 () ;                }        }    }})

As for how with hibernate,mybatis these integration use, should also not be difficult, change the data source to Atomikos data source should be almost.
If you want to learn more about Atomikos's usage, you can check this link http://www.atomikos.com/Documentation/IntegratingTransactionEssentials

Spring3.0+atomikos building a JTA Distributed transaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.