KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > test > StatsTest


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.test;
25
26 import java.util.*;
27 import java.sql.*;
28 import javax.sql.*;
29 import com.mchange.v2.c3p0.*;
30 import com.mchange.v1.db.sql.*;
31
32 public final class StatsTest
33 {
34     static void display( ComboPooledDataSource cpds ) throws Exception JavaDoc
35     {
36     System.err.println("numConnections: " + cpds.getNumConnections());
37     System.err.println("numBusyConnections: " + cpds.getNumBusyConnections());
38     System.err.println("numIdleConnections: " + cpds.getNumIdleConnections());
39     System.err.println("numUnclosedOrphanedConnections: " + cpds.getNumUnclosedOrphanedConnections());
40     System.err.println();
41     }
42
43     public static void main(String JavaDoc[] argv)
44     {
45     try
46         {
47         ComboPooledDataSource cpds = new ComboPooledDataSource();
48         cpds.setJdbcUrl( argv[0] );
49         cpds.setUser( argv[1] );
50         cpds.setPassword( argv[2] );
51         cpds.setMinPoolSize(5);
52         cpds.setAcquireIncrement(5);
53         cpds.setMaxPoolSize(20);
54
55         System.err.println("Initial...");
56         display( cpds );
57         Thread.sleep(2000);
58
59         HashSet hs = new HashSet();
60         for (int i = 0; i < 20; ++i)
61             {
62             Connection c = cpds.getConnection();
63             hs.add( c );
64             System.err.println( "Adding (" + (i + 1) + ") " + c );
65             display( cpds );
66             Thread.sleep(1000);
67
68 // if (i == 9)
69
// {
70
// //System.err.println("hardReset()ing");
71
// //cpds.hardReset();
72
// System.err.println("softReset()ing");
73
// cpds.softReset();
74
// }
75
}
76         
77         int count = 0;
78         for (Iterator ii = hs.iterator(); ii.hasNext(); )
79             {
80             Connection c = ((Connection) ii.next());
81             System.err.println("Removing " + ++count);
82             ii.remove();
83             try { c.getMetaData().getTables( null, null, "PROBABLYNOT", new String JavaDoc[] {"TABLE"} ); }
84             catch (Exception JavaDoc e)
85                 {
86                 System.err.println( e );
87                 System.err.println();
88                 continue;
89                 }
90             finally
91                 { c.close(); }
92             display( cpds );
93             }
94
95         System.err.println("Closing data source, \"forcing\" garbage collection, and sleeping for 5 seconds...");
96         cpds.close();
97         System.gc();
98         System.err.println("Main Thread: Sleeping for five seconds!");
99         Thread.sleep(5000);
100 // System.gc();
101
// Thread.sleep(5000);
102
System.err.println("Bye!");
103         }
104     catch( Exception JavaDoc e )
105         { e.printStackTrace(); }
106     }
107 }
108
109
110
111
112
113
Popular Tags