Often dba ask me for some Java code to spawn sessions and make them work for a little. Here is the code:
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
public class Test implements Runnable {
public static String hostname = "db301rai.intranet.fw";
public static int port = 1529;
public static String SID = "VOD_INT";
public static String username = "system";
public static String password = "manager";
public static String query = "select 'pppp' from dual";
/** Run for 10 seconds */
public static long periodMillis = 10000;
public static int sessions = 50;
private Connection c;
public Test(Connection c) {
this.c = c;
}
public void run() {
try {
long startTime = System.currentTimeMillis();
while(System.currentTimeMillis() - startTime < periodMillis) {
PreparedStatement ps = c.prepareStatement(query,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
try {
ps.execute();
System.out.println(new Date() + ": Query executed");
} finally {
ps.close();
}
}
} catch(Exception ex) {
ex.printStackTrace();
} finally {
try {c.close();
}catch (Exception ex) {
System.err.println("Exception closing db connection");
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
args.getClass(); //avoid compiler hint
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
String conString = "jdbc:oracle:thin:@" + hostname + ":" + port + ":" + SID;
Test[] tests = new Test[sessions];
/* First create all connection, this can't be parallelized due to
* bug 4263113. Please see the link below for a solution
* http://www.fadalti.com/java/solution_for_bug_4263113.htm */
for(int i=0; i<tests.length; i++) {
Connection c = DriverManager.getConnection(conString, username, password);
System.out.println("Opened connection " + i);
tests[i] = new Test(c);
}
for(int i=0; i<tests.length; i++) {
Thread t = new Thread(tests[i]);
t.setDaemon(false);
t.start();
}
}
}
|