分布式事务

    基于Java编码方式使用分布式事务

    1. try (Connection connection = dataSource.getConnection()) { // dataSource的类型为ShardingDataSource
    2. connection.setAutoCommit(false);
    3. PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
    4. preparedStatement.setObject(1, i);
    5. preparedStatement.setObject(2, "init");
    6. connection.commit();
    7. }

    引入Maven依赖

    1. <dependency>
    2. <groupId>org.apache.shardingsphere</groupId>
    3. <artifactId>sharding-jdbc-spring-namespace</artifactId>
    4. <version>${shardingsphere.version}</version>
    5. </dependency>
    6. <!-- 使用XA事务时,需要引入此模块 -->
    7. <dependency>
    8. <groupId>org.apache.shardingsphere</groupId>
    9. <artifactId>sharding-transaction-xa-core</artifactId>
    10. <version>${shardingsphere.version}</version>
    11. </dependency>
    12. <!-- 使用BASE事务时,需要引入此模块 -->
    13. <dependency>
    14. <groupId>org.apache.shardingsphere</groupId>
    15. <artifactId>sharding-transaction-base-seata-at</artifactId>
    16. <version>${sharding-sphere.version}</version>

    业务代码中使用分布式事务

    1. @Transactional
    2. @ShardingTransactionType(TransactionType.XA) // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    3. public void insert() {
    4. jdbcTemplate.execute("INSERT INTO t_order (user_id, status) VALUES (?, ?)", (PreparedStatementCallback<Object>) preparedStatement -> {
    5. preparedStatement.setObject(1, i);
    6. preparedStatement.setObject(2, "init");
    7. preparedStatement.executeUpdate();
    8. });
    9. }

    引入Maven依赖

    1. <dependency>
    2. <groupId>org.apache.shardingsphere</groupId>
    3. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    4. <version>${shardingsphere.version}</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.apache.shardingsphere</groupId>
    8. <artifactId>sharding-transaction-xa-core</artifactId>
    9. <version>${shardingsphere.version}</version>
    10. </dependency>
    11. <!-- 使用BASE事务时,需要引入此模块 -->
    12. <dependency>
    13. <groupId>org.apache.shardingsphere</groupId>
    14. <artifactId>sharding-transaction-base-seata-at</artifactId>
    15. </dependency>

    业务代码中使用分布式事务

    1. @Transactional
    2. @ShardingTransactionType(TransactionType.XA) // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    3. public void insert() {
    4. jdbcTemplate.execute("INSERT INTO t_order (user_id, status) VALUES (?, ?)", (PreparedStatementCallback<Object>) preparedStatement -> {
    5. preparedStatement.setObject(1, i);
    6. preparedStatement.setObject(2, "init");
    7. preparedStatement.executeUpdate();
    8. });
    9. }

    XA事务管理器参数配置(可选)

    也可以通过在项目的classpath中添加jta.properties来定制化Atomikos配置项。具体的配置规则请参考Atomikos的。

    2.在每一个分片数据库实例中执创建undo_log表(以MySQL为例)

    1. CREATE TABLE IF NOT EXISTS `undo_log`
    2. (
    3. `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
    4. `branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
    5. `xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
    6. `context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    7. `rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
    8. `log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
    9. `log_created` DATETIME NOT NULL COMMENT 'create datetime',
    10. `log_modified` DATETIME NOT NULL COMMENT 'modify datetime',
    11. PRIMARY KEY (`id`),
    12. UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
    13. ) ENGINE = InnoDB
    14. AUTO_INCREMENT = 1

    4.根据实际场景修改seata的file.conf和registry.conf文件