Java的JDBC数据库连接池实现方法Word格式.docx
- 文档编号:18431348
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:13
- 大小:20.55KB
Java的JDBC数据库连接池实现方法Word格式.docx
《Java的JDBC数据库连接池实现方法Word格式.docx》由会员分享,可在线阅读,更多相关《Java的JDBC数据库连接池实现方法Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
不过不建议这种写法,因为应该尽量避免使用JavaInterface,关于JavaInterface的缺点我另外再写文章讨论。
大家关注的是ConnectionPool的实现方法。
下面给出一种实现方法。
1.import
java.sql.*;
2.import
java.lang.reflect.*;
3.import
java.util.*;
4.import
java.io.*;
5.
6.public
SimpleConnetionPool
{
7.private
static
LinkedList
m_notUsedConnection
new
LinkedList();
8.private
HashSet
m_usedUsedConnection
HashSet();
9.private
String
m_url
"
;
10.private
m_user
11.private
m_password
12.static
final
boolean
DEBUG
true;
13.static
private
long
m_lastClearClosedConnection
System.currentTimeMillis();
14.public
CHECK_CLOSED_CONNECTION_TIME
4
*
60
1000;
//4
hours
15.
16.static
17.initDriver();
18.}
19.
20.private
SimpleConnetionPool()
21.}
22.
23.private
initDriver()
24.Driver
driver
25.//load
mysql
driver
26.try
27.driver
(Driver)
Class.forName("
com.mysql.jdbc.Driver"
).newInstance();
28.installDriver(driver);
29.}
catch
(Exception
e)
30.}
31.
32.//load
postgresql
33.try
34.driver
org.postgresql.Driver"
35.installDriver(driver);
36.}
37.}
38.}
39.
40.public
installDriver(Driver
driver)
41.try
42.DriverManager.registerDriver(driver);
43.}
44.e.printStackTrace();
45.}
46.}
47.
48.
49.public
synchronized
getConnection()
50.clearClosedConnection();
51.while
(m_notUsedConnection.size()
>
0)
52.try
53.ConnectionWrapper
wrapper
(ConnectionWrapper)
m_notUsedConnection.removeFirst();
54.if
(wrapper.connection.isClosed())
55.continue;
56.}
57.m_usedUsedConnection.add(wrapper);
58.if
(DEBUG)
59.wrapper.debugInfo
Throwable("
initial
statement"
);
60.}
61.return
wrapper.connection;
62.}
63.}
64.}
65.int
newCount
getIncreasingConnectionCount();
66.LinkedList
list
67.ConnectionWrapper
68.for
(int
i
0;
<
newCount;
i++)
69.wrapper
getNewConnection();
70.if
(wrapper
!
null)
71.list.add(wrapper);
72.}
73.}
74.if
(list.size()
==
75.return
76.}
77.wrapper
list.removeFirst();
78.m_usedUsedConnection.add(wrapper);
79.
80.m_notUsedConnection.addAll(list);
81.list.clear();
82.
83.return
84.}
85.
86.private
ConnectionWrapper
getNewConnection()
87.try
88.Connection
con
DriverManager.getConnection(m_url,
m_user,
m_password);
89.ConnectionWrapper
ConnectionWrapper(con);
90.return
wrapper;
91.}
92.e.printStackTrace();
93.}
94.return
95.}
96.
97.static
pushConnectionBackToPool(ConnectionWrapper
con)
98.boolean
exist
m_usedUsedConnection.remove(con);
99.if
(exist)
100.m_notUsedConnection.addLast(con);
101.}
102.}
103.
104.public
int
close()
105.int
count
106.
107.Iterator
iterator
m_notUsedConnection.iterator();
108.while
(iterator.hasNext())
109.try
110.(
iterator.next()).close();
111.count++;
112.}
113.}
114.}
115.m_notUsedConnection.clear();
116.
117.iterator
m_usedUsedConnection.iterator();
118.while
119.try
120.ConnectionWrapper
iterator.next();
121.wrapper.close();
122.if
123.wrapper.debugInfo.printStackTrace();
124.}
125.count++;
126.}
127.}
128.}
129.m_usedUsedConnection.clear();
130.
131.return
count;
132.}
133.
134.private
clearClosedConnection()
135.long
time
136.//sometimes
user
change
system
time,just
return
137.if
(time
m_lastClearClosedConnection)
138.time
m_lastClearClosedConnection;
139.return;
140.}
141.//no
need
check
very
often
142.if
-
CHECK_CLOSED_CONNECTION_TIME)
143.return;
144.}
145.m_lastClearClosedConnection
time;
146.
147.//begin
check
148.Iterator
149.while
150.ConnectionWrapper
151.try
152.if
153.iterator.remove();
154.}
155.}
156.iterator.remove();
157.if
158.System.out.println("
connection
is
closed,
this
StackTrace"
159.wrapper.debugInfo.printStackTrace();
160.}
161.}
162.}
163.
164.//make
pool
size
smaller
if
too
big
165.int
decrease
getDecreasingConnectionCount();
166.if
decrease)
167.return;
168.}
169.
170.while
(decrease--
171.ConnectionWrapper
172.try
173.wrapper.connection.close();
174.}
175.}
176.}
177.}
178.
179./**
180.
get
increasing
count,
not
just
add
1
connection
181.
@return
count
182.
*/
183.public
getIncreasingConnectionCount()
184.int
1;
185.int
current
getConnectionCount();
186.count
/
4;
187.if
(count
1)
188.count
189.}
190.return
191.}
192.
193./**
194.
decreasing
remove
195.
196.
197.public
getDecreasingConnectionCount()
198.int
199.int
200.if
(current
10)
201.return
202.}
203.return
3;
204.}
205.
206.public
printDebugMsg()
207.printDebugMsg(System.out);
208.}
209.
210.public
printDebugMsg(PrintStream
out)
211.if
(DEBUG
false)
212.return;
213.}
214.StringBuffer
msg
StringBuffer();
215.msg.append("
debug
message
in
+
SimpleConnetionPool.class.getName());
216.msg.append("
\r\n"
217.msg.append("
total
pool:
getConnectionCount());
218.msg.append("
219.msg.append("
used
count:
getNotUsedConnectionCount());
220.msg.append("
221.msg.append("
connection,
getUsedConnectionCount());
222.out.println(msg);
223.Iterator
224.while
225.ConnectionWrapper
226.wrapper.debugInfo.printStackTrace(out);
227.}
228.out.println();
229.}
230.
231.public
getNotUsedConnectionCount()
232.return
m_notUsedConnection.size();
233.}
234.
235.public
getUsedConnectionCount()
236.return
m_usedUsedConnection.size();
237.}
238.
239.public
getConnectionCount()
240.return
m_notUsedConnection.size()
241.}
242.
243.public
getUrl()
244.return
m_url;
245.}
246.
247.public
setUrl(String
url)
248.if
(url
249.return;
250.}
251.m_url
url.trim();
252.}
253.
254.public
getUser()
255.return
m_user;
256.}
257.
258.public
setUser(String
user)
259.if
(user
260.return;
261.}
262.m_user
user.trim();
263.}
264.
265.public
getPassword()
266.return
m_password;
267.}
268.
269.public
setPassword(String
password)
270.if
(password
271.return;
272.}
273.m_password
password.trim();
274.}
275.
276.}
277.
278.class
InvocationHandler
279.private
CLOSE_METHOD_NAME
close"
280.public
281.private
m_originConnection
282.public
lastAccessTime
283.Throwable
debugInfo
284.
285.ConnectionWrapper(Connec
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java JDBC 数据库连接 实现 方法