In some business scenarios and technical architecture, cross-Library transactions Inevitably, this time how to unify the management of transactions, to ensure that the strong consistency of the transaction is the entire system stable, usable cornerstone. Some middleware such as Tuxedo, CICS is by virtue of this ability to occupy the financial, telecommunications, banking and other large market, and fished out a lot of profits.
In Java, Atomikos as an open source project (with a commercial version), providing the ability to manage distributed transactions. This article mainly validates the simple business scenario to test the distributed transactional consistency under Atomikos management.
Prepare two MySQL instances on a single machine first
1 Download Install the first instance of http://mirrors.sohu.com/mysql/
Mysqld-install net start MySQL mysql-u root-p
2 Installing the second instance copy a MySQL directory modify the port number of the My-default.ini, and so on: Basedir = D:/program files/mysql/mysql Server datadir = D:/program files/my Sql/mysql server/data port = 3307
Run mysqld--install mysql2--defaults-file= "D:\Program files\mysql\mysql server\bin\my-default.ini" net start MYSQL2
Create a table of data in two libraries:
DROP TABLE IF EXISTS account
; CREATE TABLE account
( UserId
int (one) not NULL, Money
bigint (a) NOT null) Engine=innodb DEFAULT Charset=utf8;
--Records of account
INSERT into account
VALUES (' 123 ', ' 0 ');
Then implement the code logic:
The code logic is to implement a random transfer of a certain amount from 1 libraries to 2 libraries, or from 2 to 1 libraries.
During the transfer process, a certain probability is installed that an exception occurs and the entire transaction is rolled back. After running for a period of time, see whether the amount of two libraries can be reconciled.
See GitHub for specific code:
Https://github.com/wfb29/atomikos-test
Management--atomikos of Distributed transaction