001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.test;
024:
025: import java.util.*;
026: import java.sql.*;
027: import javax.sql.*;
028: import com.mchange.v2.c3p0.*;
029: import com.mchange.v1.db.sql.*;
030:
031: public final class StatsTest {
032: static void display(ComboPooledDataSource cpds) throws Exception {
033: System.err.println("numConnections: "
034: + cpds.getNumConnections());
035: System.err.println("numBusyConnections: "
036: + cpds.getNumBusyConnections());
037: System.err.println("numIdleConnections: "
038: + cpds.getNumIdleConnections());
039: System.err.println("numUnclosedOrphanedConnections: "
040: + cpds.getNumUnclosedOrphanedConnections());
041: System.err.println();
042: }
043:
044: public static void main(String[] argv) {
045: try {
046: ComboPooledDataSource cpds = new ComboPooledDataSource();
047: cpds.setJdbcUrl(argv[0]);
048: cpds.setUser(argv[1]);
049: cpds.setPassword(argv[2]);
050: cpds.setMinPoolSize(5);
051: cpds.setAcquireIncrement(5);
052: cpds.setMaxPoolSize(20);
053:
054: System.err.println("Initial...");
055: display(cpds);
056: Thread.sleep(2000);
057:
058: HashSet hs = new HashSet();
059: for (int i = 0; i < 20; ++i) {
060: Connection c = cpds.getConnection();
061: hs.add(c);
062: System.err.println("Adding (" + (i + 1) + ") " + c);
063: display(cpds);
064: Thread.sleep(1000);
065:
066: // if (i == 9)
067: // {
068: // //System.err.println("hardReset()ing");
069: // //cpds.hardReset();
070: // System.err.println("softReset()ing");
071: // cpds.softReset();
072: // }
073: }
074:
075: int count = 0;
076: for (Iterator ii = hs.iterator(); ii.hasNext();) {
077: Connection c = ((Connection) ii.next());
078: System.err.println("Removing " + ++count);
079: ii.remove();
080: try {
081: c.getMetaData().getTables(null, null,
082: "PROBABLYNOT", new String[] { "TABLE" });
083: } catch (Exception e) {
084: System.err.println(e);
085: System.err.println();
086: continue;
087: } finally {
088: c.close();
089: }
090: display(cpds);
091: }
092:
093: System.err
094: .println("Closing data source, \"forcing\" garbage collection, and sleeping for 5 seconds...");
095: cpds.close();
096: System.gc();
097: System.err
098: .println("Main Thread: Sleeping for five seconds!");
099: Thread.sleep(5000);
100: // System.gc();
101: // Thread.sleep(5000);
102: System.err.println("Bye!");
103: } catch (Exception e) {
104: e.printStackTrace();
105: }
106: }
107: }
|