Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
16828c5b
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
16828c5b
编写于
6月 02, 2020
作者:
T
TaoZhi
提交者:
GitHub
6月 02, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add example for bitronix. (#5887)
上级
49ab3972
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
345 addition
and
2 deletion
+345
-2
examples/example-core/example-api/src/main/java/org/apache/shardingsphere/example/core/api/ExampleExecuteTemplate.java
...ardingsphere/example/core/api/ExampleExecuteTemplate.java
+9
-0
examples/pom.xml
examples/pom.xml
+12
-1
examples/shardingsphere-jdbc-example/transaction-example/pom.xml
...s/shardingsphere-jdbc-example/transaction-example/pom.xml
+2
-1
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/pom.xml
...mple/transaction-2pc-xa-bitronix-raw-jdbc-example/pom.xml
+54
-0
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/ExampleMain.java
...example/transaction/xa/bitronix/raw/jdbc/ExampleMain.java
+41
-0
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/OrderServiceImpl.java
...le/transaction/xa/bitronix/raw/jdbc/OrderServiceImpl.java
+128
-0
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml
...rc/main/resources/META-INF/sharding-databases-tables.yaml
+61
-0
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/logback.xml
...-bitronix-raw-jdbc-example/src/main/resources/logback.xml
+38
-0
未找到文件。
examples/example-core/example-api/src/main/java/org/apache/shardingsphere/example/core/api/ExampleExecuteTemplate.java
浏览文件 @
16828c5b
...
...
@@ -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
();
}
}
}
examples/pom.xml
浏览文件 @
16828c5b
...
...
@@ -54,6 +54,7 @@
<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>
...
...
examples/shardingsphere-jdbc-example/transaction-example/pom.xml
浏览文件 @
16828c5b
...
...
@@ -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>
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/pom.xml
0 → 100644
浏览文件 @
16828c5b
<?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>
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/ExampleMain.java
0 → 100644
浏览文件 @
16828c5b
/*
* 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
);
}
}
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/transaction/xa/bitronix/raw/jdbc/OrderServiceImpl.java
0 → 100644
浏览文件 @
16828c5b
/*
* 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
;
}
}
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables.yaml
0 → 100644
浏览文件 @
16828c5b
#
# 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
examples/shardingsphere-jdbc-example/transaction-example/transaction-2pc-xa-bitronix-raw-jdbc-example/src/main/resources/logback.xml
0 → 100644
浏览文件 @
16828c5b
<?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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录