Loading sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/base/seata/raw/jdbc/YamlConfigurationTransactionExample.java 0 → 100755 +114 −0 Original line number Diff line number Diff line /* * 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.base.seata.raw.jdbc; import org.apache.shardingsphere.example.common.entity.OrderItem; import org.apache.shardingsphere.example.common.jdbc.repository.OrderItemRepositoryImpl; import org.apache.shardingsphere.example.common.jdbc.repository.OrderRepositoryImpl; import org.apache.shardingsphere.example.common.jdbc.service.CommonServiceImpl; import org.apache.shardingsphere.example.common.service.CommonService; import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory; import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.core.TransactionTypeHolder; import javax.sql.DataSource; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class YamlConfigurationTransactionExample { private static String configFile = "/META-INF/sharding-databases-tables.yaml"; // private static String configFile = "/META-INF/master-slave.yaml"; public static void main(final String[] args) throws SQLException, IOException { DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile(configFile)); CommonService commonService = getCommonService(dataSource); commonService.initEnvironment(); processSagaTransaction(dataSource, commonService); commonService.cleanEnvironment(); } 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 processSagaTransaction(final DataSource dataSource, final CommonService commonService) throws SQLException { TransactionTypeHolder.set(TransactionType.BASE); System.out.println("------ start seata transaction ------"); try (Connection connection = dataSource.getConnection()) { insertSuccess(connection, commonService); connection.commit(); commonService.printData(); } truncateTable(dataSource); System.out.println("------ end seata 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 < 10; i++) { OrderItem item = new OrderItem(); item.setUserId(i); item.setStatus("SEATA-INIT"); insertOrderItem(connection, item); } commonService.printData(); } private static Long insertOrderItem(final Connection connection, final OrderItem orderItem) { String sql = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (?, ?, ?)"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { preparedStatement.setLong(1, 1L); 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)); } } } catch (final SQLException ignored) { } return orderItem.getOrderItemId(); } private static void truncateTable(final DataSource dataSource) { OrderRepositoryImpl orderRepository = new OrderRepositoryImpl(dataSource); orderRepository.truncateTable(); } } sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/META-INF/master-slave.yaml 0 → 100755 +24 −0 Original line number Diff line number Diff line dataSources: ds_master: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master username: root password: ds_slave_0: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_0 username: root password: ds_slave_1: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_1 username: root password: masterSlaveRule: name: ds_ms masterDataSourceName: ds_master slaveDataSourceNames: [ds_slave_0, ds_slave_1] props: sql.show: false sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml 0 → 100755 +41 −0 Original line number Diff line number Diff line dataSources: ds_0: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0 username: root password: ds_1: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1 username: root password: shardingRule: tables: t_order: actualDataNodes: ds_${0..1}.t_order_${0..1} tableStrategy: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} keyGenerator: type: SNOWFLAKE column: order_id t_order_item: actualDataNodes: ds_${0..1}.t_order_item_${0..1} tableStrategy: inline: shardingColumn: order_id algorithmExpression: t_order_item_${order_id % 2} bindingTables: - t_order,t_order_item defaultDatabaseStrategy: inline: shardingColumn: user_id algorithmExpression: ds_${user_id % 2} defaultTableStrategy: none: props: sql.show: false sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/file.conf 0 → 100644 +57 −0 Original line number Diff line number Diff line ## --------------------------------------------------------------------------- ## 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. ## --------------------------------------------------------------------------- transport { # tcp udt unix-domain-socket type = "TCP" #NIO NATIVE server = "NIO" #enable heartbeat heartbeat = true #thread factory for netty thread-factory { boss-thread-prefix = "NettyBoss" worker-thread-prefix = "NettyServerNIOWorker" server-executor-thread-prefix = "NettyServerBizHandler" share-boss-worker = false client-selector-thread-prefix = "NettyClientSelector" client-selector-thread-size = 1 client-worker-thread-prefix = "NettyClientWorkerThread" # netty boss thread size,will not be used for UDT boss-thread-size = 1 #auto default pin or 8 worker-thread-size = 8 } } service { #vgroup->rgroup vgroup_mapping.raw-jdbc-group = "default" #only support single node default.grouplist = "127.0.0.1:8091" #degrade current not support enableDegrade = false #disable disable = false } client { async.commit.buffer.limit = 10000 lock { retry.internal = 10 retry.times = 30 } } sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/logback.xml 0 → 100755 +34 −0 Original line number Diff line number Diff line <?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-base-seata-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> <root> <level value="INFO" /> <appender-ref ref="STDOUT" /> </root> </configuration> Loading
sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/base/seata/raw/jdbc/YamlConfigurationTransactionExample.java 0 → 100755 +114 −0 Original line number Diff line number Diff line /* * 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.base.seata.raw.jdbc; import org.apache.shardingsphere.example.common.entity.OrderItem; import org.apache.shardingsphere.example.common.jdbc.repository.OrderItemRepositoryImpl; import org.apache.shardingsphere.example.common.jdbc.repository.OrderRepositoryImpl; import org.apache.shardingsphere.example.common.jdbc.service.CommonServiceImpl; import org.apache.shardingsphere.example.common.service.CommonService; import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory; import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.core.TransactionTypeHolder; import javax.sql.DataSource; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class YamlConfigurationTransactionExample { private static String configFile = "/META-INF/sharding-databases-tables.yaml"; // private static String configFile = "/META-INF/master-slave.yaml"; public static void main(final String[] args) throws SQLException, IOException { DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile(configFile)); CommonService commonService = getCommonService(dataSource); commonService.initEnvironment(); processSagaTransaction(dataSource, commonService); commonService.cleanEnvironment(); } 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 processSagaTransaction(final DataSource dataSource, final CommonService commonService) throws SQLException { TransactionTypeHolder.set(TransactionType.BASE); System.out.println("------ start seata transaction ------"); try (Connection connection = dataSource.getConnection()) { insertSuccess(connection, commonService); connection.commit(); commonService.printData(); } truncateTable(dataSource); System.out.println("------ end seata 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 < 10; i++) { OrderItem item = new OrderItem(); item.setUserId(i); item.setStatus("SEATA-INIT"); insertOrderItem(connection, item); } commonService.printData(); } private static Long insertOrderItem(final Connection connection, final OrderItem orderItem) { String sql = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (?, ?, ?)"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { preparedStatement.setLong(1, 1L); 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)); } } } catch (final SQLException ignored) { } return orderItem.getOrderItemId(); } private static void truncateTable(final DataSource dataSource) { OrderRepositoryImpl orderRepository = new OrderRepositoryImpl(dataSource); orderRepository.truncateTable(); } }
sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/META-INF/master-slave.yaml 0 → 100755 +24 −0 Original line number Diff line number Diff line dataSources: ds_master: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master username: root password: ds_slave_0: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_0 username: root password: ds_slave_1: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_1 username: root password: masterSlaveRule: name: ds_ms masterDataSourceName: ds_master slaveDataSourceNames: [ds_slave_0, ds_slave_1] props: sql.show: false
sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml 0 → 100755 +41 −0 Original line number Diff line number Diff line dataSources: ds_0: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0 username: root password: ds_1: !!com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1 username: root password: shardingRule: tables: t_order: actualDataNodes: ds_${0..1}.t_order_${0..1} tableStrategy: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} keyGenerator: type: SNOWFLAKE column: order_id t_order_item: actualDataNodes: ds_${0..1}.t_order_item_${0..1} tableStrategy: inline: shardingColumn: order_id algorithmExpression: t_order_item_${order_id % 2} bindingTables: - t_order,t_order_item defaultDatabaseStrategy: inline: shardingColumn: user_id algorithmExpression: ds_${user_id % 2} defaultTableStrategy: none: props: sql.show: false
sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/file.conf 0 → 100644 +57 −0 Original line number Diff line number Diff line ## --------------------------------------------------------------------------- ## 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. ## --------------------------------------------------------------------------- transport { # tcp udt unix-domain-socket type = "TCP" #NIO NATIVE server = "NIO" #enable heartbeat heartbeat = true #thread factory for netty thread-factory { boss-thread-prefix = "NettyBoss" worker-thread-prefix = "NettyServerNIOWorker" server-executor-thread-prefix = "NettyServerBizHandler" share-boss-worker = false client-selector-thread-prefix = "NettyClientSelector" client-selector-thread-size = 1 client-worker-thread-prefix = "NettyClientWorkerThread" # netty boss thread size,will not be used for UDT boss-thread-size = 1 #auto default pin or 8 worker-thread-size = 8 } } service { #vgroup->rgroup vgroup_mapping.raw-jdbc-group = "default" #only support single node default.grouplist = "127.0.0.1:8091" #degrade current not support enableDegrade = false #disable disable = false } client { async.commit.buffer.limit = 10000 lock { retry.internal = 10 retry.times = 30 } }
sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example/src/main/resources/logback.xml 0 → 100755 +34 −0 Original line number Diff line number Diff line <?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-base-seata-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> <root> <level value="INFO" /> <appender-ref ref="STDOUT" /> </root> </configuration>