使用:proxool-0.9.1.zip
需要加入的jar如下:
代码如下:
package yerasel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
public class Test {
int beginIndex = 0; // 待取记录起始数
int endIndex = 10; // 待取得记录终止跳
String MySQLdbTableName = "usrpsw";
String MySQLreq = "select * from " + MySQLdbTableName + " limit " + beginIndex + ", " + endIndex;
/**
* proxool方式测试
*
* @throws Exception
*/
public void test2() throws Exception {
// Java应用中先要加载配置文件,否则谁知道你配置给谁用的
JAXPConfigurator
.configure("proxool.xml", false);
// 1:注册驱动类,这次这个驱动已经不是Oracle的驱动了,是Proxool专用的驱动
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
// 2:创建数据库连接,这个参数是一个字符串,是数据源的别名,在配置文件中配置的timalias,参数格式为:proxool.数据源的别名
Connection conn = DriverManager.getConnection("proxool.mysql");
for(int i = 0; i< 20; i++)
conn = DriverManager.getConnection("proxool.mysql");
// 3:创建执行SQL的对象
Statement stmt = conn.createStatement();
// 4:执行SQL,并获取返回结果
ResultSet res = stmt.executeQuery(MySQLreq);
// 5:处理返回结果,此处打印查询结果
ResultSetMetaData rsmd = res.getMetaData();
int columnCount = rsmd.getColumnCount();
int rowCount = 0;
while (res.next()) {
System.out.print(rowCount + " ");
rowCount++;
for (int j = 1; j <= columnCount; j++) {// 循环处理
String strRes = res.getString(j);
System.out.print(strRes + "|\t");
}
System.out.println();
}
// 6:关闭数据库连接
conn.close();
}
public static void main(String[] args) throws Exception {
Test obj = new Test();
obj.test2();
}
}
proxool.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>mysql</alias>
<!--数据源的别名-->
<driver-url>jdbc:mysql://127.0.0.1:3306/mysqlconn</driver-url>
<!--url连接串-->
<driver-class>com.mysql.jdbc.Driver</driver-class>
<!--驱动类-->
<driver-properties>
<property name="user" value="root"/>
<!--用户名-->
<property name="password" value="123456"/>
<!--密码-->
</driver-properties>
<!--最大连接数(默认5个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->
<maximum-connection-count>5</maximum-connection-count>
<!--最小连接数(默认2个)-->
<minimum-connection-count>1</minimum-connection-count>
<!--proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 默认30秒-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!--没有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->
<maximum-new-connections>6</maximum-new-connections>
<!--最少保持的空闲连接数(默认2个)-->
<prototype-count>5</prototype-count>
<!--在使用之前测试-->
<test-before-use>true</test-before-use>
<!--用于保持连接的测试语句 -->
<house-keeping-test-sql>show tables</house-keeping-test-sql>
</proxool>
</something-else-entirely>
注意,在程序中发起了过多线程,目的就是检验线程限制是否有效。
数据库内容如下:
结果如下:
[DEBUG 2012-08-06 15:57:24] Configuring from xml file: proxool.xml
[DEBUG 2012-08-06 15:57:24] SAXParserFactory class: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
[DEBUG 2012-08-06 15:57:24] sax parser classcom.sun.org.apache.xerces.internal.jaxp.SAXParserImpl
[DEBUG 2012-08-06 15:57:24] XML reader class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser
[DEBUG 2012-08-06 15:57:24] Setting sax feature: 'http://xml.org/sax/features/namespaces'. State: true.
[DEBUG 2012-08-06 15:57:24] Setting sax feature: 'http://xml.org/sax/features/namespace-prefixes'. State: false.
[DEBUG 2012-08-06 15:57:24] Adding driver property: user=root
[DEBUG 2012-08-06 15:57:24] Adding driver property: password=*******
[DEBUG 2012-08-06 15:57:24] Setting property 'proxool.maximum-connection-count' to value '5'.
[DEBUG 2012-08-06 15:57:24] Setting property 'proxool.minimum-connection-count' to value '1'.
[DEBUG 2012-08-06 15:57:24] Setting property 'proxool.house-keeping-sleep-time' to value '90000'.
[DEBUG 2012-08-06 15:57:24] Setting property 'proxool.maximum-new-connections' to value '6'.
[DEBUG 2012-08-06 15:57:24] Setting property 'proxool.prototype-count' to value '5'.
[DEBUG 2012-08-06 15:57:24] Setting property 'proxool.test-before-use' to value 'true'.
[DEBUG 2012-08-06 15:57:24] Setting property 'proxool.house-keeping-test-sql' to value 'show tables'.
[DEBUG 2012-08-06 15:57:24] Created url: proxool.mysql:com.mysql.jdbc.Driver:jdbc:mysql://127.0.0.1:3306/mysqlconn
[INFO 2012-08-06 15:57:24] Proxool 0.9.1 (23-Aug-2008 11:10)
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.driver=com.mysql.jdbc.Driver
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.url=jdbc:mysql://127.0.0.1:3306/mysqlconn
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.minimum-connection-count=1
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.test-before-use=true
[WARN 2012-08-06 15:57:24] Use of proxool.maximum-new-connections is deprecated. Use more descriptive proxool.simultaneous-build-throttle instead.
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.maximum-new-connections=6
[DEBUG 2012-08-06 15:57:24] Delegating property to driver: user=root
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.house-keeping-sleep-time=90000
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.prototype-count=5
[DEBUG 2012-08-06 15:57:24] Delegating property to driver: password=********
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.maximum-connection-count=5
[DEBUG 2012-08-06 15:57:24] Recognised proxool property: proxool.house-keeping-test-sql=show tables
[DEBUG 2012-08-06 15:57:24] Registered shutdownHook
[DEBUG 2012-08-06 15:57:24] Registering 'mysql' house keeper
[DEBUG 2012-08-06 15:57:24] Starting a house keeper thread
[DEBUG 2012-08-06 15:57:25] Remembering default value: getTransactionIsolation() = 4
[DEBUG 2012-08-06 15:57:25] Remembering default value: getHoldability() = 2
[DEBUG 2012-08-06 15:57:25] Remembering default value: getCatalog() = mysqlconn
[DEBUG 2012-08-06 15:57:25] Remembering default value: isReadOnly() = false
[DEBUG 2012-08-06 15:57:25] Remembering default value: getTypeMap() = {}
[INFO 2012-08-06 15:57:25] Proxool statistics legend: "s - r (a/t/o)" > s=served, r=refused (only shown if non-zero), a=active, t=total, o=offline (being tested)
[INFO 2012-08-06 15:57:25] Proxool statistics legend: "s - r (a/t/o)" > s=served, r=refused (only shown if non-zero), a=active, t=total, o=offline (being tested)
[DEBUG 2012-08-06 15:57:25] 000000 (01/02/00) - Connection #2 created to keep 5 available = AVAILABLE
[DEBUG 2012-08-06 15:57:25] 000000 (01/02/00) - Connection #1 created on demand = ACTIVE
[DEBUG 2012-08-06 15:57:25] 000000 (01/02/00) - Connection #1 tested: OK
[DEBUG 2012-08-06 15:57:25] 000001 (01/03/00) - Connection #3 created to keep 5 available = AVAILABLE
[DEBUG 2012-08-06 15:57:25] 000001 (01/04/00) - Connection #4 created to keep 5 available = AVAILABLE
[DEBUG 2012-08-06 15:57:25] 000001 (01/05/00) - Connection #5 created to keep 5 available = AVAILABLE
[DEBUG 2012-08-06 15:57:25] Implementing interface java.sql.Connection
[DEBUG 2012-08-06 15:57:25] Implementing interface java.sql.Wrapper
[DEBUG 2012-08-06 15:57:25] 000001 (02/05/00) - Connection #2 tested: OK
[DEBUG 2012-08-06 15:57:25] 000002 (03/05/00) - Connection #3 tested: OK
[DEBUG 2012-08-06 15:57:25] 000003 (04/05/00) - Connection #4 tested: OK
[DEBUG 2012-08-06 15:57:25] 000004 (05/05/00) - Connection #5 tested: OK
[INFO 2012-08-06 15:57:25] 000005 -000001 (05/05/00) - Couldn't get connection because we are at maximum connection count and there are none available
Exception in thread "main" java.sql.SQLException: Couldn't get connection because we are at maximum connection count (5/5) and there are none available
at org.logicalcobwebs.proxool.Prototyper.quickRefuse(Prototyper.java:309)
at org.logicalcobwebs.proxool.ConnectionPool.getConnection(ConnectionPool.java:152)
at org.logicalcobwebs.proxool.ProxoolDriver.connect(ProxoolDriver.java:89)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at yerasel.Test.test2(Test.java:36)
at yerasel.Test.main(Test.java:60)
[DEBUG 2012-08-06 15:57:25] Running ShutdownHook
[INFO 2012-08-06 15:57:25] Shutting down 'mysql' pool immediately [Shutdown Hook]
[INFO 2012-08-06 15:57:25] Waiting until Mon Aug 06 15:57:25 CST 2012 for all connections to become inactive (active count is 5).
[WARN 2012-08-06 15:57:25] Shutdown waited for 0 milliseconds for all the connections to become inactive but the active count is still 5. Shutting down anyway.
[DEBUG 2012-08-06 15:57:25] 000005 -000001 (04/04/00) - #0005 removed because of shutdown.
[DEBUG 2012-08-06 15:57:25] Connection #5 closed
[DEBUG 2012-08-06 15:57:25] 000005 -000001 (03/03/00) - #0004 removed because of shutdown.
[DEBUG 2012-08-06 15:57:25] Connection #4 closed
[DEBUG 2012-08-06 15:57:25] 000005 -000001 (02/02/00) - #0003 removed because of shutdown.
[DEBUG 2012-08-06 15:57:25] Connection #3 closed
[DEBUG 2012-08-06 15:57:25] 000005 -000001 (01/01/00) - #0001 removed because of shutdown.
[DEBUG 2012-08-06 15:57:25] Connection #1 closed
[DEBUG 2012-08-06 15:57:25] 000005 -000001 (00/00/00) - #0002 removed because of shutdown.
[DEBUG 2012-08-06 15:57:25] Connection #2 closed
[INFO 2012-08-06 15:57:25] 'mysql' pool has been closed down by Shutdown Hook in 0 milliseconds.
[INFO 2012-08-06 15:57:25] Stopping Prototyper thread
[INFO 2012-08-06 15:57:25] Stopping HouseKeeper thread