/* * Created on 25-Sep-2006 * */ package net.playtxt.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; public class TestDBCPWithRepDriver { public static void main(String[] args) { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // System.out.println("Setting up data source."); DataSource dataSource = setupDataSource(args[0]); System.out.println("Done."); // // Now, we can use JDBC DataSource as we normally would. // Connection conn = null; Statement stmt = null; ResultSet rset = null; long startTime = System.currentTimeMillis(); boolean keepRunning = true; while(keepRunning) { try { System.out.println("Creating connection."); conn = dataSource.getConnection(); conn.setReadOnly(false); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while(rset.next()) { for(int i=1;i<=numcols;i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch(SQLException e) { e.printStackTrace(); } finally { try { rset.close(); } catch(Exception e) { } try { stmt.close(); } catch(Exception e) { } try { conn.close(); } catch(Exception e) { } } long nowTime = System.currentTimeMillis(); long runSecs = (nowTime - startTime) / 1000; System.out.println("Now have run for " + runSecs); if (runSecs > 80) keepRunning = false; try{Thread.sleep(5000);}catch(InterruptedException e) {} } try { System.out.println("Creating connection."); conn = dataSource.getConnection(); conn.setReadOnly(true); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while(rset.next()) { for(int i=1;i<=numcols;i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch(SQLException e) { System.out.println("Exception:" + e.getMessage()); e.printStackTrace(); } finally { try { rset.close(); } catch(Exception e) { } try { stmt.close(); } catch(Exception e) { } try { conn.close(); } catch(Exception e) { } } } public static DataSource setupDataSource(String connectURI) { BasicDataSource ds = new BasicDataSource(); ds.setMaxActive(1); ds.setMaxIdle(1); ds.setDriverClassName("com.mysql.jdbc.ReplicationDriver"); ds.setUsername("clive"); ds.setPassword("clive"); ds.setUrl(connectURI); return ds; } public static void printDataSourceStats(DataSource ds) throws SQLException { BasicDataSource bds = (BasicDataSource) ds; System.out.println("NumActive: " + bds.getNumActive()); System.out.println("NumIdle: " + bds.getNumIdle()); } public static void shutdownDataSource(DataSource ds) throws SQLException { BasicDataSource bds = (BasicDataSource) ds; bds.close(); } }