未验证 提交 16828c5b 编写于 作者: T TaoZhi 提交者: GitHub

add example for bitronix. (#5887)

上级 49ab3972
......@@ -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();
}
}
}
......@@ -53,7 +53,8 @@
<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>
<btm.version>2.1.3</btm.version>
<seata.version>1.0.0</seata.version>
<junit.version>4.12</junit.version>
......@@ -101,6 +102,11 @@
<artifactId>shardingsphere-transaction-xa-core</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-xa-bitronix</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-base-seata-at</artifactId>
......@@ -217,6 +223,11 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.btm</groupId>
<artifactId>btm</artifactId>
<version>${btm.version}</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-rm-datasource</artifactId>
......
......@@ -32,9 +32,10 @@
<modules>
<module>transaction-2pc-xa-raw-jdbc-example</module>
<module>transaction-base-seata-raw-jdbc-example</module>
<module>transaction-2pc-xa-bitronix-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>
<module>transaction-base-seata-spring-boot-example</module>
</modules>
</project>
<?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-bitronix-raw-jdbc-example</artifactId>
<name>Example::transaction::2pc-xa-bitronix-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-bitronix</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.btm</groupId>
<artifactId>btm</artifactId>
</dependency>
</dependencies>
</project>
/*
* 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);
}
}
/*
* 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;
}
}
#
# 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
<?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>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册