-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBConnectionManager.java
351 lines (327 loc) · 10.6 KB
/
DBConnectionManager.java
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package oracle.pool.database;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
public class DBConnectionManager {
static private DBConnectionManager instance; // ����Ψһʵ��
static private int clients; // �ͻ���
private Vector drivers = new Vector(); //�����
private PrintWriter log; //��־
private Hashtable pools = new Hashtable(); //���ӳ�
/**
* ����Ψһʵ��.
* ����ǵ�һ�ε��ô˷���,��ʵ��
*
* @return DBConnectionManager Ψһʵ��
*/
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
return instance;
}
/**
* ˽����������
*/
private DBConnectionManager() {
init();
}
/**
* �����Ӷ��ظ�������ָ�������ӳ�
*
* @param name �������ļ��ж�������ӳ�����
* @param con ���Ӷ���
*/
public void freeConnection(String name, Connection con) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
pool.freeConnection(con);
}
}
/**
* ���һ�����е�����.���û�п�������������������С�����������
* ��������������
*
* @param name �������ļ��ж�������ӳ�����
* @return Connection �������ӻ�null
*/
public Connection getConnection(String name) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection();
}
return null;
}
/**
* ���һ�����е�����.��û�п��е�����,������������С���������������,
* ��������������.����,��ָ����ʱ���ڵȴ������߳��ͷ�����.
*
* @param name ���ӳ�����
* @param time �Ժ���Ƶĵȴ�ʱ��
* @return Connection �������ӻ�null
*/
public Connection getConnection(String name, long time) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection(time);
}
return null;
}
/**
* �ر���������,����������ע��
*/
public synchronized void release() {
// �ȴ�ֱ�����һ���ͻ��������
if (--clients != 0) {
return;
}
Enumeration allPools = pools.elements();
while (allPools.hasMoreElements()) {
DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
log("����JDBC����� " + driver.getClass().getName()+"��ע��");
}
catch (SQLException e) {
log(e, "����������JDBC������ע��: " + driver.getClass().getName());
}
}
}
/**
* ���ָ�����Դ������ӳ�ʵ��.
*
* @param props ���ӳ�����
*/
private void createPools(Properties props) {
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String name = (String) propNames.nextElement();
if (name.endsWith(".url")) {
String poolName = name.substring(0, name.lastIndexOf("."));
String url = props.getProperty(poolName + ".url");
if (url == null) {
log("û��Ϊ���ӳ�" + poolName + "ָ��URL");
continue;
}
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxconn", "0");
int max;
try {
max = Integer.valueOf(maxconn).intValue();
}
catch (NumberFormatException e) {
log("������������������: " + maxconn + " .���ӳ�: " + poolName);
max = 0;
}
DBConnectionPool pool =
new DBConnectionPool(poolName, url, user, password, max);
pools.put(poolName, pool);
log("�ɹ��������ӳ�" + poolName);
}
}
}
/**
* ��ȡ������ɳ�ʼ��
*/
private void init() {
InputStream is = getClass().getResourceAsStream("/DBConfig.properties"); //��ȡdb.properties�ļ��е���������
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
System.err.println("���ܶ�ȡ�����ļ�. " +
"��ȷ��db.properties��CLASSPATHָ����·����");
return;
}
String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log");
try {
log = new PrintWriter(new FileWriter(logFile, true), true);
}
catch (IOException e) {
System.err.println("������־�ļ�: " + logFile);
log = new PrintWriter(System.err);
}
loadDrivers(dbProps);
createPools(dbProps);
}
/**
* װ�غ�ע������JDBC�����
*
* @param props ����
*/
private void loadDrivers(Properties props) {
String driverClasses = props.getProperty("drivers");
StringTokenizer st = new StringTokenizer(driverClasses);
while (st.hasMoreElements()) {
String driverClassName = st.nextToken().trim();
try {
Driver driver = (Driver)
Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
log("�ɹ�ע��JDBC�����" + driverClassName);
}
catch (Exception e) {
log("��ע��JDBC�����: " +
driverClassName + ", ����: " + e);
}
}
}
/**
* ���ı���Ϣд����־�ļ�
*/
private void log(String msg) {
log.println(new Date() + ": " + msg);
}
/**
* ���ı���Ϣ���쳣д����־�ļ�
*/
private void log(Throwable e, String msg) {
log.println(new Date() + ": " + msg);
e.printStackTrace(log);
}
/**
* ���ڲ��ඨ����һ�����ӳ�.
* ���ܹ����Ҫ��������,ֱ��Ԥ�������������Ϊֹ.
* �ڷ������Ӹ�Ӧ�ó���֮ǰ,���ܹ���֤���ӵ���Ч��.
*
*/
class DBConnectionPool {
private int checkedOut;
private Vector freeConnections = new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user;
/**
* �����µ����ӳ�
*
* @param name ���ӳ�����
* @param URL ��ݿ��JDBC URL
* @param user ��ݿ��ʺ�,�� null
* @param password ����,�� null
* @param maxConn �����ӳ������������������
*/
public DBConnectionPool(String name, String URL, String user, String password,
int maxConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}
/**
* ������ʹ�õ����ӷ��ظ����ӳ�
*
* @param con �ͻ������ͷŵ�����
*/
public synchronized void freeConnection(Connection con) {
// ��ָ�����Ӽ��뵽����ĩβ
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
/**
* �����ӳػ��һ�����е�����.
* ��û�п��е������ҵ�ǰ������С���������������,
* ��������.
* ��ԭ���Ǽ�Ϊ���е����Ӳ�����Ч,�������Vector��ɾ��,
* Ȼ��ݹ�����Գ����µĿ�������.
*/
public synchronized Connection getConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
// ��ȡ�����е�һ����������
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log("�����ӳ�" + name+"ɾ��һ����Ч����");
// �ݹ�����Լ�,�����ٴλ�ȡ��������
con = getConnection();
}
}
catch (SQLException e) {
log("�����ӳ�" + name+"ɾ��һ����Ч����");
// �ݹ�����Լ�,�����ٴλ�ȡ��������
con = getConnection();
}
}
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}
return con;
}
/**
* �����ӳػ�ȡ��������.����ָ���ͻ������ܹ��ȴ���ʱ��
* �μ�ǰһ��getConnection()����.
*
* @param timeout �Ժ���Ƶĵȴ�ʱ������
*/
public synchronized Connection getConnection(long timeout) {
long startTime = new Date().getTime();
Connection con;
while ((con = getConnection()) == null) {
try {
wait(timeout);
}
catch (InterruptedException e) {}
if ((new Date().getTime() - startTime) >= timeout) {
// wait()���ص�ԭ���dz�ʱ
return null;
}
}
return con;
}
/**
* �ر���������
*/
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log("�ر����ӳ�" + name+"�е�һ������");
}
catch (SQLException e) {
log(e, "���ر����ӳ�" + name+"�е�����");
}
}
freeConnections.removeAllElements();
}
/**
* �����µ�����
*/
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
log("���ӳ�" + name+"����һ���µ�����");
}
catch (SQLException e) {
log(e, "����������URL������: " + URL);
return null;
}
return con;
}
}
}