Java事務(七) - 分散式交易 - spring + JTA + jotm

來源:互聯網
上載者:User

一. 前言:

在寫這篇部落格之前,我們需要弄清楚兩個概念:本地事務和分散式交易。

本地事務:只處理單一資料來源,比如單個資料庫。

分散式交易:處理多種異構的資料來源, 比如某個業務操作中同時包含JDBC和JMS或者某個操作需要訪問多個不同的資料庫。


Java通過JTA完成分散式交易, JTA本身只是一種規範, 本篇部落格將使用JOTM作為實現, 後續還會使用Atomikos實現。


二. 業務背景:

假定我們有這樣一個需求:當我們建立一個使用者的時候需要往一個DB中插入一條使用者記錄,還需要往另一個DB中記錄日誌

因為是不同的DB操作,所以這裡就涉及到分散式交易的處理。


三. 代碼實現:

1. 代碼結構圖:



2. 建表語句:

[sql]  view plain  copy   create database log;   DROP TABLE IF EXISTS `log`;   CREATE TABLE `log` (     `id` varchar(20) NOT NULL,     `content` varchar(100) default NULL,     PRIMARY KEY  (`id`)   );      create database user;   DROP TABLE IF EXISTS `user`;   CREATE TABLE `user` (     `id` varchar(20) NOT NULL,     `name` varchar(40) default NULL,     PRIMARY KEY  (`id`)   );  
3. 設定檔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   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.