This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 8288105  Add readwrite-splitting spring namespace example (#13584)
8288105 is described below

commit 82881055bf2117dcbf9cf6089a084bb08d36268a
Author: Guocheng Tang <[email protected]>
AuthorDate: Tue Nov 16 12:03:03 2021 +0800

    Add readwrite-splitting spring namespace example (#13584)
---
 .../spring-namespace-jdbc/data-model.yaml          |   2 +-
 .../main/resources/template/xml/application.ftl    |  41 +---
 .../template/xml/readwriteSplittingApplication.ftl |  31 +++
 .../resources/template/xml/shardingApplication.ftl |  49 +++++
 .../pom.xml                                        |   1 +
 .../pom.xml                                        |  20 +-
 ...adwriteSplittingSpringNamespaceJdbcExample.java |  32 +++
 ...SplittingSpringNamespaceJdbcExampleService.java | 227 +++++++++++++++++++++
 .../spring/namespace/jdbc/entity/Order.java        |  70 +++++++
 .../spring/namespace/jdbc/entity/OrderItem.java    |  70 +++++++
 .../src/main/resources/application.xml             |  73 +++++++
 .../src/main/resources/logback.xml}                |  31 ++-
 12 files changed, 592 insertions(+), 55 deletions(-)

diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/spring-namespace-jdbc/data-model.yaml
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/spring-namespace-jdbc/data-model.yaml
index c58817d..27c7904 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/spring-namespace-jdbc/data-model.yaml
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/spring-namespace-jdbc/data-model.yaml
@@ -17,6 +17,6 @@
 
 mode: memory
 transaction: local
-feature: sharding
+feature: readwrite-splitting
 framework: spring-namespace-jdbc
 
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
index 03d0c63..1b2a735 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
@@ -23,6 +23,7 @@
        xmlns:tx="http://www.springframework.org/schema/tx";
        
xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource";
        
xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding";
+       
xmlns:readwrite-splitting="http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting";
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            
http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context
@@ -33,9 +34,11 @@
                            
http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
                            
http://shardingsphere.apache.org/schema/shardingsphere/sharding
                            
http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
+                           
http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting
+                           
http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting/readwrite-splitting.xsd
                            ">
     <context:annotation-config />
-    <context:component-scan 
base-package="org.apache.shardingsphere.example.${feature}.${framework?replace('-',
 '.')}"/>
+    <context:component-scan 
base-package="org.apache.shardingsphere.example.${feature?replace('-', 
'.')}.${framework?replace('-', '.')}"/>
 <#if framework=="spring-namespace-jpa">
     <bean id="entityManagerFactory" 
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="dataSource" ref="dataSource" />
@@ -68,39 +71,13 @@
         <property name="username" value="root"/>
         <property name="password" value="123456"/>
     </bean>
-
-    <sharding:sharding-algorithm id="databaseAlgorithm" type="INLINE">
-        <props>
-            <prop key="algorithm-expression">demo_ds_${r'${user_id % 
2}'}</prop>
-        </props>
-    </sharding:sharding-algorithm>
-    <sharding:standard-strategy id="databaseStrategy" 
sharding-column="user_id" algorithm-ref="databaseAlgorithm" />
-    
-    <sharding:key-generate-algorithm id="snowflakeAlgorithm" type="SNOWFLAKE">
-        <props>
-            <prop key="worker-id">123</prop>
-        </props>
-    </sharding:key-generate-algorithm>
     
-    <sharding:key-generate-strategy id="orderKeyGenerator" column="order_id" 
algorithm-ref="snowflakeAlgorithm" />
-    <sharding:key-generate-strategy id="accountKeyGenerator" 
column="account_id" algorithm-ref="snowflakeAlgorithm" />
-    <sharding:key-generate-strategy id="itemKeyGenerator" 
column="order_item_id" algorithm-ref="snowflakeAlgorithm" />
-    
-    <sharding:rule id="shardingRule">
-        <sharding:table-rules>
-            <sharding:table-rule logic-table="t_order" 
database-strategy-ref="databaseStrategy" 
key-generate-strategy-ref="orderKeyGenerator" />
-            <sharding:table-rule logic-table="t_order_item" 
database-strategy-ref="databaseStrategy" 
key-generate-strategy-ref="itemKeyGenerator" />
-            <sharding:table-rule logic-table="t_account" 
database-strategy-ref="databaseStrategy" 
key-generate-strategy-ref="accountKeyGenerator" />
-        </sharding:table-rules>
-        <sharding:binding-table-rules>
-            <sharding:binding-table-rule logic-tables="t_order,t_order_item"/>
-        </sharding:binding-table-rules>
-        <sharding:broadcast-table-rules>
-            <sharding:broadcast-table-rule table="t_address"/>
-        </sharding:broadcast-table-rules>
-    </sharding:rule>
+<#if feature=="sharding">
+    <#include "shardingApplication.ftl">
+<#elseif feature=="readwrite-splitting">
+    <#include "readwriteSplittingApplication.ftl">
+</#if>
     
