不使用Spring
引入Maven依赖
org.apache.shardingsphere
sharding-jdbc-core
${sharding-sphere.version}
org.apache.shardingsphere
sharding-transaction-xa-core
${shardingsphere.version}
org.apache.shardingsphere
sharding-transaction-base-seata-at
${sharding-sphere.version}
基于Java编码方式使用分布式事务
TransactionTypeHolder.set(TransactionType.XA); // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
try (Connection connection = dataSource.getConnection()) { // dataSource的类型为ShardingDataSource
connection.setAutoCommit(false);
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
preparedStatement.setObject(1, i);
preparedStatement.setObject(2, "init");
preparedStatement.executeUpdate();
connection.commit();
}
使用Spring-namespace
引入Maven依赖
org.apache.shardingsphere
sharding-jdbc-spring-namespace
${shardingsphere.version}
org.apache.shardingsphere
sharding-transaction-xa-core
${shardingsphere.version}
org.apache.shardingsphere
sharding-transaction-base-seata-at
${sharding-sphere.version}
配置spring-namespace的事务管理器
...
业务代码中使用分布式事务
@Transactional
@ShardingTransactionType(TransactionType.XA) // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
public void insert() {
jdbcTemplate.execute("INSERT INTO t_order (user_id, status) VALUES (?, ?)", (PreparedStatementCallback
使用Spring-boot
引入Maven依赖
org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
${shardingsphere.version}
org.apache.shardingsphere
sharding-transaction-xa-core
${shardingsphere.version}
org.apache.shardingsphere
sharding-transaction-base-seata-at
${sharding-sphere.version}
配置spring-boot的事务管理器
@Configuration
@EnableTransactionManagement
public class TransactionConfiguration {
@Bean
public PlatformTransactionManager txManager(final DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
业务代码中使用分布式事务
@Transactional
@ShardingTransactionType(TransactionType.XA) // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
public void insert() {
jdbcTemplate.execute("INSERT INTO t_order (user_id, status) VALUES (?, ?)", (PreparedStatementCallback
分布式事务管理器的特有配置
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为例)
CREATE TABLE IF NOT EXISTS `undo_log`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` DATETIME NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME NOT NULL COMMENT 'modify datetime',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
3.在classpath中增加seata.conf
client {
application.id = example ## 应用唯一id
transaction.service.group = my_test_tx_group ## 所属事务组
}
4.根据实际场景修改seata的file.conf和registry.conf文件