1 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 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 [] 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 } 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 [] {"TABLE"} ); } 84 catch (Exception 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.err.println("Bye!"); 103 } 104 catch( Exception e ) 105 { e.printStackTrace(); } 106 } 107 } 108 109 110 111 112 113 | Popular Tags |