-    <shardingsphere:data-source id="dataSource" data-source-names="demo_ds_0, 
demo_ds_1" rule-refs="shardingRule" />
 <#if framework=="spring-namespace-mybatis">
     <bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource" />
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/readwriteSplittingApplication.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/readwriteSplittingApplication.ftl
new file mode 100644
index 0000000..23a8a04
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/readwriteSplittingApplication.ftl
@@ -0,0 +1,31 @@
+<#--
+  ~ 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.
+  -->
+    
+    <bean id="demo_ds_2" class="com.zaxxer.hikari.HikariDataSource" 
destroy-method="close">
+        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+        <property name="jdbcUrl" 
value="jdbc:mysql://localhost:3306/demo_read_ds_1?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
+        <property name="username" value="root"/>
+        <property name="password" value=""/>
+    </bean>
+    
+    <readwrite-splitting:load-balance-algorithm id="randomStrategy" 
type="RANDOM" />
+    
+    <readwrite-splitting:rule id="readWriteSplittingRule">
+        <readwrite-splitting:data-source-rule id="demo_ds" 
write-data-source-name="demo_ds_0" read-data-source-names="demo_ds_1, 
demo_ds_2" load-balance-algorithm-ref="randomStrategy" />
+    </readwrite-splitting:rule>
+    
+    <shardingsphere:data-source id="dataSource" data-source-names="demo_ds_0, 
demo_ds_1, demo_ds_2" rule-refs="readWriteSplittingRule" />
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/shardingApplication.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/shardingApplication.ftl
new file mode 100644
index 0000000..7fc7e9d
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/shardingApplication.ftl
@@ -0,0 +1,49 @@
+<#--
+  ~ 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.
+  -->
+
+    <sharding:sharding-algorithm id="databaseAlgorithm" type="INLINE">
+        <props>
+            <prop key="algorithm-expression">demo_ds_${r'${user_id % 
2}'}</prop>
+        </props>
+    </sharding:sharding-algorithm>
+    <sharding:standard-strategy id="databaseStrategy" 
sharding-column="user_id" algorithm-ref="databaseAlgorithm" />
+    
+    <sharding:key-generate-algorithm id="snowflakeAlgorithm" type="SNOWFLAKE">
+        <props>
+            <prop key="worker-id">123</prop>
+        </props>
+    </sharding:key-generate-algorithm>
+    
+    <sharding:key-generate-strategy id="orderKeyGenerator" column="order_id" 
algorithm-ref="snowflakeAlgorithm" />
+    <sharding:key-generate-strategy id="accountKeyGenerator" 
column="account_id" algorithm-ref="snowflakeAlgorithm" />
+    <sharding:key-generate-strategy id="itemKeyGenerator" 
column="order_item_id" algorithm-ref="snowflakeAlgorithm" />
+    
+    <sharding:rule id="shardingRule">
+        <sharding:table-rules>
+            <sharding:table-rule logic-table="t_order" 
database-strategy-ref="databaseStrategy" 
key-generate-strategy-ref="orderKeyGenerator" />
+            <sharding:table-rule logic-table="t_order_item" 
database-strategy-ref="databaseStrategy" 
key-generate-strategy-ref="itemKeyGenerator" />
+            <sharding:table-rule logic-table="t_account" 
database-strategy-ref="databaseStrategy" 
key-generate-strategy-ref="accountKeyGenerator" />
+        </sharding:table-rules>
+        <sharding:binding-table-rules>
+            <sharding:binding-table-rule logic-tables="t_order,t_order_item"/>
+        </sharding:binding-table-rules>
+        <sharding:broadcast-table-rules>
+            <sharding:broadcast-table-rule table="t_address"/>
+        </sharding:broadcast-table-rules>
+    </sharding:rule>
+    
+    <shardingsphere:data-source id="dataSource" data-source-names="demo_ds_0, 
demo_ds_1" rule-refs="shardingRule" />
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
index ca1ca8d..6d77c6d 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
@@ -31,5 +31,6 @@
     
     <modules>
         
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-jdbc-example</module>
+        
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example</module>
     </modules>
 </project>
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-examp
 [...]
