Implementation of Atomikos and spring integration practice with Distributed transaction JTA

Source: Internet
Author: User
Tags maven central

Understanding the principles of distributed transaction JTA see: http://www.ibm.com/developerworks/cn/java/j-lo-jta/

JTA Realization Product Introduction: http://blog.chinaunix.net/uid-122937-id-3793220.html


Atomikos website is inaccessible, but the MAVEN Central Library has a Atomikos package. Atomikos Integrated Spring,hibernate,mybatis Online articles More, this article is javase way to borrow the spring configuration to test the Atomikos implementation of JTA.


The next thing to do is two (+) database, in a transaction to the database operation to verify the atomicity of the operation, that is, either two database operations are successful or fail.


1. Preparatory work

1.1 Maven Pom.xml Add Dependency Packages

Atomikos: (currently the latest version)

<dependency><groupid>com.atomikos</groupid><artifactid>transactions-jdbc</ Artifactid><version>3.9.3</version></dependency>


Jar Dependency Graphs:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6D/ED/wKiom1Vu-WuiaLnzAACxcXC9yhs625.jpg "title=" Atomikos-transactions.png "alt=" Wkiom1vu-wuialnzaacxcxc9yhs625.jpg "/>

PostgreSQL Database driver:

<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId> <version>9.2-1004-jdbc4</version></dependency>

Spring,junit relies here omitted.


1.2 Creating a database and tables

The databases are: Javaee,tomdb

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6D/E8/wKioL1Vu-8eAXWY-AADUZDXsd7M684.jpg "title=" Jta-temp.png "alt=" Wkiol1vu-8eaxwy-aaduzdxsd7m684.jpg "/>


2. Add a configuration file to your project

Spring-jta.xml and Transaction.properties file, Spring-jta.xml under Src/main/resources/integration, Transaction.properties under the src/main/resources/.


2.1 Configure two Xadatasource in Spring-jta.xml:

<!--  When using distributed transactions, set the max_prepared_transactions of PostgreSQL to a value greater than 0, which defaults to 0 --><!--  database a -- ><bean id= "A"  class= "Com.atomikos.jdbc.AtomikosDataSourceBean" init-method= "Init"   Destroy-method= "Close" ><property name= "Uniqueresourcename"  value= "pg/a"  /><property  name= "Xadatasourceclassname"  value= "Org.postgresql.xa.PGXADataSource"  /><property  Name= "xaproperties" ><props><prop key= "user" >postgres</prop><prop key= " Password ">postgres</prop><prop key=" ServerName ">localhost</prop><prop key=" PortNumber ">5432</prop><prop key=" DatabaseName ">tomdb</prop></props></ Property><property name= "Poolsize"  value= " /><property name=" ReapTimeout "  value= "20000"  /></bean><bean id= "B"  class= " Com.atomikos.jdbc.AtomikosDataSourceBean "init-method=" Init " desTroy-method= "Close" ><property name= "Uniqueresourcename"  value= "pg/b"  /><property  name= "Xadatasourceclassname"  value= "Org.postgresql.xa.PGXADataSource"  /><property  Name= "xaproperties" ><props><prop key= "user" >postgres</prop><prop key= " Password ">postgres</prop><prop key=" ServerName ">localhost</prop><prop key=" PortNumber ">5432</prop><prop key=" DatabaseName ">javaee</prop></props></ Property><property name= "Poolsize"  value= " /><property name=" ReapTimeout "  value= "20000"  /></bean>


Description

The max_prepared_transactions parameter value for PostgreSQL is 0 by default, and to turn on a distributed transaction needs to be set to a value greater than 0, which is in the postgresql\9.3\data\postgresql.conf file.

Pgxadatasource's parent class Basedatasource does not have a URL property, you can set properties such as Servername,portnumber,databasename, respectively. Different database drivers have different implementation methods.


2.2 Configuring Transaction Management Objects and UserTransaction interface implementations

<!--Atomikos Transaction management--><bean id= "Atomikosusertransactionmanager" class= " Com.atomikos.icatch.jta.UserTransactionManager "><description>usertransactionmanager</description ><property name= "Forceshutdown" value= "true"/></bean><bean id= "atomikosusertransaction" class= " Com.atomikos.icatch.jta.UserTransactionImp "><property name=" TransactionTimeout "value="/></bean ><bean id= "TransactionManager" class= "Org.springframework.transaction.jta.JtaTransactionManager" >< Property Name= "TransactionManager" ref= "Atomikosusertransactionmanager" ></property></bean>

The above three beans can be used independently for transaction control, see below 3.


3. Writing tests

3.1 Using Atomikosusertransactionmanager Object Test (Testatomikos1.java)

