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 OneThreadRepeatedInsertOrQueryTest 33 { 34 final static String INSERT_STMT = "INSERT INTO testpbds VALUES ( ? , ? )"; 35 final static String SELECT_STMT = "SELECT count(*) FROM testpbds"; 36 37 static Random random = new Random(); 38 static DataSource ds; 39 40 public static void main(String [] argv) 41 { 42 String jdbc_url = null; 43 String username = null; 44 String password = null; 45 if (argv.length == 3) 46 { 47 jdbc_url = argv[0]; 48 username = argv[1]; 49 password = argv[2]; 50 } 51 else if (argv.length == 1) 52 { 53 jdbc_url = argv[0]; 54 username = null; 55 password = null; 56 } 57 else 58 usage(); 59 60 if (! jdbc_url.startsWith("jdbc:") ) 61 usage(); 62 63 64 try 65 { 66 DataSource ds_unpooled = DataSources.unpooledDataSource(jdbc_url, username, password); 67 ds = DataSources.pooledDataSource( ds_unpooled ); 68 69 Connection con = null; 70 Statement stmt = null; 71 72 try 73 { 74 con = ds.getConnection(); 75 stmt = con.createStatement(); 76 stmt.executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )"); 77 } 78 catch (SQLException e) 79 { 80 e.printStackTrace(); 81 System.err.println("relation testpbds already exists, or something " + 82 "bad happened."); 83 } 84 finally 85 { 86 StatementUtils.attemptClose( stmt ); 87 ConnectionUtils.attemptClose( con ); 88 } 89 90 while(true) 91 { 92 con = null; 93 try 94 { 95 con = ds.getConnection(); 96 boolean select = random.nextBoolean(); 97 if (select) 98 executeSelect( con ); 99 else 100 executeInsert( con ); 101 } 102 catch (Exception e) 103 { e.printStackTrace(); } 104 finally 105 { ConnectionUtils.attemptClose( con ); } 106 107 } 109 } 110 catch (Exception e) 111 { e.printStackTrace(); } 112 } 113 114 static void executeInsert(Connection con) throws SQLException 115 { 116 PreparedStatement pstmt = null; 117 try 118 { 119 pstmt = con.prepareStatement(INSERT_STMT); 120 pstmt.setInt(1, random.nextInt()); 121 pstmt.setInt(2, random.nextInt()); 122 pstmt.executeUpdate(); 123 System.out.println("INSERTION"); 124 } 125 finally 126 { 127 131 StatementUtils.attemptClose( pstmt ); 132 } 133 } 134 135 static void executeSelect(Connection con) throws SQLException 136 { 137 long l = System.currentTimeMillis(); 138 PreparedStatement pstmt = null; 139 ResultSet rs = null; 140 try 141 { 142 pstmt = con.prepareStatement(SELECT_STMT); 143 rs = pstmt.executeQuery(); 144 rs.next(); System.out.println("SELECT [count=" + rs.getInt(1) + ", time=" + 146 (System.currentTimeMillis() - l) + " msecs]"); 147 } 148 finally 149 { 150 ResultSetUtils.attemptClose( rs ); 151 StatementUtils.attemptClose( pstmt ); 152 } 153 } 154 155 private static void usage() 156 { 157 System.err.println("java " + 158 "-Djdbc.drivers=<comma_sep_list_of_drivers> " + 159 OneThreadRepeatedInsertOrQueryTest.class.getName() + 160 " <jdbc_url> [<username> <password>]" ); 161 System.exit(-1); 162 } 163 } 164 | Popular Tags |