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 import com.mchange.v2.c3p0.DriverManagerDataSource; 32 33 public final class LoadPoolBackedDataSource 34 { 35 final static int NUM_THREADS = 50; 36 final static int ITERATIONS_PER_THREAD = 1000; 37 38 static Random random = new Random(); 39 static DataSource ds; 40 41 public static void main(String [] argv) 42 { 43 if (argv.length > 0) 44 { 45 System.err.println( LoadPoolBackedDataSource.class.getName() + 46 " now requires no args. Please set everything in standard c3p0 config files."); 47 return; 48 } 49 String jdbc_url = null; 50 String username = null; 51 String password = null; 52 53 72 73 try 74 { 75 DataSource ds_unpooled = DataSources.unpooledDataSource(); 77 ds = DataSources.pooledDataSource( ds_unpooled ); 78 79 Connection con = null; 80 Statement stmt = null; 81 82 try 83 { 84 con = ds.getConnection(); 85 stmt = con.createStatement(); 86 stmt.executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )"); 87 System.err.println( "LoadPoolBackedDataSource -- TEST SCHEMA CREATED" ); 88 } 89 catch (SQLException e) 90 { 91 e.printStackTrace(); 92 System.err.println("relation testpbds already exists, or something " + 93 "bad happened."); 94 } 95 finally 96 { 97 StatementUtils.attemptClose( stmt ); 98 ConnectionUtils.attemptClose( con ); 99 } 100 101 Thread [] threads = new Thread [NUM_THREADS]; 102 for (int i = 0; i < NUM_THREADS; ++i) 103 { 104 Thread t = new ChurnThread(); 105 threads[i] = t; 106 t.start(); 107 System.out.println("THREAD MADE [" + i + "]"); 108 Thread.sleep(1000); 109 } 110 for (int i = 0; i < NUM_THREADS; ++i) 111 threads[i].join(); 112 113 } 114 catch (Exception e) 115 { e.printStackTrace(); } 116 finally 117 { 118 Connection con = null; 119 Statement stmt = null; 120 121 try 122 { 123 con = ds.getConnection(); 124 stmt = con.createStatement(); 125 stmt.executeUpdate("DROP TABLE testpbds"); 126 System.err.println( "LoadPoolBackedDataSource -- TEST SCHEMA DROPPED" ); 127 } 128 catch (Exception e) 129 { 130 e.printStackTrace(); 131 } 132 finally 133 { 134 StatementUtils.attemptClose( stmt ); 135 ConnectionUtils.attemptClose( con ); 136 } 137 } 138 } 139 140 static class ChurnThread extends Thread 141 { 142 public void run() 143 { 144 try 145 { 146 for( int i = 0; i < ITERATIONS_PER_THREAD; ++i ) 147 { 148 Connection con = null; 149 try 150 { 151 con = ds.getConnection(); 152 int select = random.nextInt(3); 153 switch (select) 154 { 155 case 0: 156 executeSelect( con ); 157 break; 158 case 1: 159 executeInsert( con ); 160 break; 161 case 2: 162 executeDelete( con ); 163 break; 164 } 165 PooledDataSource pds = (PooledDataSource) ds; 166 System.out.println( pds.getNumConnectionsDefaultUser() ); 167 System.out.println( pds.getNumIdleConnectionsDefaultUser() ); 168 System.out.println( pds.getNumBusyConnectionsDefaultUser() ); 169 System.out.println( pds.getNumConnectionsAllUsers() ); 170 } 171 finally 172 { ConnectionUtils.attemptClose( con ); } 173 174 } 176 } 177 catch (Exception e) 178 { e.printStackTrace(); } 179 } 180 } 181 182 static void executeInsert(Connection con) throws SQLException 183 { 184 Statement stmt = null; 185 try 186 { 187 stmt = con.createStatement(); 188 stmt.executeUpdate("INSERT INTO testpbds VALUES ('" + 189 random.nextInt() + "', '" + 190 random.nextInt() + "')"); 191 System.out.println("INSERTION"); 192 } 193 finally 194 { 195 StatementUtils.attemptClose( stmt ); 196 } 197 } 198 199 static void executeDelete(Connection con) throws SQLException 200 { 201 Statement stmt = null; 202 try 203 { 204 stmt = con.createStatement(); 205 stmt.executeUpdate("DELETE FROM testpbds;"); 206 System.out.println("DELETION"); 207 } 208 finally 209 { 210 StatementUtils.attemptClose( stmt ); 211 } 212 } 213 214 static void executeSelect(Connection con) throws SQLException 215 { 216 long l = System.currentTimeMillis(); 217 Statement stmt = null; 218 ResultSet rs = null; 219 try 220 { 221 stmt = con.createStatement(); 222 rs = stmt.executeQuery("SELECT count(*) FROM testpbds"); 223 rs.next(); System.out.println("SELECT [count=" + rs.getInt(1) + ", time=" + 225 (System.currentTimeMillis() - l) + " msecs]"); 226 } 227 finally 228 { 229 ResultSetUtils.attemptClose( rs ); 230 StatementUtils.attemptClose( stmt ); 231 } 232 } 233 234 private static void usage() 235 { 236 System.err.println("java " + 237 "-Djdbc.drivers=<comma_sep_list_of_drivers> " + 238 LoadPoolBackedDataSource.class.getName() + 239 " <jdbc_url> [<username> <password>]" ); 240 System.exit(-1); 241 } 242 243 244 } 245 | Popular Tags |