package secondriver.springsubway.example.jta;import java.sql.connection;import  java.sql.sqlexception;import javax.transaction.heuristicmixedexception;import  javax.transaction.heuristicrollbackexception;import javax.transaction.notsupportedexception;import  javax.transaction.rollbackexception;import javax.transaction.systemexception;import  org.junit.beforeclass;import org.junit.test;import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext;import  Com.atomikos.icatch.jta.usertransactionmanager;import com.atomikos.jdbc.atomikosdatasourcebean;public  class TestAtomikos1 {public static ApplicationContext ctx; @BeforeClasspublic  static void beforeclass ()  {ctx = new classpathxmlapplicationcontext (" Classpath:integration/spring-jta.xml ");} Public static void afterclass () &NBSP;{CTX&NBsp;= null;} @Testpublic  void test1 ()  {exe ("abc",  "abc");} @Testpublic  void test2 ()  {exe ("123=",  "123");} Public void exe (STRING&NBSP;AV,&NBSP;STRING&NBSP;BV)  {atomikosdatasourcebean adsa =   (Atomikosdatasourcebean)  ctx.getbean ("a"); atomikosdatasourcebean adsb =  (Atomikosdatasourcebean)  ctx.getbean ("B"); connection conna; connection connb; usertransactionmanager utm =  (Usertransactionmanager)  ctx.getbean (" Atomikosusertransactionmanager "); Try {utm.begin (); Conna = adsa.getconnection (); connB =  adsb.getconnection (); Conna.preparestatement ("insert into jta_temp  (value)  values ('"  + av +  "')"). Execute (); Connb.preparestatement ("insert into jta_temp " (Value )  values ('  + bv +  ') '). Execute (); Utm.commit ();}  catch  (sqlexception | notsupportedexception | systemexception| securityexception | illegalstateexception |  Rollbackexception| heuristicmixedexception | heuristicrollbackexception e)  { E.printstacktrace ();}}}


3.2 Using Spring's Jtausertransactionmanager Object Test (Testatomikos2.java modifying the Exe method in Testatomikos1.java)

@Testpublic void Test1 () {exe ("abc", "abc");} @Testpublic void Test2 () {exe ("123 =", "123");}
public void exe (string av, String bv) {transactionfactory TxM = (transactionfactory) ctx.getbean ("TransactionManager"); JdbcTemplate a = (jdbctemplate) ctx.getbean ("Jdbctemplatea"); JdbcTemplate B = (jdbctemplate) ctx.getbean ("Jdbctemplateb"); try {Transaction tx = Txm.createtransaction (" Tx-name-define ", 10000); A.update (" INSERT into jta_temp (value) VALUES (' "+ av +" ') "); B.update (" INSERT INTO jta_temp (valu e) VALUES (' + BV + ') '); Tx.commit ();} catch (NotSupportedException | SystemException | securityexception| rollbackexception | heuristicmixedexception| Heuristicrollbackexception e) {e.printstacktrace ();}}

3.3 Test with Atomikosusertransaction Bean object (Testatomikos3.java Modify EXE method in Testatomikos1.java)

@Testpublic void Test1 () {exe ("abc", "abc");} @Testpublic void Test2 () {exe ("123", "123 =");}


Public void exe (STRING&NBSP;AV,&NBSP;STRING&NBSP;BV)  {atomikosdatasourcebean adsa =   (Atomikosdatasourcebean)  ctx.getbean ("a"); atomikosdatasourcebean adsb =  (Atomikosdatasourcebean)  ctx.getbean ("B"); connection conna; connection connb; usertransaction utx =  (usertransaction)  ctx.getbean ("Atomikosusertransaction"); try { Utx.begin (); Conna = adsa.getconnection (); Connb = adsb.getconnection (); Conna.preparestatement ("insert into jta_temp  (value)  values ('"  + av +  "')"). Execute (); Connb.preparestatement ("insert into jta_temp  (value)  values ('"  +  bv +  "')"). Execute (); Utx.commit ();}  catch  (sqlexception | notsupportedexception | systemexception|  securityexception | illegalstateexception | rollbackexception|  Heuristicmixedexception | heuristicrollbackexception e)  {e.printstacktrace ();}} 


Test with the above three usertransaction, where the Test1 method is executed successfully, and the Test2 method is failed (because the length of the inserted value exceeds the field length limit). After analysis, if distributed things are controlled correctly, the values written in the database are not numeric values written for two different tables. :

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6D/ED/wKiom1Vu_tiwRFwEAAHJhvqOEPQ597.jpg "title=" Db-value.png "alt=" Wkiom1vu_tiwrfweaahjhvqoepq597.jpg "/>


During the testing process, the effect of distributed transaction control is really achieved by comparison.


About the JTA principle article began to mention that article, written in very detailed and clear, can be read and understand carefully.

This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1658102

Implementation of Atomikos and spring integration practice with Distributed transaction JTA

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.