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 PSLoadPoolBackedDataSource 33 { 34 final static String INSERT_STMT = "INSERT INTO testpbds VALUES ( ? , ? )"; 35 final static String SELECT_STMT = "SELECT count(*) FROM testpbds"; 36 final static String DELETE_STMT = "DELETE FROM testpbds"; 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( PSLoadPoolBackedDataSource.class.getName() + 46 " now requires no args. Please set everything in standard c3p0 config files."); 47 return; 48 } 49 50 String jdbc_url = null; 51 String username = null; 52 String password = null; 53 54 73 74 try 75 { 76 79 DataSource ds_unpooled = DataSources.unpooledDataSource(); 80 ds = DataSources.pooledDataSource( ds_unpooled ); 81 82 84 Connection con = null; 85 Statement stmt = null; 86 87 try 88 { 89 con = ds_unpooled.getConnection(); 90 stmt = con.createStatement(); 91 stmt.executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )"); 92 } 93 catch (SQLException e) 94 { 95 e.printStackTrace(); 96 System.err.println("relation testpbds already exists, or something " + 97 "bad happened."); 98 } 99 finally 100 { 101 StatementUtils.attemptClose( stmt ); 102 ConnectionUtils.attemptClose( con ); 103 } 104 105 for (int i = 0; i < 50; ++i) 107 { 108 Thread t = new ChurnThread(); 109 t.start(); 110 System.out.println("THREAD MADE [" + i + "]"); 111 Thread.sleep(1000); 112 } 113 114 } 115 catch (Exception e) 116 { e.printStackTrace(); } 117 } 118 119 static class ChurnThread extends Thread 120 { 121 public void run() 122 { 123 try 124 { 125 while(true) 126 { 127 Connection con = null; 128 try 129 { 130 con = ds.getConnection(); 131 int select = random.nextInt(3); 132 switch (select) 133 { 134 case 0: 135 executeSelect( con ); 136 break; 137 case 1: 138 executeInsert( con ); 139 break; 140 case 2: 141 executeDelete( con ); 142 break; 143 } 144 } 145 catch (Exception e) 146 { e.printStackTrace(); } 147 finally 148 { ConnectionUtils.attemptClose( con ); } 149 150 } 152 } 153 catch (Exception e) 154 { e.printStackTrace(); } 155 } 156 } 157 158 static void executeInsert(Connection con) throws SQLException 159 { 160 PreparedStatement pstmt = null; 161 try 162 { 163 pstmt = con.prepareStatement(INSERT_STMT); 164 pstmt.setInt(1, random.nextInt()); 165 pstmt.setInt(2, random.nextInt()); 166 pstmt.executeUpdate(); 167 System.out.println("INSERTION"); 168 } 169 finally 170 { 171 175 StatementUtils.attemptClose( pstmt ); 176 } 177 } 178 179 static void executeSelect(Connection con) throws SQLException 180 { 181 long l = System.currentTimeMillis(); 182 PreparedStatement pstmt = null; 183 ResultSet rs = null; 184 try 185 { 186 pstmt = con.prepareStatement(SELECT_STMT); 187 rs = pstmt.executeQuery(); 188 rs.next(); System.out.println("SELECT [count=" + rs.getInt(1) + ", time=" + 190 (System.currentTimeMillis() - l) + " msecs]"); 191 } 192 finally 193 { 194 ResultSetUtils.attemptClose( rs ); 195 StatementUtils.attemptClose( pstmt ); 196 } 197 } 198 199 static void executeDelete(Connection con) throws SQLException 200 { 201 PreparedStatement pstmt = null; 202 ResultSet rs = null; 203 try 204 { 205 pstmt = con.prepareStatement(DELETE_STMT); 206 int deleted = pstmt.executeUpdate(); 207 System.out.println("DELETE [" + deleted + " rows]"); 208 } 209 finally 210 { 211 ResultSetUtils.attemptClose( rs ); 212 StatementUtils.attemptClose( pstmt ); 213 } 214 } 215 216 226 } 227 | Popular Tags |