Skip to content

Commit 5280689

Browse files
增加 用于检测连接同步的测试类
1 parent 512b1e5 commit 5280689

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.opencloudb.sqlexecute;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class MultiThreadSelectTest {
12+
private static void testSequnce(Connection theCon) throws SQLException {
13+
boolean autCommit = System.currentTimeMillis() % 2 == 1;
14+
theCon.setAutoCommit(autCommit);
15+
16+
String sql = "select * from company ";
17+
Statement stmt = theCon.createStatement();
18+
int charChoise = (int) (System.currentTimeMillis() % 3);
19+
if (charChoise == 0) {
20+
stmt.executeQuery("SET NAMES UTF8;");
21+
} else if (charChoise == 1) {
22+
stmt.executeQuery("SET NAMES latin1;");
23+
}
24+
if (charChoise == 2) {
25+
stmt.executeQuery("SET NAMES gb2312;");
26+
}
27+
ResultSet rs = stmt.executeQuery(sql);
28+
if (rs.next()) {
29+
System.out.println(Thread.currentThread().getName() + " get seq " + rs.getLong(1));
30+
} else {
31+
System.out.println(Thread.currentThread().getName() + " can't get seq ");
32+
}
33+
if (autCommit == false) {
34+
theCon.commit();
35+
}
36+
stmt.close();
37+
38+
}
39+
40+
private static Connection getCon(String url, String user, String passwd) throws SQLException {
41+
Connection theCon = DriverManager.getConnection(url, user, passwd);
42+
return theCon;
43+
}
44+
45+
public static void main(String[] args) {
46+
try {
47+
Class.forName("com.mysql.jdbc.Driver");
48+
} catch (ClassNotFoundException e1) {
49+
e1.printStackTrace();
50+
}
51+
52+
final String url = "jdbc:mysql://localhost:8066/TESTDB";
53+
final String user = "test";
54+
final String password = "test";
55+
List<Thread> threads = new ArrayList<Thread>(100);
56+
for (int i = 0; i < 50; i++) {
57+
58+
threads.add(new Thread() {
59+
public void run() {
60+
Connection con;
61+
try {
62+
con = getCon(url, user, password);
63+
for (int i = 0; i < 10000; i++) {
64+
testSequnce(con);
65+
}
66+
} catch (SQLException e) {
67+
68+
e.printStackTrace();
69+
}
70+
71+
}
72+
});
73+
74+
}
75+
for (Thread thred : threads) {
76+
thred.start();
77+
78+
}
79+
boolean hasRunning = true;
80+
while (hasRunning) {
81+
hasRunning = false;
82+
for (Thread thred : threads) {
83+
if (thred.isAlive()) {
84+
try {
85+
Thread.sleep(1000);
86+
hasRunning = true;
87+
} catch (InterruptedException e) {
88+
89+
}
90+
}
91+
92+
}
93+
}
94+
95+
}
96+
}

0 commit comments

Comments
 (0)