├── README.md ├── src └── main │ └── java │ └── transaction │ ├── README.md │ ├── XaCommitDemo.java │ ├── XaRollbackDemo.java │ └── BaseDemo.java └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # xa-transaction-demo 2 | xa事务demo 3 | -------------------------------------------------------------------------------- /src/main/java/transaction/README.md: -------------------------------------------------------------------------------- 1 | # 准备工作 2 | 3 | 需要在数据库 **jdbc:mysql://localhost:3306/db1** 中创建下面的表 4 | 5 | ``` 6 | DROP TABLE IF EXISTS `t_order`; 7 | CREATE TABLE `t_order` ( 8 | `id` int(11) NOT NULL AUTO_INCREMENT, 9 | `uid` int(11) NOT NULL, 10 | `price` int(11) NOT NULL, 11 | `status` int(11) NOT NULL, 12 | PRIMARY KEY (`id`) 13 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 14 | ``` 15 | 16 | 需要在数据库 **jdbc:mysql://localhost:3306/db2** 中创建下面的表 17 | 18 | ``` 19 | DROP TABLE IF EXISTS `t_log`; 20 | CREATE TABLE `t_log` ( 21 | `id` int(11) NOT NULL AUTO_INCREMENT, 22 | `uid` int(11) NOT NULL, 23 | `price` int(11) NOT NULL, 24 | `status` int(11) NOT NULL, 25 | PRIMARY KEY (`id`) 26 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 27 | ``` -------------------------------------------------------------------------------- /src/main/java/transaction/XaCommitDemo.java: -------------------------------------------------------------------------------- 1 | package transaction; 2 | 3 | import java.sql.Connection; 4 | import java.sql.Statement; 5 | 6 | /** 7 | * @author ash 8 | */ 9 | public class XaCommitDemo extends BaseDemo { 10 | 11 | public static void main(String[] args) throws Exception { 12 | init(); 13 | 14 | tm.begin(); // 开启xa事务 15 | 16 | Connection c1 = ds1.getConnection(); 17 | Statement stmt = c1.createStatement(); 18 | stmt.execute("insert into t_order(uid, price, status) values(1, 2, 1)"); 19 | stmt.close(); 20 | c1.close(); 21 | 22 | Connection c2 = ds2.getConnection(); 23 | Statement stmt2 = c2.createStatement(); 24 | stmt2.execute("insert into t_log(uid, price, status) values(1, 2, 1)"); 25 | stmt2.close(); 26 | c2.close(); 27 | 28 | tm.commit(); // 提交xa事务 29 | 30 | shutdown(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/transaction/XaRollbackDemo.java: -------------------------------------------------------------------------------- 1 | package transaction; 2 | 3 | import java.sql.Connection; 4 | import java.sql.Statement; 5 | 6 | /** 7 | * @author ash 8 | */ 9 | public class XaRollbackDemo extends BaseDemo { 10 | 11 | public static void main(String[] args) throws Exception { 12 | init(); 13 | 14 | tm.begin(); // 开启xa事务 15 | 16 | Connection c1 = ds1.getConnection(); 17 | Statement stmt = c1.createStatement(); 18 | stmt.execute("insert into t_order(uid, price, status) values(1, 2, 1)"); 19 | stmt.close(); 20 | c1.close(); 21 | 22 | Connection c2 = ds2.getConnection(); 23 | Statement stmt2 = c2.createStatement(); 24 | stmt2.execute("insert into t_log(uid, price, status) values(1, 2, 1)"); 25 | stmt2.close(); 26 | c2.close(); 27 | 28 | tm.rollback(); // 回滚xa事务 29 | 30 | shutdown(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | transaction 8 | xa-transaction-demo 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | com.atomikos 14 | transactions 15 | 4.0.4 16 | 17 | 18 | com.atomikos 19 | transactions-jta 20 | 4.0.4 21 | 22 | 23 | com.atomikos 24 | transactions-jdbc 25 | 4.0.4 26 | 27 | 28 | javax.transaction 29 | jta 30 | 1.1 31 | 32 | 33 | mysql 34 | mysql-connector-java 35 | 5.1.29 36 | 37 | 38 | org.springframework 39 | spring-jdbc 40 | 4.3.7.RELEASE 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/main/java/transaction/BaseDemo.java: -------------------------------------------------------------------------------- 1 | package transaction; 2 | 3 | import com.atomikos.icatch.jta.UserTransactionManager; 4 | import com.atomikos.jdbc.AtomikosDataSourceBean; 5 | 6 | import javax.sql.DataSource; 7 | import javax.transaction.TransactionManager; 8 | import java.util.Properties; 9 | 10 | /** 11 | * @author ash 12 | */ 13 | public class BaseDemo { 14 | 15 | // Atomikos相关 16 | private static UserTransactionManager utm; 17 | private static AtomikosDataSourceBean adsb1; 18 | private static AtomikosDataSourceBean adsb2; 19 | 20 | // 标准接口 21 | protected static TransactionManager tm; 22 | protected static DataSource ds1; 23 | protected static DataSource ds2; 24 | 25 | // 初始化资源 26 | protected static void init() throws Exception { 27 | utm = new UserTransactionManager(); 28 | utm.init(); 29 | tm = utm; 30 | 31 | adsb1 = new AtomikosDataSourceBean(); 32 | adsb1.setUniqueResourceName("mysql-db1"); 33 | adsb1.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); 34 | Properties p1 = new Properties(); 35 | p1.setProperty("user", "root"); 36 | p1.setProperty("password", "root"); 37 | p1.setProperty("URL", "jdbc:mysql://localhost:3306/db1"); 38 | adsb1.setXaProperties(p1); 39 | ds1 = adsb1; 40 | 41 | adsb2 = new AtomikosDataSourceBean(); 42 | adsb2.setUniqueResourceName("mysql-db2"); 43 | adsb2.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); 44 | Properties p2 = new Properties(); 45 | p2.setProperty("user", "root"); 46 | p2.setProperty("password", "root"); 47 | p2.setProperty("URL", "jdbc:mysql://localhost:3306/db2"); 48 | adsb2.setXaProperties(p2); 49 | ds2 = adsb2; 50 | } 51 | 52 | // 释放资源 53 | protected static void shutdown() { 54 | adsb1.close(); 55 | adsb2.close(); 56 | utm.close(); 57 | } 58 | 59 | } 60 | --------------------------------------------------------------------------------