package Test;
import javax.sql.DataSource; import java.sql.*; import oracle.ucp.jdbc.ValidConnection; import oracle.ucp.jdbc.PoolDataSource; import oracle.ucp.jdbc.oracle.OracleJDBCConnectionPoolStatistics; import java.util.Random; import java.text.SimpleDateFormat; import java.text.DateFormat; import java.util.Calendar;
class Worker implements Runnable { PoolDataSource ds; Random random; Boolean cpuIntensive; Worker(PoolDataSource _ds) { ds = _ds; random = new Random(); } void databaseWorkload(Connection c) throws SQLException { //准备SQL语句 //只循环1次,这样写便于以后更改重复执行的次数 for(int i=0;i<1;i++) { PreparedStatement pstmt = c.prepareStatement("begin hr.proctest1; end;"); try{ pstmt.executeUpdate(); if (Test.VERBOSE) { System.out.println("Adding row to test1"); }
} catch(SQLException insertsqlex) { if (insertsqlex instanceof SQLIntegrityConstraintViolationException) { if (insertsqlex.getMessage().startsWith("ORA-00001: unique constraint (HR.PK_TEST1) violated")){ System.out.println("in catch block for constraint violation\n"); } } else throw insertsqlex; } pstmt.close(); } // c.rollback(); c.commit(); } public void run() { long counter = 0; boolean retry = false; Connection c = null; DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:MM:SS"); Calendar cal = Calendar.getInstance(); long nanoTimeStart=0,timeSpentOnDb = 0; try { c = ds.getConnection(); // Make sure auto commit if off: c.setAutoCommit(false);
ResultSet rs; Statement stmt = c.createStatement(); rs =
stmt.executeQuery("select '... Connected to '||sys_context('userenv','instance_name') from dual"); while (rs.next()) { // Only display for VERBOSE operation if (Test.VERBOSE) { System.out.println( dateFormat.format(cal.getTime()) + " " + rs.getString(1)); } } rs.close(); stmt.close(); */ if (retry) { System.out.println(" Application driven connection retry succeeded"); retry = false; }
nanoTimeStart = System.nanoTime(); if (Test.VERBOSE) { System.out.println("Executing databaseWorkload()"); } databaseWorkload(c); } catch (SQLException ea) { /* 有了AC,应用开发人员就不用编写代码来恢复事务*/ try { if (c == null ||!((ValidConnection)c).isValid()){ ea.printStackTrace(); System.out.println("Application error handling: attempting to get a new connection "+ea.getMessage()+"."); c.close(); String fcfInfo = ((OracleJDBCConnectionPoolStatistics) ds.getStatistics()).getFCFProcessingInfoProcessedOnly(); System.out.println("FCF information: " + fcfInfo); retry = true; } else { System.out.println("unknown exception: " + ea); } }catch (SQLException ea1) {} // synchronized (Test.statsLock) { Test.nbOfExceptions++; if(Test.applicationCrashOnErrors && Test.nbOfExceptions > 20) { System.err.println("20 fatal exceptions."); System.err.println(""); System.err.println("*** APPLICATION CRASHED ***"); System.err.println(""); System.exit(1); } } if(Test.VERBOSE) { ea.printStackTrace(); System.err.println("."+ea.getMessage()+"."); } } finally { timeSpentOnDb = (System.nanoTime()-nanoTimeStart)/1000000; // in ms try { if (c != null) { c.close(); if (Test.VERBOSE) { System.out.println("Closed connection"); } } } catch (SQLException ea) {} }
if(counter > 0) { synchronized (Test.statsLock) { Test.operationsCompleted++; Test.timeSpentOnDb += timeSpentOnDb; } }
if (Test.threadThinkTime > 0) { // Introduce delay between requests for processing webpages long timeToSleep = Test.threadThinkTime + random.nextInt((Test.threadThinkTime<10)?10:Test.threadThinkTime/10); try { Thread.sleep(timeToSleep); } catch (Exception ea) {} } counter++; }
} |