forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBPool.java
More file actions
116 lines (105 loc) · 3.04 KB
/
Copy pathDBPool.java
File metadata and controls
116 lines (105 loc) · 3.04 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package DNA.sdk.dbpool;
import java.io.Closeable;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class DBPool implements Closeable{
public static final String url = DBConsts.dbUrl;
public static final String driver = DBConsts.dbDriver;
public static final String user = DBConsts.dbUser;
public static final String password = DBConsts.dbPassword;
private static final int connCount = DBConsts.dbPoolCount;
private Map<String, ConnectionManager> connMap = new ConcurrentHashMap<String, ConnectionManager>();
public DBPool() {
init();
}
private void init() {
if (loadDriver()) {
createPool();
PrintHelper.printDebug("dbpool init finished");
return;
}
PrintHelper.printError("Load database driver failed, please to check your db'driver...");
}
private boolean loadDriver() {
try {
DriverManager.registerDriver((Driver) Class.forName(driver).newInstance());
return true;
} catch (Exception e) {
PrintHelper.printError(" Load Database driver fail : " + e);
}
return false;
}
private void createPool() {
for(int i=0; i<connCount; ++i) {
ConnectionManager connMgr = createConn();
if(connMgr == null) {
sleep();
--i; continue;
}
connMap.put(connMgr.getConn().toString(), connMgr);
}
}
private ConnectionManager createConn() {
Connection localConn = null;
try {
localConn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
PrintHelper.printError("CreatePool faild..., PooledConnection is null");
}
if(localConn != null)
return new ConnectionManager(this, localConn);
else
return null;
}
public void reOpen(Connection conn) {
if(conn != null) {
ConnectionManager oconMgr = connMap.remove(conn.toString());
if(oconMgr != null) {
oconMgr.close();
}
ConnectionManager connMgr = null;
while(true) {
connMgr = createConn();
if(connMgr != null) {
break;
}
// PrintHelper.printError("reOpenConn failed, continue reOpen after 1(s).....count:"+(++count));
sleep();
}
connMap.put(connMgr.getConn().toString(), connMgr);
}
}
private void sleep() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
public Connection getConnection() {
ConnectionManager connMgr = null;
while(true){
connMgr = connMap.values().stream().filter(p -> !p.isBusy).findAny().orElse(null);
if(connMgr != null) {
connMgr.isBusy = true;
break;
}
PrintHelper.printError("Not find spare conn, please wait 1(s).....");
sleep();
}
return connMgr.getConn();
}
public void freeConnection(Connection conn) {
if(connMap.containsKey(conn.toString())) {
ConnectionManager connMgr = connMap.get(conn.toString());
connMgr.isBusy = false;
connMap.put(conn.toString(), connMgr);
}
}
public void close() {
connMap.values().stream().forEach(p -> p.close());
}
}