You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/07/25 14:51:52 UTC
[shardingsphere] branch master updated: add example for narayana.
(#6190)
This is an automated email from the ASF dual-hosted git repository.
zhaojun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 77ac3e2 add example for narayana. (#6190)
77ac3e2 is described below
commit 77ac3e23894d4d5196b9692bc8f140fe248d388c
Author: TaoZhi <UI...@163.com>
AuthorDate: Sat Jul 25 22:51:42 2020 +0800
add example for narayana. (#6190)
---
examples/pom.xml | 30 ++++-
.../transaction-example/pom.xml | 1 +
.../pom.xml | 66 +++++++++++
.../src/main/java/ExampleMain.java | 39 +++++++
.../src/main/java/OrderServiceImpl.java | 130 +++++++++++++++++++++
.../META-INF/sharding-databases-tables.yaml | 69 +++++++++++
.../src/main/resources/jbossts-properties.xml | 46 ++++++++
.../src/main/resources/logback.xml | 38 ++++++
8 files changed, 418 insertions(+), 1 deletion(-)
diff --git a/examples/pom.xml b/examples/pom.xml
index 2fdc4d7..03b34ed 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -53,8 +53,11 @@
<mybatis-spring.version>1.3.0</mybatis-spring.version>
<jpa.version>1.0.0.Final</jpa.version>
<hibernate.version>4.3.11.Final</hibernate.version>
-
+ <narayana.version>5.9.1.Final</narayana.version>
+ <jboss-transaction-spi.version>7.6.0.Final</jboss-transaction-spi.version>
+ <jboss-logging.version>3.2.1.Final</jboss-logging.version>
<btm.version>2.1.3</btm.version>
+
<seata.version>1.0.0</seata.version>
<junit.version>4.12</junit.version>
@@ -109,6 +112,11 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-transaction-xa-narayana</artifactId>
+ <version>${shardingsphere.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-base-seata-at</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
@@ -229,6 +237,26 @@
<version>${btm.version}</version>
</dependency>
<dependency>
+ <groupId>org.jboss.narayana.jta</groupId>
+ <artifactId>jta</artifactId>
+ <version>${narayana.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.narayana.jts</groupId>
+ <artifactId>narayana-jts-integration</artifactId>
+ <version>${narayana.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss</groupId>
+ <artifactId>jboss-transaction-spi</artifactId>
+ <version>${jboss-transaction-spi.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.logging</groupId>
+ <artifactId>jboss-logging</artifactId>
+ <version>${jboss-logging.version}</version>
+ </dependency>
+ <dependency>
<groupId>io.seata</groupId>
<artifactId>seata-rm-datasource</artifactId>
<version>${seata.version}</version>
diff --git a/examples/shardingsphere-jdbc-example/transaction-example/pom.xml b/examples/shardingsphere-jdbc-example/transaction-example/pom.xml
index 7681ec6..5f88d78 100644
--- a/examples/shardingsphere-jdbc-example/transaction-example/pom.xml
+++ b/examples/shardingsphere-jdbc-example/transaction-example/pom.xml
@@ -33,6 +33,7 @@
<modules>
<module>transaction-2pc-xa-raw-jdbc-example</module>
<module>transaction-2pc-xa-bitronix-raw-jdbc-example</module>
+ <module>transaction-2pc-xa-narayana-raw-jdbc-example</module>
<module>transaction-2pc-xa-spring-boot-example</module>
<module>transaction-2pc-xa-spring-namespace-example</module>
<module>transaction-base-seata-raw-jdbc-example</module>
diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/pom.xml b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/pom.xml
new file mode 100644
index 0000000..b566db1
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/pom.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.shardingsphere.example</groupId>
+ <artifactId>transaction-example</artifactId>
+ <version>5.0.0-RC1-SNAPSHOT</version>
+ </parent>
+ <artifactId>transaction-2pc-xa-narayana-raw-jdbc-example</artifactId>
+ <name>Example::transaction::2pc-xa-narayana-raw-jdbc</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere.example</groupId>
+ <artifactId>example-raw-jdbc</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-transaction-xa-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-transaction-xa-narayana</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.narayana.jta</groupId>
+ <artifactId>jta</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.narayana.jts</groupId>
+ <artifactId>narayana-jts-integration</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss</groupId>
+ <artifactId>jboss-transaction-spi</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.logging</groupId>
+ <artifactId>jboss-logging</artifactId>
+ </dependency>
+ </dependencies>
+</project>
\ No newline at end of file
diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/java/ExampleMain.java b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/java/ExampleMain.java
new file mode 100644
index 0000000..6e0f794
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/java/ExampleMain.java
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+
+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-narayana-raw-jdbc-example/src/main/java/OrderServiceImpl.java b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/java/OrderServiceImpl.java
new file mode 100644
index 0000000..0f8396e
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/java/OrderServiceImpl.java
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import javax.sql.DataSource;
+import org.apache.shardingsphere.example.core.api.service.ExampleService;
+import org.apache.shardingsphere.transaction.core.TransactionType;
+import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
+
+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");
+ } finally {
+ TransactionTypeHolder.clear();
+ }
+ 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");
+ } finally {
+ TransactionTypeHolder.clear();
+ }
+ 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-narayana-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml
new file mode 100644
index 0000000..e0204c0
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml
@@ -0,0 +1,69 @@
+#
+# 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
+ shardingAlgorithmName: table_inline
+ keyGenerateStrategy:
+ column: order_id
+ keyGeneratorName: snowflake
+
+ defaultDatabaseStrategy:
+ standard:
+ shardingColumn: user_id
+ shardingAlgorithmName: database_inline
+ defaultTableStrategy:
+ none:
+
+ shardingAlgorithms:
+ database_inline:
+ type: INLINE
+ props:
+ algorithm.expression: ds_${user_id % 2}
+ table_inline:
+ type: INLINE
+ props:
+ algorithm.expression: t_order_${order_id % 2}
+
+ keyGenerators:
+ snowflake:
+ type: SNOWFLAKE
+ props:
+ worker.id: 123
+
+props:
+ sql.show: false
diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/jbossts-properties.xml b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/jbossts-properties.xml
new file mode 100644
index 0000000..3dfecae
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/jbossts-properties.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+<properties>
+ <entry key="CoordinatorEnvironmentBean.commitOnePhase">YES</entry>
+ <entry key="ObjectStoreEnvironmentBean.objectStoreDir">target</entry>
+ <entry key="com.arjuna.ats.arjuna.objectstore.objectStoreDir">target</entry>
+ <entry key="ObjectStoreEnvironmentBean.transactionSync">ON</entry>
+ <entry key="CoreEnvironmentBean.nodeIdentifier">1</entry>
+ <entry key="JTAEnvironmentBean.xaRecoveryNodes">1</entry>
+ <entry key="JTAEnvironmentBean.xaResourceOrphanFilterClassNames">
+ com.arjuna.ats.internal.jta.recovery.arjunacore.JTATransactionLogXAResourceOrphanFilter
+ com.arjuna.ats.internal.jta.recovery.arjunacore.JTANodeNameXAResourceOrphanFilter
+ com.arjuna.ats.internal.jta.recovery.arjunacore.JTAActionStatusServiceXAResourceOrphanFilter
+ </entry>
+ <entry key="CoreEnvironmentBean.socketProcessIdPort">0</entry>
+ <entry key="RecoveryEnvironmentBean.recoveryModuleClassNames">
+ com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule
+ com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule
+ </entry>
+ <entry key="RecoveryEnvironmentBean.expiryScannerClassNames">
+ com.arjuna.ats.internal.arjuna.recovery.ExpiredTransactionStatusManagerScanner
+ </entry>
+ <entry key="RecoveryEnvironmentBean.recoveryPort">4712</entry>
+ <entry key="RecoveryEnvironmentBean.recoveryAddress"></entry>
+ <entry key="RecoveryEnvironmentBean.transactionStatusManagerPort">0</entry>
+ <entry key="RecoveryEnvironmentBean.transactionStatusManagerAddress"></entry>
+ <entry key="RecoveryEnvironmentBean.recoveryListener">NO</entry>
+ <entry key="RecoveryEnvironmentBean.recoveryBackoffPeriod">1</entry>
+</properties>
diff --git a/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/logback.xml b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/logback.xml
new file mode 100644
index 0000000..459992b
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-narayana-raw-jdbc-example/src/main/resources/logback.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<configuration>
+ <property name="log.context.name" value="transaction-2pc-xa-raw-jdbc-example" />
+ <property name="log.charset" value="UTF-8" />
+ <property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] %msg %n" />
+ <contextName>${log.context.name}</contextName>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder charset="${log.charset}">
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ </appender>
+
+ <logger name="bitronix.tm" level="info"/>
+ <logger name="com.zaxxer.hikari" level="off"/>
+
+ <root>
+ <level value="INFO" />
+ <appender-ref ref="STDOUT" />
+ </root>
+</configuration>