-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysqlDemoPool.java
More file actions
66 lines (55 loc) · 1.87 KB
/
Copy pathMysqlDemoPool.java
File metadata and controls
66 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
public class MysqlDemoPool {
private static final String URL = "jdbc:mysql://localhost:3306/study";
private static final String username = "root";
private static final String password = "root";
private static BasicDataSource datasource = new BasicDataSource();
public static void main(String[] args) throws SQLException, ClassNotFoundException {
MysqlDemoPool mdp = new MysqlDemoPool();
datasource.setDriverClassName("com.mysql.jdbc.Driver");
datasource.seturl(URL);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setInitialSize(10);
datasource.setMaxActive(8);
datasource.setMaxIdle(5);
datasource.setMinIdle(1);
long begin = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
new Thread(() -> {
try {
Connection connection = datasource.getConnection();
System.out.println(connection.hashCode());
mdp.queryNormal(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}).start();
}
long end = System.currentTimeMillis();
// for (int i = 0; i < 100; i++) {
// Connection connection = datasource.getConnection();
// System.out.println(connection.hashCode());
// mdp.queryNormal(connection);
// }
System.out.println(end - begin);
}
public void queryNormal(Connection connection) throws SQLException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PreparedStatement ps = connection.prepareStatement("select * from dept");
ResultSet rs = ps.executeQuery();
connection.close();
}
}