1 28 29 package com.caucho.sql.spy; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import java.sql.*; 35 import java.util.Map ; 36 import java.util.logging.Logger ; 37 38 41 public class SpyConnection implements java.sql.Connection { 42 protected final static Logger log = Log.open(SpyConnection.class); 43 protected final static Logger logXA = 44 Logger.getLogger(SpyConnection.class.getName() + ".XA"); 45 protected final static L10N L = new L10N(SpyConnection.class); 46 47 private int _id; 48 private int _stmtCount; 49 50 private Connection _conn; 52 53 56 public SpyConnection(Connection conn, int id) 57 { 58 _conn = conn; 59 _id = id; 60 } 61 62 65 public Connection getConnection() 66 { 67 return _conn; 68 } 69 70 75 public String getCatalog() 76 throws SQLException 77 { 78 try { 79 String catalog = _conn.getCatalog(); 80 81 log.info(_id + ":getCatalog() -> " + catalog); 82 83 return catalog; 84 } catch (SQLException e) { 85 log.info(_id + ":exn-getCatalog(" + e + ")"); 86 87 throw e; 88 } 89 } 90 91 94 public void setCatalog(String catalog) 95 throws SQLException 96 { 97 try { 98 log.info(_id + ":setCatalog(" + catalog + ")"); 99 100 _conn.setCatalog(catalog); 101 } catch (SQLException e) { 102 log.info(_id + ":exn-setCatalog(" + e + ")"); 103 throw e; 104 } 105 } 106 107 110 public DatabaseMetaData getMetaData() 111 throws SQLException 112 { 113 try { 114 DatabaseMetaData metaData = _conn.getMetaData(); 115 116 log.info(_id + ":getMetaData() -> " + metaData); 117 118 return metaData; 119 } catch (SQLException e) { 120 log.info(_id + ":exn-getMetaData(" + e + ")"); 121 throw e; 122 } 123 } 124 125 128 public Map getTypeMap() 129 throws SQLException 130 { 131 try { 132 Map map = _conn.getTypeMap(); 133 134 log.info(_id + ":getTypeMap() -> " + map); 135 136 return map; 137 } catch (SQLException e) { 138 log.info(_id + ":exn-getTypeMap(" + e + ")"); 139 throw e; 140 } 141 } 142 143 146 public void setTypeMap(Map <String ,Class <?>> map) 147 throws SQLException 148 { 149 try { 150 log.info(_id + ":setTypeMap(" + map + ")"); 151 152 _conn.setTypeMap(map); 153 } catch (SQLException e) { 154 log.info(_id + ":exn-setTypeMap(" + e + ")"); 155 156 throw e; 157 } 158 } 159 160 163 public String nativeSQL(String sql) 164 throws SQLException 165 { 166 try { 167 String nativeSQL = _conn.nativeSQL(sql); 168 169 log.info(_id + ":nativeSQL() -> " + nativeSQL); 170 171 return nativeSQL; 172 } catch (SQLException e) { 173 log.info(_id + ":exn-nativeSQL(" + e + ")"); 174 175 throw e; 176 } 177 } 178 179 public int getTransactionIsolation() 180 throws SQLException 181 { 182 try { 183 int isolation = _conn.getTransactionIsolation(); 184 185 log.info(_id + ":getTransactionIsolation() -> " + isolation); 186 187 return isolation; 188 } catch (SQLException e) { 189 log.info(_id + ":exn-getTransactionIsolation(" + e + ")"); 190 191 throw e; 192 } 193 } 194 195 public void setTransactionIsolation(int isolation) 196 throws SQLException 197 { 198 try { 199 log.info(_id + ":setTransactionIsolation(" + isolation + ")"); 200 201 _conn.setTransactionIsolation(isolation); 202 } catch (SQLException e) { 203 log.info(_id + ":exn-setTransactionIsolation(" + e + ")"); 204 205 throw e; 206 } 207 } 208 209 public SQLWarning getWarnings() 210 throws SQLException 211 { 212 try { 213 SQLWarning warning = _conn.getWarnings(); 214 215 log.info(_id + ":getWarnings() -> " + warning); 216 217 return warning; 218 } catch (SQLException e) { 219 log.info(_id + ":exn-getWarnings(" + e + ")"); 220 221 throw e; 222 } 223 } 224 225 public void clearWarnings() 226 throws SQLException 227 { 228 try { 229 log.info(_id + ":clearWarnings()"); 230 231 _conn.clearWarnings(); 232 } catch (SQLException e) { 233 log.info(_id + ":exn-clearWarnings(" + e + ")"); 234 235 throw e; 236 } 237 } 238 239 public void setReadOnly(boolean readOnly) 240 throws SQLException 241 { 242 try { 243 log.info(_id + ":setReadOnly(" + readOnly + ")"); 244 245 _conn.setReadOnly(readOnly); 246 } catch (SQLException e) { 247 log.info(_id + ":exn-setReadOnly(" + e + ")"); 248 249 throw e; 250 } 251 } 252 253 public boolean isReadOnly() 254 throws SQLException 255 { 256 try { 257 boolean isReadOnly = _conn.isReadOnly(); 258 259 log.info(_id + "isReadOnly() -> " + isReadOnly); 260 261 return isReadOnly; 262 263 } catch (SQLException e) { 264 log.info(_id + ":exn-isReadOnly(" + e + ")"); 265 266 throw e; 267 } 268 } 269 270 277 public Statement createStatement() 278 throws SQLException 279 { 280 try { 281 String stmtId = _id + "." + _stmtCount++; 282 283 log.info(stmtId + ":createStatement()"); 284 285 Statement stmt; 286 287 stmt = _conn.createStatement(); 288 289 return new SpyStatement(stmtId, this, stmt); 290 } catch (SQLException e) { 291 log.info(_id + ":exn-createStatement(" + e + ")"); 292 throw e; 293 } 294 } 295 296 303 public Statement createStatement(int resultSetType, int resultSetConcurrency) 304 throws SQLException 305 { 306 try { 307 String stmtId = _id + "." + _stmtCount++; 308 309 log.info(stmtId + ":createStatement(type=" + resultSetType + 310 ",concurrency=" + resultSetConcurrency + ")"); 311 312 Statement stmt; 313 314 stmt = _conn.createStatement(resultSetType, resultSetConcurrency); 315 316 return new SpyStatement(stmtId, this, stmt); 317 } catch (SQLException e) { 318 log.info(_id + ":exn-createStatement(" + e + ")"); 319 throw e; 320 } 321 } 322 323 public Statement createStatement(int resultSetType, 324 int resultSetConcurrency, 325 int resultSetHoldability) 326 throws SQLException 327 { 328 try { 329 String stmtId = _id + "." + _stmtCount++; 330 331 log.info(stmtId + ":createStatement(type=" + resultSetType + 332 ",concurrency=" + resultSetConcurrency + 333 ",holdability=" + resultSetHoldability + ")"); 334 335 Statement stmt; 336 337 stmt = _conn.createStatement(resultSetType, 338 resultSetConcurrency, 339 resultSetHoldability); 340 341 return new SpyStatement(stmtId, this, stmt); 342 } catch (SQLException e) { 343 log.info(_id + ":exn-createStatement(" + e + ")"); 344 throw e; 345 } 346 } 347 348 349 public PreparedStatement prepareStatement(String sql) 350 throws SQLException 351 { 352 try { 353 String stmtId = _id + "." + _stmtCount++; 354 log.info(stmtId + ":prepareStatement(" + sql + ")"); 355 356 PreparedStatement stmt; 357 358 stmt = _conn.prepareStatement(sql); 359 360 return new SpyPreparedStatement(stmtId, this, stmt, sql); 361 } catch (SQLException e) { 362 log.info(_id + ":exn-prepareStatement(" + e + ")"); 363 364 throw e; 365 } 366 } 367 368 public PreparedStatement prepareStatement(String sql, 369 int resultSetType) 370 throws SQLException 371 { 372 try { 373 String stmtId = _id + "." + _stmtCount++; 374 log.info(stmtId + ":prepareStatement(" + sql + ",type=" + resultSetType + ")"); 375 376 PreparedStatement stmt; 377 378 stmt = _conn.prepareStatement(sql, resultSetType); 379 380 return new SpyPreparedStatement(stmtId, this, stmt, sql); 381 } catch (SQLException e) { 382 log.info(_id + ":exn-prepareStatement(" + e + ")"); 383 384 throw e; 385 } 386 } 387 388 public PreparedStatement prepareStatement(String sql, int resultSetType, 389 int resultSetConcurrency) 390 throws SQLException 391 { 392 try { 393 String stmtId = _id + "." + _stmtCount++; 394 log.info(stmtId + ":prepareStatement(" + sql + ",type=" + resultSetType + 395 ",concurrency=" + resultSetConcurrency + ")"); 396 397 PreparedStatement stmt; 398 399 stmt = _conn.prepareStatement(sql, resultSetType, resultSetConcurrency); 400 401 return new SpyPreparedStatement(stmtId, this, stmt, sql); 402 } catch (SQLException e) { 403 log.info(_id + ":exn-prepareStatement(" + e + ")"); 404 405 throw e; 406 } 407 } 408 409 public PreparedStatement prepareStatement(String sql, 410 int resultSetType, 411 int resultSetConcurrency, 412 int resultSetHoldability) 413 throws SQLException 414 { 415 return null; 416 } 417 418 public PreparedStatement prepareStatement(String sql, 419 int []columnIndexes) 420 throws SQLException 421 { 422 return null; 423 } 424 425 public PreparedStatement prepareStatement(String sql, 426 String []columnNames) 427 throws SQLException 428 { 429 return null; 430 } 431 432 public CallableStatement prepareCall(String sql) 433 throws SQLException 434 { 435 try { 436 String stmtId = _id + "." + _stmtCount++; 437 log.info(stmtId + ":prepareCall(" + sql + ")"); 438 439 CallableStatement stmt; 440 441 stmt = _conn.prepareCall(sql); 442 443 return stmt; 444 } catch (SQLException e) { 445 log.info(_id + ":exn-prepareCall(" + e + ")"); 446 447 throw e; 448 } 449 } 450 451 public CallableStatement prepareCall(String sql, int resultSetType, 452 int resultSetConcurrency) 453 throws SQLException 454 { 455 try { 456 String stmtId = _id + "." + _stmtCount++; 457 log.info(stmtId + ":prepareCall(" + sql + ",type=" + resultSetType + 458 ",concurrency=" + resultSetConcurrency + ")"); 459 460 CallableStatement stmt; 461 462 stmt = _conn.prepareCall(sql); 463 464 return stmt; 465 } catch (SQLException e) { 466 log.info(_id + ":exn-prepareCall(" + e + ")"); 467 468 throw e; 469 } 470 } 471 472 public CallableStatement prepareCall(String sql, 473 int resultSetType, 474 int resultSetConcurrency, 475 int resultSetHoldability) 476 throws SQLException 477 { 478 return null; 479 } 480 481 public boolean getAutoCommit() 482 throws SQLException 483 { 484 try { 485 boolean autoCommit = _conn.getAutoCommit(); 486 487 log.info(_id + ":getAutoCommit() -> " + autoCommit); 488 489 return autoCommit; 490 } catch (SQLException e) { 491 log.info(_id + ":exn-getAutoCommit(" + e + ")"); 492 493 throw e; 494 } 495 } 496 497 public void setAutoCommit(boolean autoCommit) 498 throws SQLException 499 { 500 try { 501 logXA.info(_id + ":setAutoCommit(" + autoCommit + ")"); 502 503 _conn.setAutoCommit(autoCommit); 504 } catch (SQLException e) { 505 logXA.info(_id + ":exn-setAutoCommit(" + e + ")"); 506 507 throw e; 508 } 509 } 510 511 public void commit() 512 throws SQLException 513 { 514 try { 515 logXA.info(_id + ":commit()"); 516 517 _conn.commit(); 518 } catch (SQLException e) { 519 logXA.info(_id + ":exn-commit(" + e + ")"); 520 521 throw e; 522 } 523 } 524 525 public void rollback() 526 throws SQLException 527 { 528 try { 529 logXA.info(_id + ":rollback()"); 530 531 _conn.rollback(); 532 } catch (SQLException e) { 533 logXA.info(_id + ":exn-rollback(" + e + ")"); 534 535 throw e; 536 } 537 } 538 539 542 public boolean isClosed() 543 throws SQLException 544 { 545 try { 546 boolean isClosed = _conn.isClosed(); 547 548 log.info(_id + ":isClosed() -> " + isClosed); 549 550 return isClosed; 551 } catch (SQLException e) { 552 log.info(_id + ":exn-isClosed(" + e + ")"); 553 554 throw e; 555 } 556 } 557 558 562 public void close() throws SQLException 563 { 564 log.info(_id + ":close()"); 565 566 try { 567 _conn.close(); 568 } catch (SQLException e) { 569 log.info(_id + ":exn-close(" + e + ")"); 570 571 throw e; 572 } 573 } 574 575 public void setHoldability(int hold) 576 throws SQLException 577 { 578 _conn.setHoldability(hold); 579 } 580 581 public int getHoldability() 582 throws SQLException 583 { 584 return _conn.getHoldability(); 585 } 586 587 public Savepoint setSavepoint() 588 throws SQLException 589 { 590 return _conn.setSavepoint(); 591 } 592 593 public Savepoint setSavepoint(String name) 594 throws SQLException 595 { 596 return _conn.setSavepoint(name); 597 } 598 599 public void releaseSavepoint(Savepoint savepoint) 600 throws SQLException 601 { 602 _conn.releaseSavepoint(savepoint); 603 } 604 605 public void rollback(Savepoint savepoint) 606 throws SQLException 607 { 608 _conn.rollback(savepoint); 609 } 610 611 public String toString() 612 { 613 return "SpyConnection[id=" + _id + ",conn=" + _conn + "]"; 614 } 615 } 616 | Popular Tags |