8000 第8周作业6 · JavaCourse00/JavaCourseCodes@8432715 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 8432715

Browse files
author
nononi
committed
第8周作业6
1 parent 4196075 commit 8432715

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.nononi.xademo;
2+
3+
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
4+
import org.apache.shardingsphere.transaction.core.TransactionType;
5+
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
6+
7+
import javax.sql.DataSource;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.sql.Connection;
11+
import java.sql.PreparedStatement;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
15+
public class XaDemo {
16+
public static void main(String[] args) throws IOException, SQLException {
17+
DataSource dataSource = getShardingDatasource();
18+
cleanupData(dataSource);
19+
20+
TransactionTypeHolder.set(TransactionType.XA);
21+
22+
Connection conn = dataSource.getConnection();
23+
String sql = "insert into t_order (user_id, order_id) VALUES (?, ?);";
24+
25+
System.out.println("First XA Start insert data");
26+
try (PreparedStatement statement = conn.prepareStatement(sql)) {
27+
conn.setAutoCommit(false);
28+
//会插入到ds1的t_order_1;
29+
statement.setLong(1, 1);
30+
statement.setLong(2, 1);
31+
statement.executeUpdate();
32+
//会插入到ds0的t_order_0;
33+
statement.setLong(1, 2);
34+
statement.setLong(2, 2);
35+
statement.executeUpdate();
36+
37+
conn.commit();
38+
}
39+
40+
System.out.println("First XA insert successful");
41+
42+
System.out.println("Second XA Start insert data");
43+
try (PreparedStatement statement = conn.prepareStatement(sql)) {
44+
conn.setAutoCommit(false);
45+
//会插入到ds1的t_order_1;
46+
//回滚后应该两个数据库中都没有插入
47+
statement.setLong(1, 3);
48+
statement.setLong(2, 3);
49+
statement.executeUpdate();
50+
//会插入到ds0的t_order_0;
51+
statement.setLong(1, 4);
52+
statement.setLong(2, 4);
53+
statement.executeUpdate();
54+
conn.rollback();
55+
56+
} finally {
57+
conn.close();
58+
}
59+
System.out.println("Second XA insert rollback");
60+
}
61+
62+
private static void cleanupData(DataSource dataSource) {
63+
System.out.println("Delete all Data");
64+
try (Connection conn = dataSource.getConnection(); Statement statement = conn.createStatement()) {
65+
statement.execute("delete from t_order;");
66+
conn.commit();
67+
} catch (SQLException e) {
68+
e.printStackTrace();
69+
}
70+
System.out.println("Delete all Data successful");
71+
}
72+
73+
static private DataSource getShardingDatasource() throws IOException, SQLException {
74+
String fileName = "C:\\Users\\NoNo_Ni\\Desktop\\Java进阶训练营\\javacourse\\06db\\work6ofweek8\\src\\main\\resources\\sharding-databases-tables.yaml";
75+
File yamlFile = new File(fileName);
76+
return YamlShardingSphereDataSourceFactory.createDataSource(yamlFile);
77+
}
78+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
dataSources:
2+
ds_0: !!com.zaxxer.hikari.HikariDataSource
3+
driverClassName: com.mysql.jdbc.Driver
4+
jdbcUrl: jdbc:mysql://127.0.0.1:3316/demo_ds_xa_0?serverTimezone=UTC&useSSL=false
5+
username: root
6+
password:
7+
autoCommit: false
8+
ds_1: !!com.zaxxer.hikari.HikariDataSource
9+
driverClassName: com.mysql.jdbc.Driver
10+
jdbcUrl: jdbc:mysql://127.0.0.1:3326/demo_ds_xa_1?serverTimezone=UTC&useSSL=false
11+
username: root
12+
password:
13+
autoCommit: false
14+
15+
rules:
16+
- !SHARDING
17+
tables:
18+
t_order:
19+
actualDataNodes: ds_${0..1}.t_order_${0..1}
20+
databaseStrategy:
21+
standard:
22+
shardingColumn: user_id
23+
shardingAlgorithmName: database_inline
24+
tableStrategy:
25+
standard:
26+
shardingColumn: order_id
27+
shardingAlgorithmName: t_order_inline
28+
bindingTables:
29+
- t_order
30+
31+
shardingAlgorithms:
32+
database_inline:
33+
type: INLINE
34+
props:
35+
algorithm-expression: ds_${user_id % 2}
36+
t_order_inline:
37+
type: INLINE
38+
props:
39+
algorithm-expression: t_order_${order_id % 2}
40+
41+
props:
42+
sql-show: true

0 commit comments

Comments
 (0)
0