diff --git a/examples/example-core/example-api/src/main/java/org/apache/shardingsphere/example/core/api/ExampleExecuteTemplate.java b/examples/example-core/example-api/src/main/java/org/apache/shardingsphere/example/core/api/ExampleExecuteTemplate.java index adbe0e84bce977dde6d5d0a42217b91b00ebf979..93efe772cd1a59414219c82f2e670a8f05b24dc1 100644 --- a/examples/example-core/example-api/src/main/java/org/apache/shardingsphere/example/core/api/ExampleExecuteTemplate.java +++ b/examples/example-core/example-api/src/main/java/org/apache/shardingsphere/example/core/api/ExampleExecuteTemplate.java @@ -31,4 +31,13 @@ public final class ExampleExecuteTemplate { exampleService.cleanEnvironment(); } } + + public static void runFailure(final ExampleService exampleService) throws SQLException { + try { + exampleService.initEnvironment(); + exampleService.processFailure(); + } finally { + exampleService.cleanEnvironment(); + } + } } diff --git a/examples/pom.xml b/examples/pom.xml index 1c6e42e84a44f98d1d86254b6b02838c8a47db13..6d86f73a36f3d187d9b8bf794291a8124073041e 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -53,7 +53,8 @@ 1.3.0 1.0.0.Final 4.3.11.Final - + + 2.1.3 1.0.0 4.12 @@ -101,6 +102,11 @@ shardingsphere-transaction-xa-core ${shardingsphere.version} + + org.apache.shardingsphere + shardingsphere-transaction-xa-bitronix + ${shardingsphere.version} + org.apache.shardingsphere shardingsphere-transaction-base-seata-at @@ -217,6 +223,11 @@ mybatis-spring-boot-starter ${mybatis-spring.version} + + org.codehaus.btm + btm + ${btm.version} + io.seata seata-rm-datasource diff --git a/examples/shardingsphere-jdbc-example/transaction-example/pom.xml b/examples/shardingsphere-jdbc-example/transaction-example/pom.xml index 5736bc2dc3978543044817e502ce71766c475cc0..7681ec6f81fb893c35de236e9bce269b3a54ba9d 100644 --- a/examples/shardingsphere-jdbc-example/transaction-example/pom.xml +++ b/examples/shardingsphere-jdbc-example/transaction-example/pom.xml @@ -32,9 +32,10 @@ transaction-2pc-xa-raw-jdbc-example - transaction-base-seata-raw-jdbc-example + transaction-2pc-xa-bitronix-raw-jdbc-example transaction-2pc-xa-spring-boot-example transaction-2pc-xa-spring-namespace-example + transaction-base-seata-raw-jdbc-example transaction-base-seata-spring-boot-example diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/pom.xml b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..75573987de9a5f2722b6c017631f17180e8dbf37 --- /dev/null +++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/pom.xml @@ -0,0 +1,54 @@ + + + + + 4.0.0 + + org.apache.shardingsphere.example + transaction-example + 5.0.0-RC1-SNAPSHOT + + transaction-2pc-xa-bitronix-raw-jdbc-example + Example::transaction::2pc-xa-bitronix-raw-jdbc + + + + org.apache.shardingsphere.example + example-raw-jdbc + ${project.version} + + + org.apache.shardingsphere + shardingsphere-jdbc-core + + + org.apache.shardingsphere + shardingsphere-transaction-xa-core + + + org.apache.shardingsphere + shardingsphere-transaction-xa-bitronix + + + org.codehaus.btm + btm + + + diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/ExampleMain.java b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/ExampleMain.java new file mode 100644 index 0000000000000000000000000000000000000000..dab1a265101bd6e0282c843ebe8b477d4371a66e --- /dev/null +++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/ExampleMain.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.example.transaction.xa.bitronix.raw.jdbc; + +import java.io.File; +import javax.sql.DataSource; +import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory; +import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate; +import org.apache.shardingsphere.example.core.api.service.ExampleService; + +public class ExampleMain { + + public static void main(final String[] args) throws Exception { + DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases-tables.yaml")); + ExampleExecuteTemplate.run(getExampleService(dataSource)); + ExampleExecuteTemplate.runFailure(getExampleService(dataSource)); + } + + private static File getFile(final String fileName) { + return new File(ExampleMain.class.getResource(fileName).getFile()); + } + + private static ExampleService getExampleService(DataSource dataSource) { + return new OrderServiceImpl(dataSource); + } +} diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/OrderServiceImpl.java b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/OrderServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..25a3fee6147567987264376fb3d288111cef6419 --- /dev/null +++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/OrderServiceImpl.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.example.transaction.xa.bitronix.raw.jdbc; + +import org.apache.shardingsphere.example.core.api.service.ExampleService; +import org.apache.shardingsphere.transaction.core.TransactionType; +import org.apache.shardingsphere.transaction.core.TransactionTypeHolder; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +class OrderServiceImpl implements ExampleService { + + private final DataSource dataSource; + + OrderServiceImpl(final DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public void initEnvironment() throws SQLException { + try (Connection connection = dataSource.getConnection()) { + Statement statement = connection.createStatement(); + statement.execute("DROP TABLE IF EXISTS t_order"); + statement.execute("CREATE TABLE t_order (order_id BIGINT AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))"); + } + int quantity = selectAll(); + System.out.printf("CREATE t_order IF NOT EXIST (count: %d)\n", quantity); + } + + @Override + public void cleanEnvironment() throws SQLException { + try (Connection connection = dataSource.getConnection()) { + Statement statement = connection.createStatement(); + statement.execute("DROP TABLE IF EXISTS t_order"); + } + System.out.println("DROP t_order"); + } + + @Override + public void processSuccess() throws SQLException { + System.out.println("-------------------- Process Start ---------------------"); + TransactionTypeHolder.set(TransactionType.XA); + try (Connection connection = dataSource.getConnection()) { + connection.setAutoCommit(false); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)"); + doInsert(preparedStatement); + connection.commit(); + System.out.println("INSERT 10 orders success"); + } + int quantity = selectAll(); + System.out.printf("Commit, expect:10, actual:%d \n", quantity); + printData(); + System.out.println("-------------------- Process End -----------------------"); + } + + @Override + public void processFailure() throws SQLException { + System.out.println("-------------------- Process Start ---------------------"); + TransactionTypeHolder.set(TransactionType.XA); + try (Connection connection = dataSource.getConnection()) { + connection.setAutoCommit(false); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO t_order (user_id, status) VALUES (?, ?)"); + doInsert(preparedStatement); + connection.rollback(); + System.out.println("INSERT 10 orders failed"); + } + int quantity = selectAll(); + System.out.printf("Rollback, expect:0, actual:%d \n", quantity); + printData(); + System.out.println("-------------------- Process End -----------------------"); + } + + @Override + public void printData() throws SQLException { + System.out.println("Print Order Data"); + try (Connection connection = dataSource.getConnection()) { + Statement statement = connection.createStatement(); + statement.executeQuery("SELECT order_id, user_id, status FROM t_order"); + ResultSet resultSet = statement.getResultSet(); + while (resultSet.next()) { + String orderId = resultSet.getString("order_id"); + String userId = resultSet.getString("user_id"); + String status = resultSet.getString("status"); + System.out.printf("orderId = %s, userId = %s, status = %s \n", orderId, userId, status); + } + } + } + + private void doInsert(final PreparedStatement preparedStatement) throws SQLException { + for (int i = 0; i < 10; i++) { + preparedStatement.setObject(1, i); + preparedStatement.setObject(2, "init"); + preparedStatement.executeUpdate(); + } + } + + private int selectAll() throws SQLException { + int result = 0; + try (Connection connection = dataSource.getConnection()) { + Statement statement = connection.createStatement(); + statement.executeQuery("SELECT COUNT(1) AS count FROM t_order"); + ResultSet resultSet = statement.getResultSet(); + while (resultSet.next()) { + result = resultSet.getInt(1); + } + } + return result; + } +} diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c010cb3bb5219af94fa520027b8b0fd5f7fb254d --- /dev/null +++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml @@ -0,0 +1,61 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +dataSources: + ds_0: !!com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.jdbc.Driver + jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 + username: root + password: + autoCommit: false + ds_1: !!com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.jdbc.Driver + jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 + username: root + password: + autoCommit: false + +rules: +- !SHARDING + tables: + t_order: + actualDataNodes: ds_${0..1}.t_order_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithm: + type: INLINE + props: + algorithm.expression: t_order_${order_id % 2} + keyGenerator: + type: SNOWFLAKE + column: order_id + props: + worker.id: 123 + + defaultDatabaseStrategy: + standard: + shardingColumn: user_id + shardingAlgorithm: + type: INLINE + props: + algorithm.expression: ds_${user_id % 2} + defaultTableStrategy: + none: + +props: + sql.show: false diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/logback.xml b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/logback.xml new file mode 100644 index 0000000000000000000000000000000000000000..459992b6fdec66985c0e24e0a15c0b68cb9f74ba --- /dev/null +++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/logback.xml @@ -0,0 +1,38 @@ + + + + + + + + ${log.context.name} + + + + ${log.pattern} + + + + + + + + + + +