分布式事务

不使用Spring

引入Maven依赖

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>sharding-jdbc-core</artifactId>
  4. <version>${sharding-sphere.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>
  17. </dependency>

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

  1. TransactionTypeHolder.set(TransactionType.XA); // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
  2. try (Connection connection = dataSource.getConnection()) { // dataSource的类型为ShardingDataSource
  3. connection.setAutoCommit(false);
  4. PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
  5. preparedStatement.setObject(1, i);
  6. preparedStatement.setObject(2, "init");
  7. preparedStatement.executeUpdate();
  8. connection.commit();
  9. }

使用Spring-namespace

引入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>
  17. </dependency>

配置spring-namespace的事务管理器

  1. <!-- 进行ShardingDataSource的相关配置 -->
  2. ...
  3. <!-- 开启自动扫描@ShardingTransactionType注解,使用Spring原生的AOP在类和方法上进行增强 -->
  4. <sharding:tx-type-annotation-driven />
  5. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  6. <property name="dataSource" ref="shardingDataSource" />
  7. </bean>
  8. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  9. <property name="dataSource" ref="shardingDataSource" />
  10. </bean>
  11. <tx:annotation-driven />

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

  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. }

使用Spring-boot

引入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. <!-- 使用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>
  17. </dependency>

配置spring-boot的事务管理器

  1. @Configuration
  2. @EnableTransactionManagement
  3. public class TransactionConfiguration {
  4. @Bean
  5. public PlatformTransactionManager txManager(final DataSource dataSource) {
  6. return new DataSourceTransactionManager(dataSource);
  7. }
  8. @Bean
  9. public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
  10. return new JdbcTemplate(dataSource);
  11. }
  12. }

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

  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事务管理器参数配置(可选)

ShardingSphere默认的XA事务管理器为Atomikos,在项目的logs目录中会生成xa_tx.log, 这是XA崩溃恢复时所需的日志,请勿删除。

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

BASE柔性事务管理器(SEATA-AT配置)

1.按照seata-work-shop中的步骤,下载并启动seata server,参考 Step6 和 Step7即可。

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
  15. DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

3.在classpath中增加seata.conf

  1. client {
  2. application.id = example ## 应用唯一id
  3. transaction.service.group = my_test_tx_group ## 所属事务组
  4. }

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

分布式事务example