研究了Proxool連接池的源代碼後完成下面的程序Proxool連接池的配置過程略
/**//*
*@author 我為J狂 建立日期
*
*/
package net
blogjava
lzqdiy;
import
URL;
import java
sql
*;
import org
logicalcobwebs
proxool
ProxoolException;
import org
logicalcobwebs
proxool
ProxoolFacade;
import org
logicanfiguration
JAXPConfigurator;
import org
logicalcobwebs
proxool
admin
SnapshotIF;
public class DBConnection
{
private static boolean initialized = false;
private static int activeCount =
;
public static Connection getConnection() throws SQLException
{
Connection connection = null;
if (!initialized)
{
init();
}
connection = DriverManager
getConnection(
proxool
Develop
);
try
{
SnapshotIF snapshot = ProxoolFacade
getSnapshot(
Develop
true);
int curActiveCount = snapshot
getActiveConnectionCount();// 獲得活動連接數
int availableCount = snapshot
getAvailableConnectionCount();// 獲得可得到的連接數
int maxCount = snapshot
getMaximumConnectionCount();// 獲得總連接數
if (curActiveCount != activeCount)// 當活動連接數變化時輸出信息
{
System
out
println(
);
System
out
println(curActiveCount +
(active)
+ availableCount
+
(available)
+ maxCount +
(max)
);
System
out
println(
);
activeCount = curActiveCount;
}
} catch (ProxoolException e)
{
// TODO Auto
generated catch block
e
printStackTrace();
}
if (connection != null)
{
return connection;
} else
{
throw new NullPointerException(
Didn
t get connection
which probably means that no Driver accepted the URL
);
}
}
private static void init()
{
String fileName =
proxool
xml
;
URL resourceURL = null;
try
{
if (Thread
currentThread()
getContextClassLoader() != null)
{
resourceURL = Thread
currentThread()
getContextClassLoader()
getResource(fileName);
}
if (resourceURL == null)
{
resourceURL = DBConnection
class
getClassLoader()
getResource(
fileName);
}
nfigure(resourceURL
getFile()
false);
Class
forName(
org
logicalcobwebs
proxool
ProxoolDriver
);
} catch (ClassNotFoundException e)
{
// TODO Auto
generated catch block
e
printStackTrace();
} catch (ProxoolException e)
{
// TODO Auto
generated catch block
e
printStackTrace();
}
initialized = true;
}
}
proxoolxml內容
<?xml version=
encoding=
UTF
?>
<something
else
entirely>
<proxool>
<alias>Develop</alias>
<driver
url>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=
)(PORT=
)))(CONNECT_DATA=(SID=lzqdiy)(SERVER=DEDICATED)))
</driver
url>
<driver
class>oracle
jdbc
driver
OracleDriver</driver
class>
<driver
properties>
<property name=
user
value=
lzqdiy
/>
<property name=
password
value=
lzqdiy
/>
</driver
properties>
<maximum
connection
count>
</maximum
connection
count>
<minimum
connection
count>
</minimum
connection
count>
<house
keeping
test
sql>select CURRENT_DATE</house
keeping
test
sql>
</proxool>
</something
else
entirely>
通過語句
Connection con=DBConnection
getConnection();來獲得一個數據庫連接
當活動連接數變化時向控制台輸出連接信息
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25800.html