package Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import oracle.ucp.admin.UniversalConnectionPoolManagerImpl;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.admin.UniversalConnectionPoolManager;
import oracle.ucp.UniversalConnectionPoolAdapter;
import oracle.ucp.UniversalConnectionPoolException;
import oracle.ucp.UniversalConnectionPoolStatistics;
public class Test extends Thread {
static final int DELAY_BETWEEN_PRINTING_STATS = 5 * 1000;
static boolean VERBOSE = true;
private static String PROP_FILE="test.properties";
static int connectionWaitTimeout = 3; // seconds
static int nbOfThreads = 0;
static int ucpPoolSize = 0;
static int threadThinkTime = 0;
// 12.2以上版本(或者打过p31112088)补丁,不需要验证连接有效性
static boolean validateConnectionOnBorrow = false;
static boolean applicationCrashOnErrors = true;
static boolean fastConnectionFailover = false;
static boolean cpuIntensive = false;
static final Object statsLock = new Object();
static int operationsCompleted = 0;
static long timeSpentOnDb = 0;
static int nbOfExceptions = 0;
static public void main(String args[])
throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
if(args.length > 0) {
PROP_FILE = args[0];
}
try {
Properties prop = new Properties();
try {
prop.load(new FileInputStream(PROP_FILE));
} catch (IOException e) {e.printStackTrace();}
nbOfThreads = Integer.parseInt(prop.getProperty("number_of_threads"));
ucpPoolSize = Integer.parseInt(prop.getProperty("ucp_pool_size"));
threadThinkTime = Integer.parseInt(prop.getProperty("thread_think_time","20"));
VERBOSE = Boolean.parseBoolean(prop.getProperty("verbose","false"));
applicationCrashOnErrors = Boolean.parseBoolean(prop.getProperty("application_crash_on_errors","true"));
fastConnectionFailover = Boolean.parseBoolean(prop.getProperty("fastConnectionFailover","false"));
validateConnectionOnBorrow = Boolean.parseBoolean(prop.getProperty("validateConnectionOnBorrow","false"));
connectionWaitTimeout = Integer.parseInt(prop.getProperty("connectionWaitTimeout","3"));
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
pds.setConnectionFactoryClassName(prop.getProperty("datasource"));
pds.setUser(prop.getProperty("username","HR"));
pds.setPassword(prop.getProperty("password","oracle_4U"));
pds.setURL(prop.getProperty("url"));
pds.setConnectionPoolName(UCP_POOL_NAME);
pds.setConnectionWaitTimeout(connectionWaitTimeout);
pds.setFastConnectionFailoverEnabled(fastConnectionFailover);
pds.setValidateConnectionOnBorrow(validateConnectionOnBorrow);
pds.setInitialPoolSize(ucpPoolSize);
pds.setMinPoolSize(ucpPoolSize);
pds.setMaxPoolSize(ucpPoolSize);
pds.setConnectionProperties(prop);
System.out.println("######################################################");
System.out.println("Connecting to " + prop.getProperty("url"));
System.out.println(" # of Threads : " + nbOfThreads);
System.out.println(" UCP pool size : " + ucpPoolSize);
System.out.println("FCF Enabled: " + pds.getFastConnectionFailoverEnabled());
System.out.println("VCoB Enabled: " + pds.getValidateConnectionOnBorrow());
System.out.println("ONS Configuration: " + pds.getONSConfiguration());
System.out.println("Enable Intensive Wload: " + cpuIntensive);
System.out.format("Thread think time : %d ms\n",
threadThinkTime);
System.out.println("######################################################");
System.out.println("");
// 使用UniversalConnectPoolManagerImpl启动连接池:
UniversalConnectionPoolManager poolManager =
UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
poolManager.createConnectionPool((UniversalConnectionPoolAdapter)pds);
System.out.println("Starting the pool now... (please wait)");
long start = System.currentTimeMillis();
poolManager.startConnectionPool(UCP_POOL_NAME);
long end = System.currentTimeMillis();
System.out.println("Pool is started in "+(end-start)+"ms");
Test u = new Test();
u.runDemo(pds);
} catch (SQLException sqlea) {
do{
sqlea.printStackTrace();
sqlea = sqlea.getNextException();
}
while(sqlea != null);
}
catch (Exception ea) {
System.out.println("Error during execution: " + ea);
ea.printStackTrace();
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
}
/**
* 启动Worker线程,,每一个线程并发运行一次PL/SQL存储过程:
*/
private void runDemo(PoolDataSource pds)
throws Exception {
Thread[] t = new Thread[nbOfThreads];
for (int i = 0; i < nbOfThreads; ++i) {
t[i] = new Thread(new Worker(pds));
t[i].start();
}
/* 显示线程统计信息 */
Thread stat = new PrintStatThread();
stat.start();
for (int i = 0; i < nbOfThreads; ++i) {
t[i].join();
}
needToPrintStats = false;
stat.interrupt();
//acStat.interrupt();
}
static boolean needToPrintStats = true;
static String UCP_POOL_NAME="actest";
}