1 14 package org.compiere.dbPort; 15 16 import java.sql.*; 17 import javax.sql.*; 18 19 import oracle.jdbc.OracleConnection; 20 import oracle.jdbc.pool.OracleDataSource; 21 import oracle.jdbc.pool.OracleConnectionCacheImpl; 22 23 25 31 public class JdbcTest extends Thread 32 { 33 81 82 private static final int NUM_OF_THREADS = 10; 84 85 private static final String DRIVER = 86 "thin"; 88 89 private static final String CONNECTION = 90 "jdbc:oracle:thin:@dev:1521:dev1"; 93 94 95 private static final String UID = "compiere"; 96 private static final String PWD = "compiere"; 97 private static final String STATEMENT = "SELECT * FROM AD_Column"; 98 private static final boolean WITH_OUTPUT = false; 99 100 private static boolean s_do_yield = true; 101 102 private static Connection s_sconn = null; 103 private static Connection[] s_conn = null; 104 private static OracleDataSource s_ds = null; 105 private static OracleConnectionCacheImpl s_cc = null; 106 107 private static int s_fetchSize = 10; 108 109 private static int s_cType = 0; 111 private static final String [] C_INFO = { 112 "Shared Connection ", 113 "Multiple Connections ", 114 "Multiple PreCreated ", 115 "Data Source ", 116 "Connection Cache "}; 117 private static final int C_SHARED = 0; 118 private static final int C_MULTIPLE = 1; 119 private static final int C_PRECREATED = 2; 120 private static final int C_DATASOURCE = 3; 121 private static final int C_CACHE = 4; 122 123 private static int s_rType = 0; 125 private static final String [] R_INFO = { 126 "ResultSet ", 127 "Cached RowSet ", 128 "JDBC RowSet "}; 129 private static final int R_RESULTSET = 0; 130 private static final int R_CACHED_ROWSET = 1; 131 private static final int R_JDBC_ROWSET = 2; 132 133 134 135 139 public static void main (String args []) 140 { 141 try 142 { 143 144 DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); 145 146 s_ds = new OracleDataSource(); 147 s_ds.setDriverType(DRIVER); 148 s_ds.setServerName("dev"); 149 s_ds.setNetworkProtocol("tcp"); 150 s_ds.setDatabaseName("dev1"); 151 s_ds.setPortNumber(1521); 152 s_ds.setUser("compiere"); 153 s_ds.setPassword("compiere"); 154 s_cc = new OracleConnectionCacheImpl(); 156 s_cc.setDriverType(DRIVER); 157 s_cc.setServerName("dev"); 158 s_cc.setNetworkProtocol("tcp"); 159 s_cc.setDatabaseName("dev1"); 160 s_cc.setPortNumber(1521); 161 s_cc.setUser("compiere"); 162 s_cc.setPassword("compiere"); 163 s_cc.setMaxLimit(NUM_OF_THREADS/4); 164 s_cc.setCacheScheme(OracleConnectionCacheImpl.FIXED_WAIT_SCHEME); 165 167 s_fetchSize = 10; 168 s_cType = C_MULTIPLE; 169 statementTiming(); 170 statementTiming(); 171 statementTiming(); 172 s_cType = C_DATASOURCE; 173 statementTiming(); 174 statementTiming(); 175 statementTiming(); 176 s_cType = C_CACHE; 177 statementTiming(); 178 statementTiming(); 179 statementTiming(); 180 s_fetchSize = 20; 181 s_cType = C_MULTIPLE; 182 statementTiming(); 183 statementTiming(); 184 statementTiming(); 185 s_cType = C_DATASOURCE; 186 statementTiming(); 187 statementTiming(); 188 statementTiming(); 189 s_cType = C_CACHE; 190 statementTiming(); 191 statementTiming(); 192 statementTiming(); 193 s_fetchSize = 10; 205 s_cType = C_SHARED; 207 s_do_yield = false; 208 runTest(); 209 runTest(); 210 s_do_yield = true; 211 runTest(); 212 runTest(); 213 s_cType = C_MULTIPLE; 215 s_do_yield = false; 216 runTest(); 217 runTest(); 218 s_do_yield = true; 219 runTest(); 220 runTest(); 221 s_cType = C_PRECREATED; 223 s_do_yield = false; 224 runTest(); 225 runTest(); 226 s_do_yield = true; 227 runTest(); 228 runTest(); 229 s_cType = C_DATASOURCE; 231 s_do_yield = false; 232 runTest(); 233 runTest(); 234 s_do_yield = true; 235 runTest(); 236 runTest(); 237 s_cType = C_CACHE; 239 s_do_yield = false; 240 runTest(); 241 runTest(); 242 s_do_yield = true; 243 runTest(); 244 runTest(); 245 247 } 248 catch (Exception e) 249 { 250 e.printStackTrace(); 251 } 252 } 254 255 256 260 static void runTest() throws Exception 261 { 262 Thread [] threadList = new Thread [NUM_OF_THREADS]; 264 s_conn = new Connection[NUM_OF_THREADS]; 265 266 if (s_cType == C_SHARED) 267 s_sconn = DriverManager.getConnection (CONNECTION, UID, PWD); 268 for (int i = 0; i < NUM_OF_THREADS; i++) 271 { 272 if (s_cType == C_PRECREATED) 273 s_conn[i] = DriverManager.getConnection (CONNECTION, UID, PWD); 274 threadList[i] = new JdbcTest(i); 276 threadList[i].start(); 277 } 278 long start = System.currentTimeMillis(); 280 setGreenLight (); 281 for (int i = 0; i < NUM_OF_THREADS; i++) 283 threadList[i].join(); 284 if (s_sconn != null) 286 s_sconn.close(); 287 s_sconn = null; 288 for (int i = 0; i < NUM_OF_THREADS; i++) 289 { 290 if (s_conn[i] != null) 291 s_conn[i].close(); 292 s_conn[i] = null; 293 } 294 long result = System.currentTimeMillis() - start; 295 System.out.print (C_INFO[s_cType] 296 + "Threads=" + NUM_OF_THREADS 297 + " \tYield=" + s_do_yield 298 + " \tms= " + result 299 + " \teach= " + (result/NUM_OF_THREADS)); 300 if (s_cType == C_CACHE) 301 System.out.print (" \tCacheSize=" + s_cc.getCacheSize() + ", Active=" + s_cc.getActiveSize()); 302 System.out.println(); 303 } 305 306 309 private static void statementTiming() 310 { 311 try 312 { 313 long startConnection = System.currentTimeMillis(); 314 Connection conn = null; 315 if (s_cType == C_MULTIPLE) 316 conn = DriverManager.getConnection (CONNECTION, UID, PWD); 317 if (s_cType == C_DATASOURCE) 318 conn = s_ds.getConnection(); 319 if (s_cType == C_CACHE) 320 conn = s_cc.getConnection(); 321 322 long startStatement = System.currentTimeMillis(); 323 Statement stmt = conn.createStatement (); 324 stmt.setFetchSize(s_fetchSize); 325 326 long startQuery = System.currentTimeMillis(); 327 ResultSet rs = stmt.executeQuery (STATEMENT); 328 329 int i = 0; 330 long startRetrieve = System.currentTimeMillis(); 331 while (rs.next()) 332 { 333 rs.getString(1); 334 i++; 335 } 336 long endRetrieve = System.currentTimeMillis(); 337 339 rs.close(); 340 rs = null; 341 long endQuery = System.currentTimeMillis(); 342 343 stmt.close(); 344 stmt = null; 345 long endStatement = System.currentTimeMillis(); 346 347 conn.close(); 348 conn = null; 349 long endConnection = System.currentTimeMillis(); 350 351 System.out.println(C_INFO[s_cType] 353 + "Fetch=" + s_fetchSize 354 + " \tConn=" + (startStatement - startConnection) 355 + " \tStmt=" + (startQuery - startStatement) 356 + " \tQuery=" + (startRetrieve - startQuery) 357 + " \tRetrieve=" + (endRetrieve - startRetrieve) 358 + " \tClRs=" + (endQuery - endRetrieve) 359 + " \tClStmt=" + (endStatement - endQuery) 360 + " \tClConn=" + (endConnection - endStatement) 361 + " \t- Total=" + (endConnection - startConnection) 362 + " \tStmt=" + (endStatement - startStatement) 363 + " \tQuery=" + (endQuery - startQuery)); 364 } 365 catch (SQLException e) 366 { 367 e.printStackTrace(); 368 } 369 } 371 374 private static void rowSetTiming() 375 { 376 try 377 { 378 long startConnection = System.currentTimeMillis(); 379 RowSet rowset = null; 380 386 rowset.setUrl (CONNECTION); 387 rowset.setUsername (UID); 388 rowset.setPassword (PWD); 389 rowset.setFetchSize(s_fetchSize); 390 391 long startStatement = System.currentTimeMillis(); 392 rowset.setCommand (STATEMENT); 393 394 long startQuery = System.currentTimeMillis(); 395 rowset.execute (); 396 397 long startRetrieve = System.currentTimeMillis(); 398 while (rowset.next ()) 399 { 400 } 401 long endRetrieve = System.currentTimeMillis(); 402 long endQuery = System.currentTimeMillis(); 403 404 rowset.close(); 405 long endStatement = System.currentTimeMillis(); 406 long endConnection = System.currentTimeMillis(); 407 System.out.println(R_INFO[s_rType] 409 + "Fetch=" + s_fetchSize 410 + " \tConn=" + (startStatement - startConnection) 411 + " \tStmt=" + (startQuery - startStatement) 412 + " \tQuery=" + (startRetrieve - startQuery) 413 + " \tRetrieve=" + (endRetrieve - startRetrieve) 414 + " \tClRs=" + (endQuery - endRetrieve) 415 + " \tClStmt=" + (endStatement - endQuery) 416 + " \tClConn=" + (endConnection - endStatement) 417 + " \t- Total=" + (endConnection - startConnection) 418 + " \tStmt=" + (endStatement - startStatement) 419 + " \tQuery=" + (endQuery - startQuery)); 420 } 421 catch (SQLException e) 422 { 423 e.printStackTrace(); 424 } 425 } 427 428 429 433 public JdbcTest(int id) 434 { 435 super(); 436 m_myId = id; 437 } 439 private int m_myId = 0; 440 441 444 public void run() 445 { 446 ResultSet rs = null; 447 Statement stmt = null; 448 449 try 450 { 451 while (!getGreenLight()) 452 yield(); 453 if (WITH_OUTPUT) 454 System.out.println("Thread " + m_myId + " started"); 455 456 if (s_cType == C_SHARED) 458 stmt = s_sconn.createStatement (); 459 else if (s_cType == C_MULTIPLE) 460 { 461 s_conn[m_myId] = DriverManager.getConnection (CONNECTION, UID, PWD); 462 stmt = s_conn[m_myId].createStatement (); 463 } 464 else if (s_cType == C_PRECREATED) 465 { 466 stmt = s_conn[m_myId].createStatement (); 467 } 468 else if (s_cType == C_DATASOURCE) 469 { 470 s_conn[m_myId] = s_ds.getConnection(); 471 stmt = s_conn[m_myId].createStatement (); 472 } 473 else if (s_cType == C_CACHE) 474 { 475 s_conn[m_myId] = s_cc.getConnection(); 476 stmt = s_conn[m_myId].createStatement (); 477 } 478 stmt.setFetchSize(s_fetchSize); 479 480 rs = stmt.executeQuery (STATEMENT); 482 483 while (rs.next()) 485 { 486 if (s_do_yield) 487 yield(); } 489 490 rs.close(); 492 rs = null; 493 494 stmt.close(); 496 stmt = null; 497 498 if (s_cType == C_SHARED || s_cType == C_PRECREATED) 500 ; 501 else 502 { 503 s_conn[m_myId].close(); 504 s_conn[m_myId] = null; 505 } 506 } 507 catch (Exception e) 508 { 509 System.out.println("Thread " + m_myId + " got Exception: " + e); 510 e.printStackTrace(); 511 return; 512 } 513 if (WITH_OUTPUT) 514 System.out.println("Thread " + m_myId + " finished"); 515 } 516 517 518 519 static boolean greenLight = false; 520 static synchronized void setGreenLight () { greenLight = true; } 521 synchronized boolean getGreenLight () { return greenLight; } 522 523 } | Popular Tags |