Use Java API
Background
With ShardingSphere-JDBC, XA and BASE mode transactions can be used through the API.
Prerequisites
Introducing Maven dependency
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<!-- This module is required when using XA transactions -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-xa-core</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<!-- This module is required when using the Narayana mode with XA transactions -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-xa-narayana</artifactId>
<version>${project.version}</version>
</dependency>
<!-- This module is required when using BASE transactions -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-base-seata-at</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
Procedure
Perform the business logic using transactions
Sample
// Use ShardingSphereDataSource to get a connection and perform transaction operations.
try (Connection connection = dataSource.getConnection()) {
connection.setAutoCommit(false);
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)");
preparedStatement.setObject(1, 1000);
preparedStatement.setObject(2, "init");
preparedStatement.executeUpdate();
connection.commit();
}
当前内容版权归 ShardingSphere 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 ShardingSphere .