|
| 1 | +package io.nononi.xademo; |
| 2 | + |
| 3 | +import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory; |
| 4 | +import org.apache.shardingsphere.transaction.core.TransactionType; |
| 5 | +import org.apache.shardingsphere.transaction.core.TransactionTypeHolder; |
| 6 | + |
| 7 | +import javax.sql.DataSource; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.sql.Connection; |
| 11 | +import java.sql.PreparedStatement; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.sql.Statement; |
| 14 | + |
| 15 | +public class XaDemo { |
| 16 | + public static void main(String[] args) throws IOException, SQLException { |
| 17 | + DataSource dataSource = getShardingDatasource(); |
| 18 | + cleanupData(dataSource); |
| 19 | + |
| 20 | + TransactionTypeHolder.set(TransactionType.XA); |
| 21 | + |
| 22 | + Connection conn = dataSource.getConnection(); |
| 23 | + String sql = "insert into t_order (user_id, order_id) VALUES (?, ?);"; |
| 24 | + |
| 25 | + System.out.println("First XA Start insert data"); |
| 26 | + try (PreparedStatement statement = conn.prepareStatement(sql)) { |
| 27 | + conn.setAutoCommit(false); |
| 28 | + //会插入到ds1的t_order_1; |
| 29 | + statement.setLong(1, 1); |
| 30 | + statement.setLong(2, 1); |
| 31 | + statement.executeUpdate(); |
| 32 | + //会插入到ds0的t_order_0; |
| 33 | + statement.setLong(1, 2); |
| 34 | + statement.setLong(2, 2); |
| 35 | + statement.executeUpdate(); |
| 36 | + |
| 37 | + conn.commit(); |
| 38 | + } |
| 39 | + |
| 40 | + System.out.println("First XA insert successful"); |
| 41 | + |
| 42 | + System.out.println("Second XA Start insert data"); |
| 43 | + try (PreparedStatement statement = conn.prepareStatement(sql)) { |
| 44 | + conn.setAutoCommit(false); |
| 45 | + //会插入到ds1的t_order_1; |
| 46 | + //回滚后应该两个数据库中都没有插入 |
| 47 | + statement.setLong(1, 3); |
| 48 | + statement.setLong(2, 3); |
| 49 | + statement.executeUpdate(); |
| 50 | + //会插入到ds0的t_order_0; |
| 51 | + statement.setLong(1, 4); |
| 52 | + statement.setLong(2, 4); |
| 53 | + statement.executeUpdate(); |
| 54 | + conn.rollback(); |
| 55 | + |
| 56 | + } finally { |
| 57 | + conn.close(); |
| 58 | + } |
| 59 | + System.out.println("Second XA insert rollback"); |
| 60 | + } |
| 61 | + |
| 62 | + private static void cleanupData(DataSource dataSource) { |
| 63 | + System.out.println("Delete all Data"); |
| 64 | + try (Connection conn = dataSource.getConnection(); Statement statement = conn.createStatement()) { |
| 65 | + statement.execute("delete from t_order;"); |
| 66 | + conn.commit(); |
| 67 | + } catch (SQLException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + System.out.println("Delete all Data successful"); |
| 71 | + } |
| 72 | + |
| 73 | + static private DataSource getShardingDatasource() throws IOException, SQLException { |
| 74 | + String fileName = "C:\\Users\\NoNo_Ni\\Desktop\\Java进阶训练营\\javacourse\\06db\\work6ofweek8\\src\\main\\resources\\sharding-databases-tables.yaml"; |
| 75 | + File yamlFile = new File(fileName); |
| 76 | + return YamlShardingSphereDataSourceFactory.createDataSource(yamlFile); |
| 77 | + } |
| 78 | +} |
0 commit comments