Java Transaction (vii)-Distributed transactions-Spring + JTA + JOTM

Source: Internet
Author: User

I. Preface:

Before writing this blog, we need to understand two concepts: local transaction and distributed transaction.

Local transactions: Only a single data source, such as a single database, is processed.

Distributed transactions: Handling a variety of heterogeneous data sources, such as including JDBC and JMS at the same time in a business operation, or an operation that requires access to multiple different databases.


Java through the JTA to complete the distributed transaction, JTA itself is just a specification, this blog will use JOTM as the implementation, follow-up will also use Atomikos implementation.


two. Business background:

Let's say we have a requirement: when we create a new user, we need to insert a user record into a db, and we need to log in another db.

Because it is a different db operation, it involves the processing of distributed transactions.


three. Code implementation:

1. Code structure diagram:



2. Table statement:

[SQL] view plain copy create database log;   DROP TABLE IF EXISTS ' log ';      CREATE TABLE ' log ' (' id ' varchar ' NOT NULL, ' content ' varchar () default NULL, PRIMARY KEY (' id '));   Create database user;   DROP TABLE IF EXISTS ' user '; CREATE TABLE ' user ' (' id ' varchar ' NOT NULL, ' name ' varchar () default NULL, PRIMARY KEY (' id '));
3. configuration file Applicationcontext.xml

[HTML]  View plain  copy   <?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.xsd                          http://www.springframework.org/schema/tx                           http://www.springframework.org/schema/tx/spring-tx.xsd                          http:// www.springframework.org/schema/aop   

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.