提交 527a09c4 编写于 作者: C cherrylzhao

complete YamlConfigurationTransactionExample for XA.

上级 43448ff3
......@@ -50,18 +50,51 @@ public class YamlConfigurationTransactionExample {
commonService.cleanEnvironment();
}
private static DataSource getDataSource(final ShardingType shardingType) throws SQLException, IOException {
switch (shardingType) {
case SHARDING_DATABASES_AND_TABLES:
return YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases-tables.yaml"));
case MASTER_SLAVE:
return YamlMasterSlaveDataSourceFactory.createDataSource(getFile("/META-INF/master-slave.yaml"));
default:
throw new UnsupportedOperationException(shardingType.name());
}
}
private static File getFile(final String fileName) {
return new File(Thread.currentThread().getClass().getResource(fileName).getFile());
}
private static CommonService getCommonService(final DataSource dataSource) {
return new CommonServiceImpl(new OrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource));
}
private static void processXATransaction(final DataSource dataSource, final CommonService commonService) throws SQLException {
TransactionTypeHolder.set(TransactionType.XA);
System.out.println("------ start succeed transaction ------");
try (Connection connection = dataSource.getConnection()) {
insertSuccess(connection, commonService);
connection.commit();
commonService.printData();
}
truncateTable(dataSource);
System.out.println("------ end succeed transaction ------");
System.out.println("------ start failure transaction ------");
Connection connection = dataSource.getConnection();
try {
insertSuccess(connection, commonService);
throw new SQLException("exception occur!");
} catch (final SQLException ex) {
connection.rollback();
}
commonService.printData();
System.out.println("------ end failure transaction ------");
truncateTable(dataSource);
}
private static void insertSuccess(final Connection connection, final CommonService commonService) throws SQLException {
connection.setAutoCommit(false);
for (int i = 0; i < 100; i++) {
for (int i = 0; i < 10; i++) {
Order order = new Order();
order.setUserId(i);
order.setStatus("INIT");
......@@ -70,7 +103,6 @@ public class YamlConfigurationTransactionExample {
commonService.printData();
}
private static Long insertOrder(final Connection connection, final Order order) {
String sql = "INSERT INTO t_order (user_id, status) VALUES (?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
......@@ -87,22 +119,8 @@ public class YamlConfigurationTransactionExample {
return order.getOrderId();
}
private static DataSource getDataSource(final ShardingType shardingType) throws SQLException, IOException {
switch (shardingType) {
case SHARDING_DATABASES_AND_TABLES:
return YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases-tables.yaml"));
case MASTER_SLAVE:
return YamlMasterSlaveDataSourceFactory.createDataSource(getFile("/META-INF/master-slave.yaml"));
default:
throw new UnsupportedOperationException(shardingType.name());
}
}
private static File getFile(final String fileName) {
return new File(Thread.currentThread().getClass().getResource(fileName).getFile());
}
private static CommonService getCommonService(final DataSource dataSource) {
return new CommonServiceImpl(new OrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource));
private static void truncateTable(final DataSource dataSource) {
OrderRepositoryImpl orderRepository = new OrderRepositoryImpl(dataSource);
orderRepository.truncateTable();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册