📄 c3p0benchmarkapp.java
字号:
} } static class DataBaseMetaDataListNonexistentTablesTest extends Test { DataBaseMetaDataListNonexistentTablesTest() { super("DataBaseMetaDataListNonexistentTablesTest"); } protected long test(DataSource ds, int n) throws SQLException { Connection con = null; Statement stmt = null; try { con = ds.getConnection(); return test( con , n ); } finally { StatementUtils.attemptClose( stmt ); ConnectionUtils.attemptClose( con ); } } long test(Connection con, int n) throws SQLException { ResultSet rs = null; try { long start; long end; start = System.currentTimeMillis(); for (int i = 0; i < n; ++i) rs = con.getMetaData().getTables( null, null, "PROBABLYNOT", new String[] {"TABLE"} ); end = System.currentTimeMillis(); return end - start; } finally { ResultSetUtils.attemptClose( rs ); } } } static class PreparedStatementAcquireTest extends Test { PreparedStatementAcquireTest() { super("Acquire and Cleanup a PreparedStatement (same statement, many times)"); } protected long test(DataSource ds, int n) throws SQLException { long start; long end; Connection con = null; PreparedStatement pstmt = null; try { con = ds.getConnection(); start = System.currentTimeMillis(); for (int i = 0; i < n; ++i) { try { pstmt = con.prepareStatement(EMPTY_TABLE_CONDITIONAL_SELECT); }/* Leftover random abuses from ad hoc testing... { pstmt = con.prepareStatement(EMPTY_TABLE_CONDITIONAL_SELECT, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE, ResultSet.HOLD_CURSORS_OVER_COMMIT); } { pstmt = con.prepareStatement(N_ENTRY_TABLE_INSERT); }*/ finally { StatementUtils.attemptClose( pstmt ); } } end = System.currentTimeMillis(); return end - start; } finally { ConnectionUtils.attemptClose( con ); } } } static class PreparedStatementEmptyTableSelectTest extends Test { PreparedStatementEmptyTableSelectTest() { super("Empty Table PreparedStatement Select (on a single PreparedStatement)"); } protected long test(DataSource ds, int n) throws SQLException { Connection con = null; PreparedStatement pstmt = null; try { con = ds.getConnection(); pstmt = con.prepareStatement(EMPTY_TABLE_SELECT);// Leftover from ad-hoc testing...//// pstmt = con.prepareStatement(EMPTY_TABLE_SELECT, // ResultSet.TYPE_SCROLL_SENSITIVE, // ResultSet.CONCUR_UPDATABLE, // ResultSet.HOLD_CURSORS_OVER_COMMIT); return test( pstmt , n ); } finally { StatementUtils.attemptClose( pstmt ); ConnectionUtils.attemptClose( con ); } } long test(PreparedStatement pstmt, int n) throws SQLException { long start; long end; start = System.currentTimeMillis(); for (int i = 0; i < n; ++i) pstmt.executeQuery().close(); end = System.currentTimeMillis(); return end - start; } } static class ResultSetReadTest extends Test { ResultSetReadTest() { super("Reading one row / one entry from a result set"); } protected long test(DataSource ds, int n) throws SQLException { if (n > 10000) throw new IllegalArgumentException("10K max."); long start; long end; Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = ds.getConnection(); pstmt = con.prepareStatement(N_ENTRY_TABLE_SELECT); rs = pstmt.executeQuery(); start = System.currentTimeMillis(); for (int i = 0; i < n; ++i) { if (! rs.next() ) System.err.println("huh?"); rs.getInt(1); } end = System.currentTimeMillis(); return end - start; } finally { ResultSetUtils.attemptClose( rs ); StatementUtils.attemptClose( pstmt ); ConnectionUtils.attemptClose( con ); } } } static class FiveThreadPSQueryTestTest extends Test { // only for stupid test to simulate (illegal) concurrent access to a Statement// volatile Statement stmt; FiveThreadPSQueryTestTest() { super( "Five threads getting a connection, executing a query, " + System.getProperty( "line.separator" ) + "and retrieving results concurrently via a prepared statement (in a transaction)." ); } protected long test(final DataSource ds, final int n) throws Exception { class QueryThread extends Thread { QueryThread(int num) { super("QueryThread-" + num);} public void run() { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; for (int i = 0; i < (n / 5); ++i) { try { con = ds.getConnection();// System.err.println("before txn isolation set: " + con.getTransactionIsolation());// con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE );// //con.setTransactionIsolation( Connection.TRANSACTION_READ_UNCOMMITTED );// System.err.println("after txn isolation set: " + con.getTransactionIsolation()); con.setAutoCommit( false ); pstmt = con.prepareStatement( EMPTY_TABLE_CONDITIONAL_SELECT );// if (Math.random() < 0.5)// stmt = pstmt;// else if (stmt != null)// stmt.getResultSet();// if (Math.random() < 0.1 && con instanceof C3P0ProxyConnection)// con.close(); pstmt.setString(1, "boo"); rs = pstmt.executeQuery(); while( rs.next() ) System.err.println("Huh?? Empty table has values?"); //System.out.println(this + " " + i);// if (ds instanceof PooledDataSource)// {// PooledDataSource pds = (PooledDataSource) ds;// System.err.println("numConnections: " + pds.getNumConnections() );// System.err.println("numIdleConnections: " + pds.getNumIdleConnections() );// System.err.println("numBusyConnections: " + pds.getNumBusyConnections() );// System.err.println();// } con.commit(); } catch (Exception e) { System.err.print("FiveThreadPSQueryTestTest exception -- "); e.printStackTrace(); try { if (con != null) con.rollback(); } catch (SQLException e2) { System.err.print("Rollback on exception failed! -- "); e2.printStackTrace(); } } finally { ResultSetUtils.attemptClose( rs ); StatementUtils.attemptClose( pstmt ); ConnectionUtils.attemptClose( con ); con = null;// StatementUtils.attemptClose( pstmt ); //dup close// ConnectionUtils.attemptClose( con ); //dup close// try { System.err.println( pstmt.getConnection() ); } catch (Exception e) {e.printStackTrace();}// ResultSetUtils.attemptClose( rs ); } } //System.out.println(this + " finished."); } } long start = System.currentTimeMillis(); Thread[] ts = new Thread[5]; for (int i = 0; i < 5; ++i) { ts[i] = new QueryThread(i); ts[i].start(); } for (int i = 0; i < 5; ++i) ts[i].join(); return System.currentTimeMillis() - start; } }// static class TenByTwoResultSetReadTest extends Test// {// TenByTwoResultSetReadTest()// { super("Reading all entryies from a 10 row 2 col result set"); }// protected long test(DataSource ds, int n) throws SQLException// {// long start;// long end; // long start_ctrl;// long end_ctrl;// Connection con = null;// PreparedStatement pstmt = null;// ResultSet rs = null; // start = System.currentTimeMillis();// for (int i = 0; i < n; ++i)// {// try// {// con = ds.getConnection();// pstmt = con.prepareStatement(N_ENTRY_TABLE_SELECT);// rs = pstmt.executeQuery();// while( rs.next() )// {// rs.getInt(1);// rs.getInt(2);// }// }// finally// { // ResultSetUtils.attemptClose( rs ); // StatementUtils.attemptClose( pstmt ); // ConnectionUtils.attemptClose( con ); // }// }// end = System.currentTimeMillis();// start_ctrl = System.currentTimeMillis();// for (int i = 0; i < n; ++i)// {// try// {// con = ds.getConnection();// pstmt = con.prepareStatement(N_ENTRY_TABLE_SELECT);// rs = pstmt.executeQuery();// }// finally// { // ResultSetUtils.attemptClose( rs ); // StatementUtils.attemptClose( pstmt ); // ConnectionUtils.attemptClose( con ); // }// }// end_ctrl = System.currentTimeMillis();// return (end - start) - (end_ctrl - start_ctrl);// }// }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -