The spring+jta of Distributed transaction operation

Source: Internet
Author: User
Tags getmessage

What is a distributed transaction? On the internet to find a relatively easy to understand the "definition."

Distributed transactions refer to the participants of a transaction, the server supporting the transaction, the resource manager, and the transaction manager on the different nodes of the distributed system, accessing and updating data on two or more network computer resources, and processing the data of two or more networked computers as a whole. such as transfers between different bank accounts.

Most of the reasons for exposure to JTA in a project are due to the need to operate multiple databases in the project, and to ensure the atomicity of the operations and the consistency of operations across multiple databases.

The SPRINGMVC (struts) +spring+hibernate (JPA) +JTA should be used in a formal project, and the basic testing framework is now done with SPRING+JTA. Now let's look at the code

Applicationcontext-jta.xml

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns= "Http://www.springframework.org/schema/beans" 3 xmln S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" 4 xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" 5 xsi: schemalocation= "Http://www.springframework.org/schema/beans 6 Http://www.springframework.org/schema/beans/sprin G-beans-2.5.xsd 7 Http://www.springframework.org/schema/context 8 Http://www.springframework.org/sch Ema/context/spring-context-2.5.xsd 9 Http://www.springframework.org/schema/aop10 HTTP://WWW.SPRINGFR Amework.org/schema/aop/spring-aop-2.5.xsd11 Http://www.springframework.org/schema/tx HTTP://WWW.S Pringframework.org/schema/tx/spring-tx-2.5.xsd ">13 <!--Jotm Local instance-->15 <bean id=" Jotm "class=" org. Springframework.transaction.jta.JoTmfactorybean "/>16 <!--JTA transaction manager-->19 <bean id=" Txmanager "class=" Org.springframewor K.transaction.jta.jtatransactionmanager ">21 <property name=" usertransaction "ref=" Jotm "></property&gt </bean>23 <!--xapool configuration, which contains an XA data source corresponding to SSHDB database-->25 <bean id= "db1" class= "Org.enhydra".              Jdbc.pool.StandardXAPoolDataSource "destroy-method=" Shutdown ">27 <property name=" DataSource ">28                 <!--internal XA data source-->29 <bean class= "Org.enhydra.jdbc.standard.StandardXADataSource" 30                 destroy-method= "Shutdown" >31 <property name= "TransactionManager" ref= "Jotm"/>32                     <property name= "drivername" value= "Com.mysql.jdbc.Driver"/>33 <property name= "url" 34             Value= "Jdbc:mysql://192.168.1.28:3306/sshdb?useunicode=true&amp;characterencoding=utf-8"/>35 </bean>36         </property>37 <property name= "user" value= "root"/>38 <property name= "Password" va Lue= "123456"/>39 </bean>40 <!--Another xapool configuration, internally containing another XA data source, corresponding Babasport database-->42 <bean id= "  DB2 "class=" Org.enhydra.jdbc.pool.StandardXAPoolDataSource "destroy-method=" Shutdown ">44 <property Name= "DataSource" >45 <bean class= "Org.enhydra.jdbc.standard.StandardXADataSource"-Des troy-method= "Shutdown" >47 <property name= "TransactionManager" ref= "Jotm"/>48 &lt                     ;p roperty name= "drivername" value= "Com.mysql.jdbc.Driver"/>49 <property name= "url" 50             Value= "Jdbc:mysql://192.168.1.28:3306/babasport?useunicode=true&amp;characterencoding=utf-8"/>51 </bean>52 </property>53 <property name= "user" value= "root"/>54 <property na Me= "password" value= "123456 "/>55 </bean>56 <!--Configuring the spring JDBC template to access the SSHDB data source-->58 <bean id=" Sshdbtemplate "class= "Org.springframework.jdbc.core.JdbcTemplate" >59 <property name= "DataSource" ref= "DB1" ></property>6 0 </bean>61 <!--Configure the spring JDBC template to access the Babasport data source-->63 <bean id= "babasporttemplate" class= "or     G.springframework.jdbc.core.jdbctemplate ">64 <property name=" DataSource "ref=" DB2 "></property>65 </bean>66 </beans>

Small bet: Spring-tx-3.2.4.jar There is no Org.springframework.transaction.jta.JotmFactoryBean class, if you can choose Spring-tx-2.5.6.jar, Or build this class yourself.

Next, look at the code for the DAO layer test

 1 1 @Resource (name = "Txmanager") 2 2 private Jtatransactionmanager Txmanager; 3 9 4 protected jdbctemplate babasport_jdbctemplate; 5 6/** 7 * SSHDB SQL JdbcTemplate 8 */9 protected JdbcTemplate ssh_jdbctemplate;10 16 /**12 * Babasport SQL JDBCTEMPLATE13 * @return15 * */16 public Jdbctem Plate Getbabasport_jdbctemplate () {babasport_jdbctemplate;18-}19-Jdbctempl Ate Getssh_jdbctemplate () {ssh_jdbctemplate;22}23 @Resource (name = "Babasporttem Plate ") public void Setbabasport_jdbctemplate (JdbcTemplate babasport_jdbctemplate) {This.babasport_ JdbcTemplate = babasport_jdbctemplate;27}28 @Resource (name = "Sshdbtemplate"), public void SE Tssh_jdbctemplate (JdbcTemplate ssh_jdbctemplate) {Notoginseng this.ssh_jdbctemplate = ssh_jdbctemplate;32 38}33 39 34 40/**35 41 * Contents of table with two databases simultaneously modified * PNS * @throws RollbackException38-*/39 PU Blic void Updatemultiple () {(null = = This.txmanager) {System.out.println ("Txmanag Er is empty "); return;44}45 Wuyi usertransaction usertx = this.txManager.getUserTransact         Ion (), if (null = = Usertx) {System.out.println ("Usertx is Empty"); Return;50 56 }51------try {usertx.begin ();             Emplate57. Execute ("Update wyuser set password= ' wangyong1 ' where id=8"); 58 64 59 65 60 66 This.babasport_jdbctemplate61. Execute ("Update brand set Name= ' wangyong28 ' where code= ' 14ac8d5b-             D19c-40e9-97ea-d82dfbcd84c6 ' "); Usertx.commit (); catch (Exception e) {65 71 System.out.println ("Catch an exception, Roll back "+ e.getmessage ()"); E.printstacktrace (); try {usertx.ro Llback (); IllegalStateException E1) {System.out.println ("Illegalstateexcep tion: "+ e1.getmessage ()); SecurityException E1) {System.out.println Rityexception: "+ e1.getmessage ()); catch (SystemException E1) {System.out.printl         N ("SystemException:" + e1.getmessage ()); 77 Bayi}76//SYSTEM.OUT.PRINTLN ("SQL statement operation failed"); 83 }78 84}

If you deliberately write the latter UPDATE statement incorrectly, you will find that the rollback is executed, and the operation of the preceding statement will not take effect. This is the basic simple framework.

In fact, before also testing the framework of the next Spring+jpa+jta model, but found that in the creation of the model layer entity class, there will be a problem, the built entity class mapped to all the database, so in JPA using the attribute <property name= " Packagestoscan "value=" package name "/> This approach does solve the problem of mapping entity class entities, but it seems that there are other problems to be studied ....

The spring+jta of Distributed transaction operation

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.