1 25 26 package org.objectweb.cjdbc.requestplayer; 27 28 import java.sql.Connection ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 33 import org.objectweb.cjdbc.common.util.Stats; 34 35 45 public class ClientThread extends Thread 46 { 47 48 private static final boolean DEBUG = false; 49 50 51 private Stats selectStats = null; 52 53 54 private Stats unknownStats = null; 55 56 57 private Stats updateStats = null; 58 59 60 private Stats insertStats = null; 61 62 63 private Stats deleteStats = null; 64 65 66 private Stats beginStats = null; 67 68 69 private Stats commitStats = null; 70 71 72 private Stats rollbackStats = null; 73 74 75 private Stats getConnectionStats = null; 76 77 78 private Stats closeStats = null; 79 80 81 private Stats getRequestStats = null; 82 83 private Connection conn = null; 84 85 private ClientEmulator father; 86 private int threadId; 87 88 89 private int connectionType; 90 91 98 public ClientThread(int threadId, ClientEmulator father, int connectionType) 99 { 100 super("ClientThread" + threadId); 101 102 selectStats = father.getSelectStats(); 104 unknownStats = father.getUnknownStats(); 105 updateStats = father.getUpdateStats(); 106 insertStats = father.getInsertStats(); 107 deleteStats = father.getDeleteStats(); 108 beginStats = father.getBeginStats(); 109 commitStats = father.getCommitStats(); 110 rollbackStats = father.getRollbackStats(); 111 getRequestStats = father.getGetRequestStats(); 112 getConnectionStats = father.getGetConnectionStats(); 113 closeStats = father.getCloseStats(); 114 115 this.father = father; 116 this.threadId = threadId; 117 this.connectionType = connectionType; 118 119 if (this.connectionType == RequestPlayerProperties.FIXED_CONNECTION) 120 { 121 conn = father.getConnection(); 123 } 124 } 125 126 129 public void run() 130 { 131 String request = null; 132 int tid = 0; 134 if (DEBUG) 135 System.out.println(threadId + ": Starting"); 136 137 while (true) 139 { 140 long startg = System.currentTimeMillis(); 141 request = father.parallelGetNextSQLRequest(tid); 142 long endg = System.currentTimeMillis(); 143 getRequestStats.incrementCount(); 144 getRequestStats.updateTime(endg - startg); 145 146 if (request == null) 147 { if (tid != 0) 150 { 151 System.out.println(threadId 152 + ": Warning! Rollbacking unterminated transaction " + tid); 153 request = "R"; 154 } 155 else 156 break; } 158 159 try 160 { 161 switch (request.charAt(0)) 162 { 163 case 'B' : 164 if (DEBUG) 166 System.out.println(threadId + ": " + request); 167 long startb = System.currentTimeMillis(); 168 if (connectionType != RequestPlayerProperties.FIXED_CONNECTION) 169 { 170 conn = getConnection(); 172 } 173 conn.setAutoCommit(false); 174 long endb = System.currentTimeMillis(); 175 beginStats.incrementCount(); 176 beginStats.updateTime(endb - startb); 177 tid = new Integer (request.substring(2)).intValue(); 178 break; 179 case 'C' : 180 if (DEBUG) 182 System.out.println(threadId + ": " + request); 183 long startc = System.currentTimeMillis(); 184 conn.commit(); 185 long endc = System.currentTimeMillis(); 186 commitStats.incrementCount(); 187 commitStats.updateTime(endc - startc); 188 tid = 0; 189 if (connectionType != RequestPlayerProperties.FIXED_CONNECTION) 190 { closeConnection(); 192 } 193 break; 194 case 'R' : 195 if (DEBUG) 197 System.out.println(threadId + ": " + request); 198 long startr = System.currentTimeMillis(); 199 conn.rollback(); 200 long endr = System.currentTimeMillis(); 201 rollbackStats.incrementCount(); 202 rollbackStats.updateTime(endr - startr); 203 tid = 0; 204 if (connectionType != RequestPlayerProperties.FIXED_CONNECTION) 205 { closeConnection(); 207 } 208 break; 209 case 'S' : 210 if (tid == 0 212 && (connectionType != RequestPlayerProperties.FIXED_CONNECTION)) 213 { 214 conn = getConnection(); 216 execReadRequest(request); 218 closeConnection(); 220 } 221 else 222 { 223 execReadRequest(request); 224 } 225 break; 226 case 'W' : 227 if (tid == 0 229 && (connectionType != RequestPlayerProperties.FIXED_CONNECTION)) 230 { 231 conn = getConnection(); 233 execWriteRequest(request); 235 closeConnection(); 237 } 238 else 239 { 240 execWriteRequest(request); 241 } 242 243 break; 244 default : 245 System.err.println(threadId + ": Error! Unsupported request " 246 + request); 247 break; 248 } 249 } 250 catch (Exception e) 251 { 252 System.err.println(threadId 253 + ": An error occured while executing SQL request (" 254 + e.getMessage() + ")"); 255 if (request.charAt(0) != 'S' && request.charAt(0) != 'W') 256 { if (tid != 0) 258 { 259 try 260 { 261 conn.rollback(); 262 } 263 catch (Exception ignore) 264 { 265 } 266 father.ignoreTid(tid); 267 tid = 0; 268 } 269 } 270 if (connectionType != RequestPlayerProperties.FIXED_CONNECTION) 271 closeConnection(); 273 } 274 } 275 276 if (connectionType == RequestPlayerProperties.FIXED_CONNECTION) 277 father.closeConnection(conn); 278 279 System.out.println(threadId + ": Ending."); 281 } 282 283 288 private void execWriteRequest(String req) 289 { 290 Statement stmt = null; 291 292 String request = req.substring(2); 293 if (DEBUG) 294 System.out.println(threadId + ": " + request.substring(0, 5)); 295 long startw = System.currentTimeMillis(); 296 try 297 { 298 stmt = conn.createStatement(); 299 stmt.setQueryTimeout(father.getTimeout()); 300 stmt.executeUpdate(request); 301 stmt.close(); 302 } 303 catch (SQLException e) 304 { 305 if ((request.charAt(0) == 'i') || (request.charAt(0) == 'I')) { 307 insertStats.incrementError(); 308 } 309 else if ((request.charAt(0) == 'u') || (request.charAt(0) == 'U')) { 311 updateStats.incrementError(); 312 } 313 else if ((request.charAt(0) == 'd') || (request.charAt(0) == 'D')) { 315 deleteStats.incrementError(); 316 } 317 else 318 { 319 unknownStats.incrementError(); 320 } 321 System.err.println(threadId + ": Failed to execute request: " + request 322 + "(" + e + ")"); 323 return; 324 } 325 long endw = System.currentTimeMillis(); 326 if ((request.charAt(0) == 'i') || (request.charAt(0) == 'I')) { 328 insertStats.incrementCount(); 329 insertStats.updateTime(endw - startw); 330 } 331 else if ((request.charAt(0) == 'u') || (request.charAt(0) == 'U')) { 333 updateStats.incrementCount(); 334 updateStats.updateTime(endw - startw); 335 } 336 else if ((request.charAt(0) == 'd') || (request.charAt(0) == 'D')) { 338 deleteStats.incrementCount(); 339 deleteStats.updateTime(endw - startw); 340 } 341 else 342 { 343 unknownStats.incrementCount(); 344 unknownStats.updateTime(endw - startw); 345 } 346 } 347 348 353 private void execReadRequest(String req) 354 { 355 Statement stmt = null; 356 ResultSet dbReply = null; String request = req.substring(2); 358 if (DEBUG) 359 System.out.println(threadId + ": " + request.substring(0, 5)); 360 long startr = System.currentTimeMillis(); 361 try 362 { 363 stmt = conn.createStatement(); 364 stmt.setQueryTimeout(father.getTimeout()); 365 dbReply = stmt.executeQuery(request); 366 if (dbReply != null) 368 dbReply.next(); stmt.close(); 370 } 371 catch (SQLException e) 372 { 373 selectStats.incrementError(); 374 System.err.println(threadId + ": Failed to execute request: " + request 375 + "(" + e + ")"); 376 } 377 long endr = System.currentTimeMillis(); 378 selectStats.incrementCount(); 379 selectStats.updateTime(endr - startr); 380 } 381 382 385 private void closeConnection() 386 { 387 long start = System.currentTimeMillis(); 388 if (connectionType == RequestPlayerProperties.STANDARD_CONNECTION) 389 { 390 father.closeConnection(conn); 391 } 392 else if (connectionType == RequestPlayerProperties.POOLING_CONNECTION) 393 { 394 father.releaseConnectionToPool(conn); 395 } 396 long end = System.currentTimeMillis(); 397 closeStats.incrementCount(); 398 closeStats.updateTime(end - start); 399 } 400 401 406 private Connection getConnection() 407 { 408 Connection c = null; 409 long start = System.currentTimeMillis(); 410 if (connectionType == RequestPlayerProperties.STANDARD_CONNECTION) 411 { 412 c = father.getConnection(); 413 } 414 else if (connectionType == RequestPlayerProperties.POOLING_CONNECTION) 415 { 416 c = father.getConnectionFromPool(); 417 } 418 long end = System.currentTimeMillis(); 419 getConnectionStats.incrementCount(); 420 getConnectionStats.updateTime(end - start); 421 return c; 422 } 423 } | Popular Tags |