📄 jdbctest.java
字号:
Thread[] threadList = new Thread[NUM_OF_THREADS];
s_conn = new Connection[NUM_OF_THREADS];
if (s_cType == C_SHARED)
s_sconn = DriverManager.getConnection (CONNECTION, UID, PWD);
//
// spawn threads
for (int i = 0; i < NUM_OF_THREADS; i++)
{
if (s_cType == C_PRECREATED)
s_conn[i] = DriverManager.getConnection (CONNECTION, UID, PWD);
//
threadList[i] = new JdbcTest(i);
threadList[i].start();
}
// Start everyone at the same time
long start = System.currentTimeMillis();
setGreenLight ();
// wait for all threads to end
for (int i = 0; i < NUM_OF_THREADS; i++)
threadList[i].join();
//
if (s_sconn != null)
s_sconn.close();
s_sconn = null;
for (int i = 0; i < NUM_OF_THREADS; i++)
{
if (s_conn[i] != null)
s_conn[i].close();
s_conn[i] = null;
}
long result = System.currentTimeMillis() - start;
System.out.print (C_INFO[s_cType]
+ "Threads=" + NUM_OF_THREADS
+ " \tYield=" + s_do_yield
+ " \tms= " + result
+ " \teach= " + (result/NUM_OF_THREADS));
if (s_cType == C_CACHE)
System.out.print (" \tCacheSize=" + s_cc.getCacheSize() + ", Active=" + s_cc.getActiveSize());
System.out.println();
} // runTest
/**
* Statement Timing
*/
private static void statementTiming()
{
try
{
long startConnection = System.currentTimeMillis();
Connection conn = null;
if (s_cType == C_MULTIPLE)
conn = DriverManager.getConnection (CONNECTION, UID, PWD);
if (s_cType == C_DATASOURCE)
conn = s_ds.getConnection();
if (s_cType == C_CACHE)
conn = s_cc.getConnection();
long startStatement = System.currentTimeMillis();
Statement stmt = conn.createStatement ();
stmt.setFetchSize(s_fetchSize);
long startQuery = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery (STATEMENT);
int i = 0;
long startRetrieve = System.currentTimeMillis();
while (rs.next())
{
rs.getString(1);
i++;
}
long endRetrieve = System.currentTimeMillis();
// System.out.println(i);
rs.close();
rs = null;
long endQuery = System.currentTimeMillis();
stmt.close();
stmt = null;
long endStatement = System.currentTimeMillis();
conn.close();
conn = null;
long endConnection = System.currentTimeMillis();
//
System.out.println(C_INFO[s_cType]
+ "Fetch=" + s_fetchSize
+ " \tConn=" + (startStatement - startConnection)
+ " \tStmt=" + (startQuery - startStatement)
+ " \tQuery=" + (startRetrieve - startQuery)
+ " \tRetrieve=" + (endRetrieve - startRetrieve)
+ " \tClRs=" + (endQuery - endRetrieve)
+ " \tClStmt=" + (endStatement - endQuery)
+ " \tClConn=" + (endConnection - endStatement)
+ " \t- Total=" + (endConnection - startConnection)
+ " \tStmt=" + (endStatement - startStatement)
+ " \tQuery=" + (endQuery - startQuery));
}
catch (SQLException e)
{
e.printStackTrace();
}
} // statementTiming
/**
* Row Set Timing
*/
private static void rowSetTiming()
{
try
{
long startConnection = System.currentTimeMillis();
RowSet rowset = null;
/**
if (s_rType == R_JDBC_ROWSET)
rowset = new OracleJDBCRowSet ();
else if (s_rType == R_CACHED_ROWSET)
rowset = new OracleCachedRowSet();
**/
rowset.setUrl (CONNECTION);
rowset.setUsername (UID);
rowset.setPassword (PWD);
rowset.setFetchSize(s_fetchSize);
long startStatement = System.currentTimeMillis();
rowset.setCommand (STATEMENT);
long startQuery = System.currentTimeMillis();
rowset.execute ();
long startRetrieve = System.currentTimeMillis();
while (rowset.next ())
{
}
long endRetrieve = System.currentTimeMillis();
long endQuery = System.currentTimeMillis();
rowset.close();
long endStatement = System.currentTimeMillis();
long endConnection = System.currentTimeMillis();
//
System.out.println(R_INFO[s_rType]
+ "Fetch=" + s_fetchSize
+ " \tConn=" + (startStatement - startConnection)
+ " \tStmt=" + (startQuery - startStatement)
+ " \tQuery=" + (startRetrieve - startQuery)
+ " \tRetrieve=" + (endRetrieve - startRetrieve)
+ " \tClRs=" + (endQuery - endRetrieve)
+ " \tClStmt=" + (endStatement - endQuery)
+ " \tClConn=" + (endConnection - endStatement)
+ " \t- Total=" + (endConnection - startConnection)
+ " \tStmt=" + (endStatement - startStatement)
+ " \tQuery=" + (endQuery - startQuery));
}
catch (SQLException e)
{
e.printStackTrace();
}
} // rowSetTiming
/*************************************************************************/
/**
* JDBC Test
* @param id Thread ID
*/
public JdbcTest(int id)
{
super();
m_myId = id;
} // JdbcTest
private int m_myId = 0;
/**
* Async Worker
*/
public void run()
{
ResultSet rs = null;
Statement stmt = null;
try
{
while (!getGreenLight())
yield();
if (WITH_OUTPUT)
System.out.println("Thread " + m_myId + " started");
// Get the connection & statement
if (s_cType == C_SHARED)
stmt = s_sconn.createStatement ();
else if (s_cType == C_MULTIPLE)
{
s_conn[m_myId] = DriverManager.getConnection (CONNECTION, UID, PWD);
stmt = s_conn[m_myId].createStatement ();
}
else if (s_cType == C_PRECREATED)
{
stmt = s_conn[m_myId].createStatement ();
}
else if (s_cType == C_DATASOURCE)
{
s_conn[m_myId] = s_ds.getConnection();
stmt = s_conn[m_myId].createStatement ();
}
else if (s_cType == C_CACHE)
{
s_conn[m_myId] = s_cc.getConnection();
stmt = s_conn[m_myId].createStatement ();
}
stmt.setFetchSize(s_fetchSize);
// Execute the Query
rs = stmt.executeQuery (STATEMENT);
// Loop through the results
while (rs.next())
{
if (s_do_yield)
yield(); // Yield To other threads
}
// Close all the resources
rs.close();
rs = null;
// Close the statement
stmt.close();
stmt = null;
// Close the local connection
if (s_cType == C_SHARED || s_cType == C_PRECREATED)
;
else
{
s_conn[m_myId].close();
s_conn[m_myId] = null;
}
}
catch (Exception e)
{
System.out.println("Thread " + m_myId + " got Exception: " + e);
e.printStackTrace();
return;
}
if (WITH_OUTPUT)
System.out.println("Thread " + m_myId + " finished");
}
/*************************************************************************/
static boolean greenLight = false;
static synchronized void setGreenLight () { greenLight = true; }
synchronized boolean getGreenLight () { return greenLight; }
} // JdbcTest
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -