1 package com.protomatter.jdbc.pool; 2 3 52 53 import java.io.*; 54 import java.sql.*; 55 import java.util.*; 56 import java.text.MessageFormat ; 57 import com.protomatter.util.*; 58 import com.protomatter.pool.*; 59 import com.protomatter.syslog.*; 60 61 68 class ConnectionWrapper 69 implements Connection, SyslogChannelAware 70 { 71 private JdbcConnectionPool pool = null; 72 private JdbcConnectionPoolConnection connection = null; 73 private boolean isClosed = false; 74 75 protected static Debug DEBUG = Debug.forPackage(ConnectionWrapper.class); 76 protected static Channel log = Channel.forPackage(ConnectionWrapper.class); 77 78 ConnectionWrapper(JdbcConnectionPoolConnection connection, JdbcConnectionPool pool) 79 { 80 super(); 81 this.connection = connection; 82 this.pool = pool; 83 } 84 85 94 public Connection getConnection() 95 { 96 if (this.connection == null) 97 return null; 98 return this.connection.getConnection(); 99 } 100 101 104 public JdbcConnectionPool getConnectionPool() 105 { 106 return this.pool; 107 } 108 109 113 public Object getSyslogChannel() 114 { 115 return this.pool.getSyslogChannel(); 116 } 117 118 122 public void invalidate() 123 { 124 if (DEBUG.debug()) 125 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod()); 126 if (this.connection != null) 127 this.connection.invalidate(); 128 } 129 130 131 149 150 public synchronized void refresh(boolean verbose) 151 throws SQLException 152 { 153 if (DEBUG.debug()) 154 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod()); 155 checkClosed(); 156 this.connection.refresh(verbose); 157 } 158 159 167 public synchronized void refresh() 168 throws SQLException 169 { 170 refresh(false); 171 } 172 173 private final void checkClosed() 174 throws SQLException 175 { 176 if (this.isClosed) 177 throw new SQLException(PoolResources.getResourceString(MessageConstants.CONNECTION_IS_CLOSED)); 178 } 179 180 185 public Statement createStatement() 186 throws SQLException 187 { 188 checkClosed(); 189 long time = System.currentTimeMillis(); 190 Statement stmt = this.connection.createStatement(); 191 time = System.currentTimeMillis() - time; 192 if (DEBUG.debug()) 193 { 194 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " took " + time + "ms"); 195 return new StatementWrapper(this, stmt); 196 } 197 return stmt; 198 } 199 200 205 public PreparedStatement prepareStatement(String sql) 206 throws SQLException 207 { 208 checkClosed(); 209 long time = System.currentTimeMillis(); 210 PreparedStatement stmt = this.connection.prepareStatement(sql); 211 time = System.currentTimeMillis() - time; 212 if (DEBUG.debug()) 213 { 214 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " SQL=\"" + sql + "\" took " + time + "ms"); 215 return new PreparedStatementWrapper(this, stmt); 216 } 217 return stmt; 218 } 219 220 225 public CallableStatement prepareCall(String sql) 226 throws SQLException 227 { 228 checkClosed(); 229 long time = System.currentTimeMillis(); 230 CallableStatement stmt = this.connection.prepareCall(sql); 231 time = System.currentTimeMillis() - time; 232 if (DEBUG.debug()) 233 { 234 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " SQL=\"" + sql + "\" took " + time + "ms"); 235 return new CallableStatementWrapper(this, stmt); 236 } 237 return stmt; 238 } 239 240 245 public String nativeSQL(String sql) 246 throws SQLException 247 { 248 checkClosed(); 249 long time = System.currentTimeMillis(); 250 String nsql = this.connection.nativeSQL(sql); 251 time = System.currentTimeMillis() - time; 252 if (DEBUG.debug()) 253 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " SQL=\"" + sql + "\" NativeSQL=\"" + nsql + "\" took " + time + "ms"); 254 return nsql; 255 } 256 257 262 public void setAutoCommit(boolean autoCommit) 263 throws SQLException 264 { 265 if (DEBUG.debug()) 266 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " AutoCommit=\"" + autoCommit + "\""); 267 checkClosed(); 268 this.connection.setAutoCommit(autoCommit); 269 } 270 271 276 public boolean getAutoCommit() 277 throws SQLException 278 { 279 checkClosed(); 280 return this.connection.getAutoCommit(); 281 } 282 283 288 public void commit() 289 throws SQLException 290 { 291 if (DEBUG.debug()) 292 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod()); 293 checkClosed(); 294 this.connection.commit(); 295 } 296 297 302 public void rollback() 303 throws SQLException 304 { 305 if (DEBUG.debug()) 306 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod()); 307 checkClosed(); 308 this.connection.rollback(); 309 } 310 311 325 public void close() 326 throws SQLException 327 { 328 if (DEBUG.debug()) 329 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod()); 330 if (this.isClosed) 331 return; 332 333 this.connection.close(); 334 this.isClosed = true; 335 this.connection = null; 336 } 337 338 343 public boolean isClosed() 344 throws SQLException 345 { 346 return this.isClosed; 349 } 350 351 356 public DatabaseMetaData getMetaData() 357 throws SQLException 358 { 359 checkClosed(); 360 return this.connection.getMetaData(); 361 } 362 363 368 public void setReadOnly(boolean readOnly) 369 throws SQLException 370 { 371 if (DEBUG.debug()) 372 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " ReadOnly=\"" + readOnly + "\""); 373 checkClosed(); 374 this.connection.setReadOnly(readOnly); 375 } 376 377 382 public boolean isReadOnly() 383 throws SQLException 384 { 385 checkClosed(); 386 return this.connection.isReadOnly(); 387 } 388 389 394 public void setCatalog(String catalog) 395 throws SQLException 396 { 397 if (DEBUG.debug()) 398 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " Catalog=\"" + catalog + "\""); 399 checkClosed(); 400 this.connection.setCatalog(catalog); 401 } 402 403 408 public String getCatalog() 409 throws SQLException 410 { 411 checkClosed(); 412 return this.connection.getCatalog(); 413 } 414 415 420 public void setTransactionIsolation(int level) 421 throws SQLException 422 { 423 if (DEBUG.debug()) 424 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " IsolationLevel=\"" + level + "\""); 425 checkClosed(); 426 this.connection.setTransactionIsolation(level); 427 } 428 429 434 public int getTransactionIsolation() 435 throws SQLException 436 { 437 checkClosed(); 438 return this.connection.getTransactionIsolation(); 439 } 440 441 446 public SQLWarning getWarnings() 447 throws SQLException 448 { 449 checkClosed(); 450 return this.connection.getWarnings(); 451 } 452 453 458 public void clearWarnings() 459 throws SQLException 460 { 461 if (DEBUG.debug()) 462 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod()); 463 checkClosed(); 464 this.connection.clearWarnings(); 465 } 466 467 472 public Statement createStatement(int resultSetType, int resultSetConcurrency) 473 throws SQLException 474 { 475 checkClosed(); 476 long time = System.currentTimeMillis(); 477 Statement stmt = this.connection.createStatement(resultSetType, resultSetConcurrency); 478 time = System.currentTimeMillis() - time; 479 if (DEBUG.debug()) 480 { 481 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " Type=\"" + resultSetType + "\" Concurrency=\"" + resultSetConcurrency + "\" took " + time + "ms"); 482 return new StatementWrapper(this, stmt); 483 } 484 return stmt; 485 } 486 487 492 public Map getTypeMap() 493 throws SQLException 494 { 495 checkClosed(); 496 return this.connection.getTypeMap(); 497 } 498 499 504 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) 505 throws SQLException 506 { 507 checkClosed(); 508 509 long time = System.currentTimeMillis(); 510 CallableStatement stmt = this.connection.prepareCall(sql, resultSetType, resultSetConcurrency); 511 time = System.currentTimeMillis() - time; 512 if (DEBUG.debug()) 513 { 514 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " SQL=\"" + sql + "\" Type=\"" + resultSetType + "\" Concurrency=\"" + resultSetConcurrency + "\" took " + time + "ms"); 515 return new CallableStatementWrapper(this, stmt); 516 } 517 return stmt; 518 } 519 520 525 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) 526 throws SQLException 527 { 528 checkClosed(); 529 530 long time = System.currentTimeMillis(); 531 PreparedStatement stmt = this.connection.prepareStatement(sql, resultSetType, resultSetConcurrency); 532 time = System.currentTimeMillis() - time; 533 if (DEBUG.debug()) 534 { 535 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " SQL=\"" + sql + "\" Type=\"" + resultSetType + "\" Concurrency=\"" + resultSetConcurrency + "\" took " + time + "ms"); 536 return new PreparedStatementWrapper(this, stmt); 537 } 538 return stmt; 539 } 540 541 546 public void setTypeMap(Map typeMap) 547 throws SQLException 548 { 549 if (DEBUG.debug()) 550 log.debug(this, StackTraceUtil.whereAmI().getShortClassAndMethod() + " TypeMap=\"" + typeMap + "\""); 551 checkClosed(); 552 this.connection.setTypeMap(typeMap); 553 } 554 } 555 | Popular Tags |