similarity index 71%
copy from 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
copy to 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/pom.xml
index ca1ca8d..d2843ca 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/pom.xml
@@ -20,16 +20,24 @@
          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";>
     <parent>
-        <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
+        
<artifactId>shardingsphere-jdbc-memory-local-readwrite-splitting-example</artifactId>
         <groupId>org.apache.shardingsphere.example</groupId>
         <version>5.0.1-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
-    <packaging>pom</packaging>
-    
<artifactId>shardingsphere-jdbc-memory-local-readwrite-splitting-example</artifactId>
+    
<artifactId>shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example</artifactId>
     <name>${project.artifactId}</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>5.1.5.RELEASE</version>
+        </dependency>
+    </dependencies>
     
-    <modules>
-        
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-jdbc-example</module>
-    </modules>
 </project>
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/MemoryLocalReadwriteSplittingSpringNamespaceJdbcExample.java
 b/examples/shardingsphere-sample/shardingsphere-jdb [...]
new file mode 100644
index 0000000..3814dbf
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/MemoryLocalReadwriteSplittingSpringNamespaceJdbcExample.java
@@ -0,0 +1,32 @@
+/*
+ * 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.readwrite.splitting.spring.namespace.jdbc;
+
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import java.sql.SQLException;
+
+public class MemoryLocalReadwriteSplittingSpringNamespaceJdbcExample {
+    
+    public static void main(final String[] args) throws SQLException {
+        try (ConfigurableApplicationContext applicationContext = new 
ClassPathXmlApplicationContext("application.xml")) {
+            MemoryLocalReadwriteSplittingSpringNamespaceJdbcExampleService 
exampleService = 
applicationContext.getBean(MemoryLocalReadwriteSplittingSpringNamespaceJdbcExampleService.class);
+            exampleService.run();
+        }
+    }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/MemoryLocalReadwriteSplittingSpringNamespaceJdbcExampleService.java
 b/examples/shardingsphere-sample/shardingsph [...]
new file mode 100644
index 0000000..60ed745
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/MemoryLocalReadwriteSplittingSpringNamespaceJdbcExampleService.java
@@ -0,0 +1,227 @@
+/*
+ * 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.readwrite.splitting.spring.namespace.jdbc;
+
+import lombok.AllArgsConstructor;
+import 
org.apache.shardingsphere.example.readwrite.splitting.spring.namespace.jdbc.entity.Order;
+import 
org.apache.shardingsphere.example.readwrite.splitting.spring.namespace.jdbc.entity.OrderItem;
+import org.springframework.stereotype.Service;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+@Service
+@AllArgsConstructor
+public final class 
MemoryLocalReadwriteSplittingSpringNamespaceJdbcExampleService {
+    
+    private final DataSource dataSource;
+
+    /**
+     * Execute test.
+     *
+     * @throws SQLException
+     */
+    public void run() throws SQLException {
+        try {
+            this.initEnvironment();
+            this.processSuccess();
+        } finally {
+            this.cleanEnvironment();
+        }
+    }
+    
+    /**
+     * Initialize the database test environment.
+     * @throws SQLException
+     */
+    private void initEnvironment() throws SQLException {
+        String createOrderTableSql = "CREATE TABLE IF NOT EXISTS t_order 
(order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id 
BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))";
+        String createOrderItemTableSql = "CREATE TABLE IF NOT EXISTS 
t_order_item "
+                + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id 
BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY 
(order_item_id))";
+        String createAddressTableSql = "CREATE TABLE IF NOT EXISTS t_address "
+                + "(address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT 
NULL, PRIMARY KEY (address_id))";
+        String truncateOrderTable = "TRUNCATE TABLE t_order";
+        String truncateOrderItemTable = "TRUNCATE TABLE t_order_item";
+        String truncateAddressTableSql = "TRUNCATE TABLE t_address";
+        
+        try (Connection connection = dataSource.getConnection();
+             Statement statement = connection.createStatement()) {
+            statement.executeUpdate(createOrderTableSql);
+            statement.executeUpdate(createOrderItemTableSql);
+            statement.executeUpdate(createAddressTableSql);
+            statement.executeUpdate(createAddressTableSql);
+            statement.executeUpdate(truncateOrderTable);
+            statement.executeUpdate(truncateOrderItemTable);
+            statement.executeUpdate(truncateAddressTableSql);
+        }
+    }
+    
+    private void processSuccess() throws SQLException {
+        System.out.println("-------------- Process Success Begin 
---------------");
+        List<Long> orderIds = insertData();
+        printData(); 
+        deleteData(orderIds);
+        printData();
+        System.out.println("-------------- Process Success Finish 
--------------");
+    }
+    
+    private void processFailure() throws SQLException {
+        System.out.println("-------------- Process Failure Begin 
---------------");
+        insertData();
+        System.out.println("-------------- Process Failure Finish 
--------------");
+        throw new RuntimeException("Exception occur for transaction test.");
+    }
+
+    private List<Long> insertData() throws SQLException {
+        System.out.println("---------------------------- Insert Data 
----------------------------");
+        List<Long> result = new ArrayList<>(10);
+        for (int i = 1; i <= 10; i++) {
+            Order order = insertOrder(i);
+            insertOrderItem(i, order);
+            result.add(order.getOrderId());
+        }
+        return result;
+    }
+    
+    private Order insertOrder(final int i) throws SQLException {
+        Order order = new Order();
+        order.setUserId(i);
+        order.setAddressId(i);
+        order.setStatus("INSERT_TEST");
+        String sql = "INSERT INTO t_order (user_id, address_id, status) VALUES 
(?, ?, ?)";
+        try (Connection connection = dataSource.getConnection();
+             PreparedStatement preparedStatement = 
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+            preparedStatement.setInt(1, order.getUserId());
+            preparedStatement.setLong(2, order.getAddressId());
+            preparedStatement.setString(3, order.getStatus());
+            preparedStatement.executeUpdate();
+            try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+                if (resultSet.next()) {
+                    order.setOrderId(resultSet.getLong(1));
+                }
+            }
+        }
+        return order;
+    }
+
+    private void insertOrderItem(final int i, final Order order) throws 
SQLException {
+        OrderItem orderItem = new OrderItem();
+        orderItem.setOrderId(order.getOrderId());
+        orderItem.setUserId(i);
+        orderItem.setStatus("INSERT_TEST");
+        String sql = "INSERT INTO t_order_item (order_id, user_id, status) 
VALUES (?, ?, ?)";
+        try (Connection connection = dataSource.getConnection();
+             PreparedStatement preparedStatement = 
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+            preparedStatement.setLong(1, orderItem.getOrderId());
+            preparedStatement.setInt(2, orderItem.getUserId());
+            preparedStatement.setString(3, orderItem.getStatus());
+            preparedStatement.executeUpdate();
+            try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+                if (resultSet.next()) {
+                    orderItem.setOrderItemId(resultSet.getLong(1));
+                }
+            }
+        }
+    }
+
+    private void deleteData(final List<Long> orderIds) throws SQLException {
+        System.out.println("---------------------------- Delete Data 
----------------------------");
+        for (Long each : orderIds) {
+            String deleteOrderSql = "DELETE FROM t_order WHERE order_id=?";
+            String deleteOrderItemSql = "DELETE FROM t_order_item WHERE 
order_id=?";
+            try (Connection connection = dataSource.getConnection();
+                 PreparedStatement orderPreparedStatement = 
connection.prepareStatement(deleteOrderSql);
+                 PreparedStatement orderItemPreparedStatement = 
connection.prepareStatement(deleteOrderItemSql)) {
+                orderPreparedStatement.setLong(1, each);
+                orderItemPreparedStatement.setLong(1, each);
+                orderPreparedStatement.executeUpdate();
+                orderItemPreparedStatement.executeUpdate();
+            }
+        }
+    }
+    
+    private void printData() throws SQLException {
+        System.out.println("---------------------------- Print Order Data 
-----------------------");
+        for (Object each : this.getOrders()) {
+            System.out.println(each);
+        }
+        System.out.println("---------------------------- Print OrderItem Data 
-------------------");
+        for (Object each : this.getOrderItems()) {
+            System.out.println(each);
+        }
+    }
+
+    protected List<OrderItem> getOrderItems() throws SQLException {
+        String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE 
o.order_id = i.order_id";
+        List<OrderItem> result = new LinkedList<>();
+        try (Connection connection = dataSource.getConnection();
+             PreparedStatement preparedStatement = 
connection.prepareStatement(sql);
+             ResultSet resultSet = preparedStatement.executeQuery()) {
+            while (resultSet.next()) {
+                OrderItem orderItem = new OrderItem();
+                orderItem.setOrderItemId(resultSet.getLong(1));
+                orderItem.setOrderId(resultSet.getLong(2));
+                orderItem.setUserId(resultSet.getInt(3));
+                orderItem.setStatus(resultSet.getString(4));
+                result.add(orderItem);
+            }
+        }
+        return result;
+    }
+    
+    protected List<Order> getOrders() throws SQLException {
+        String sql = "SELECT * FROM t_order";
+        List<Order> result = new LinkedList<>();
+        try (Connection connection = dataSource.getConnection();
+             PreparedStatement preparedStatement = 
connection.prepareStatement(sql);
+             ResultSet resultSet = preparedStatement.executeQuery()) {
+            while (resultSet.next()) {
+                Order order = new Order();
+                order.setOrderId(resultSet.getLong(1));
+                order.setUserId(resultSet.getInt(2));
+                order.setAddressId(resultSet.getLong(3));
+                order.setStatus(resultSet.getString(4));
+                result.add(order);
+            }
+        }
+        return result;
+    }
+    
+    /**
+     * Restore the environment.
+     * @throws SQLException
+     */
+    private void cleanEnvironment() throws SQLException {
+        String dropOrderSql = "DROP TABLE t_order";
+        String dropOrderItemSql = "DROP TABLE t_order_item";
+        String dropAddressSql = "DROP TABLE t_address";
+        try (Connection connection = dataSource.getConnection();
+             Statement statement = connection.createStatement()) {
+            statement.executeUpdate(dropOrderSql);
+            statement.executeUpdate(dropOrderItemSql);
+            statement.executeUpdate(dropAddressSql);
+        }
+    }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/entity/Order.java
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example
 [...]
new file mode 100644
index 0000000..aed8681
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/entity/Order.java
@@ -0,0 +1,70 @@
+/*
+ * 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.readwrite.splitting.spring.namespace.jdbc.entity;
+
+import java.io.Serializable;
+
+public class Order implements Serializable {
+    
+    private static final long serialVersionUID = 8306802022239174861L;
+    
+    private long orderId;
+    
+    private int userId;
+    
+    private long addressId;
+    
+    private String status;
+    
+    public long getOrderId() {
+        return orderId;
+    }
+    
+    public void setOrderId(final long orderId) {
+        this.orderId = orderId;
+    }
+    
+    public int getUserId() {
+        return userId;
+    }
+    
+    public void setUserId(final int userId) {
+        this.userId = userId;
+    }
+    
+    public String getStatus() {
+        return status;
+    }
+    
+    public void setStatus(final String status) {
+        this.status = status;
+    }
+    
+    public long getAddressId() {
+        return addressId;
+    }
+    
+    public void setAddressId(final long addressId) {
+        this.addressId = addressId;
+    }
+    
+    @Override
+    public String toString() {
+        return String.format("order_id: %s, user_id: %s, address_id: %s, 
status: %s", orderId, userId, addressId, status);
+    }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/entity/OrderItem.java
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-exa
 [...]
new file mode 100644
index 0000000..3b315cb
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/readwrite/splitting/spring/namespace/jdbc/entity/OrderItem.java
@@ -0,0 +1,70 @@
+/*
+ * 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.readwrite.splitting.spring.namespace.jdbc.entity;
+
+import java.io.Serializable;
+
+public class OrderItem implements Serializable {
+    
+    private static final long serialVersionUID = 1332162822494069342L;
+    
+    private long orderItemId;
+    
+    private long orderId;
+    
+    private int userId;
+    
+    private String status;
+    
+    public long getOrderItemId() {
+        return orderItemId;
+    }
+    
+    public void setOrderItemId(final long orderItemId) {
+        this.orderItemId = orderItemId;
+    }
+    
+    public long getOrderId() {
+        return orderId;
+    }
+    
+    public void setOrderId(final long orderId) {
+        this.orderId = orderId;
+    }
+    
+    public int getUserId() {
+        return userId;
+    }
+    
+    public void setUserId(final int userId) {
+        this.userId = userId;
+    }
+    
+    public String getStatus() {
+        return status;
+    }
+    
+    public void setStatus(final String status) {
+        this.status = status;
+    }
+    
+    @Override
+    public String toString() {
+        return String.format("order_item_id:%s, order_id: %s, user_id: %s, 
status: %s", orderItemId, orderId, userId, status);
+    }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/resources/application.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-loca
 [...]
new file mode 100644
index 0000000..499a567
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/resources/application.xml
@@ -0,0 +1,73 @@
+<?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.
+  -->
+
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns:p="http://www.springframework.org/schema/p";
+       xmlns:context="http://www.springframework.org/schema/context";
+       xmlns:tx="http://www.springframework.org/schema/tx";
+       
xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource";
+       
xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding";
+       
xmlns:readwrite-splitting="http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting";
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
+                           
http://www.springframework.org/schema/beans/spring-beans.xsd
+                           http://www.springframework.org/schema/context
+                           
http://www.springframework.org/schema/context/spring-context.xsd
+                           http://www.springframework.org/schema/tx
+                           
http://www.springframework.org/schema/tx/spring-tx.xsd
+                           
http://shardingsphere.apache.org/schema/shardingsphere/datasource
+                           
http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
+                           
http://shardingsphere.apache.org/schema/shardingsphere/sharding
+                           
http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
+                           
http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting
+                           
http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting/readwrite-splitting.xsd
+                           ">
+    <context:annotation-config />
+    <context:component-scan 
base-package="org.apache.shardingsphere.example.readwrite.splitting.spring.namespace.jdbc"/>
+    
+    <bean id="demo_ds_0" class="com.zaxxer.hikari.HikariDataSource" 
destroy-method="close">
+        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+        <property name="jdbcUrl" 
value="jdbc:mysql://localhost:3307/demo_ds_0?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
+        <property name="username" value="root"/>
+        <property name="password" value="123456"/>
+    </bean>
+    
+    <bean id="demo_ds_1" class="com.zaxxer.hikari.HikariDataSource" 
destroy-method="close">
+        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+        <property name="jdbcUrl" 
value="jdbc:mysql://localhost:3307/demo_ds_1?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
+        <property name="username" value="root"/>
+        <property name="password" value="123456"/>
+    </bean>
+    
+    
+    <bean id="demo_ds_2" class="com.zaxxer.hikari.HikariDataSource" 
destroy-method="close">
+        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+        <property name="jdbcUrl" 
value="jdbc:mysql://localhost:3307/demo_read_ds_1?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
+        <property name="username" value="root"/>
+        <property name="password" value="123456"/>
+    </bean>
+    
+    <readwrite-splitting:load-balance-algorithm id="randomStrategy" 
type="RANDOM" />
+    
+    <readwrite-splitting:rule id="readWriteSplittingRule">
+        <readwrite-splitting:data-source-rule id="demo_ds" 
write-data-source-name="demo_ds_0" read-data-source-names="demo_ds_1, 
demo_ds_2" load-balance-algorithm-ref="randomStrategy" />
+    </readwrite-splitting:rule>
+    
+    <shardingsphere:data-source id="dataSource" data-source-names="demo_ds_0, 
demo_ds_1, demo_ds_2" rule-refs="readWriteSplittingRule" />
+    
+</beans>
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-examp
 [...]
similarity index 53%
copy from 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
copy to 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/resources/logback.xml
index ca1ca8d..6959f53 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/pom.xml
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-readwrite-splitting-example/shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-jdbc-example/src/main/resources/logback.xml
@@ -16,20 +16,19 @@
   ~ 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";>
-    <parent>
-        <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
-        <groupId>org.apache.shardingsphere.example</groupId>
-        <version>5.0.1-SNAPSHOT</version>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-    <packaging>pom</packaging>
-    
<artifactId>shardingsphere-jdbc-memory-local-readwrite-splitting-example</artifactId>
-    <name>${project.artifactId}</name>
+<configuration>
+    <property name="log.context.name" 
value="shardingsphere-jdbc-memory-local-readwrite-splitting-spring-namespace-example"
 />
+    <property name="log.charset" value="UTF-8" />
+    <property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] 
%msg %n" />
+    <contextName>${log.context.name}</contextName>
     
-    <modules>
-        
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-jdbc-example</module>
-    </modules>
-</project>
+    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder charset="${log.charset}">
+            <pattern>${log.pattern}</pattern>
+        </encoder>
+    </appender>
+    <root>
+        <level value="INFO" />
+        <appender-ref ref="STDOUT" />
+    </root>
+</configuration>

Reply via email to