1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.CallableStatement ; 20 import java.sql.Connection ; 21 import java.sql.DatabaseMetaData ; 22 import java.sql.PreparedStatement ; 23 import java.sql.SQLException ; 24 import java.sql.SQLWarning ; 25 import java.sql.Statement ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 50 public class DelegatingConnection extends AbandonedTrace 51 implements Connection { 52 53 protected Connection _conn = null; 54 55 protected boolean _closed = false; 56 57 63 public DelegatingConnection(Connection c) { 64 super(); 65 _conn = c; 66 } 67 68 77 public DelegatingConnection(Connection c, AbandonedConfig config) { 78 super(config); 79 _conn = c; 80 } 81 82 86 public Connection getDelegate() { 87 return _conn; 88 } 89 90 public boolean equals(Object obj) { 91 Connection delegate = getInnermostDelegate(); 92 if (delegate == null) { 93 return false; 94 } 95 if (obj instanceof DelegatingConnection) { 96 DelegatingConnection c = (DelegatingConnection) obj; 97 return delegate.equals(c.getInnermostDelegate()); 98 } 99 else { 100 return delegate.equals(obj); 101 } 102 } 103 104 public int hashCode() { 105 Object obj = getInnermostDelegate(); 106 if (obj == null) { 107 return 0; 108 } 109 return obj.hashCode(); 110 } 111 112 113 128 public Connection getInnermostDelegate() { 129 Connection c = _conn; 130 while(c != null && c instanceof DelegatingConnection) { 131 c = ((DelegatingConnection)c).getDelegate(); 132 if(this == c) { 133 return null; 134 } 135 } 136 return c; 137 } 138 139 140 public void setDelegate(Connection c) { 141 _conn = c; 142 } 143 144 148 public void close() throws SQLException 149 { 150 passivate(); 151 _conn.close(); 152 } 153 154 protected void handleException(SQLException e) throws SQLException { 155 throw e; 156 } 157 158 public Statement createStatement() throws SQLException { 159 checkOpen(); 160 try { 161 return new DelegatingStatement(this, _conn.createStatement()); 162 } 163 catch (SQLException e) { 164 handleException(e); 165 return null; 166 } 167 } 168 169 public Statement createStatement(int resultSetType, 170 int resultSetConcurrency) throws SQLException { 171 checkOpen(); 172 try { 173 return new DelegatingStatement 174 (this, _conn.createStatement(resultSetType,resultSetConcurrency)); 175 } 176 catch (SQLException e) { 177 handleException(e); 178 return null; 179 } 180 } 181 182 public PreparedStatement prepareStatement(String sql) throws SQLException { 183 checkOpen(); 184 try { 185 return new DelegatingPreparedStatement 186 (this, _conn.prepareStatement(sql)); 187 } 188 catch (SQLException e) { 189 handleException(e); 190 return null; 191 } 192 } 193 194 public PreparedStatement prepareStatement(String sql, 195 int resultSetType, 196 int resultSetConcurrency) throws SQLException { 197 checkOpen(); 198 try { 199 return new DelegatingPreparedStatement 200 (this, _conn.prepareStatement 201 (sql,resultSetType,resultSetConcurrency)); 202 } 203 catch (SQLException e) { 204 handleException(e); 205 return null; 206 } 207 } 208 209 public CallableStatement prepareCall(String sql) throws SQLException { 210 checkOpen(); 211 try { 212 return new DelegatingCallableStatement(this, _conn.prepareCall(sql)); 213 } 214 catch (SQLException e) { 215 handleException(e); 216 return null; 217 } 218 } 219 220 public CallableStatement prepareCall(String sql, 221 int resultSetType, 222 int resultSetConcurrency) throws SQLException { 223 checkOpen(); 224 try { 225 return new DelegatingCallableStatement 226 (this, _conn.prepareCall(sql, resultSetType,resultSetConcurrency)); 227 } 228 catch (SQLException e) { 229 handleException(e); 230 return null; 231 } 232 } 233 234 public void clearWarnings() throws SQLException 235 { checkOpen(); try { _conn.clearWarnings(); } catch (SQLException e) { handleException(e); } } 236 237 public void commit() throws SQLException 238 { checkOpen(); try { _conn.commit(); } catch (SQLException e) { handleException(e); } } 239 240 public boolean getAutoCommit() throws SQLException 241 { checkOpen(); try { return _conn.getAutoCommit(); } catch (SQLException e) { handleException(e); return false; } 242 } 243 public String getCatalog() throws SQLException 244 { checkOpen(); try { return _conn.getCatalog(); } catch (SQLException e) { handleException(e); return null; } } 245 246 public DatabaseMetaData getMetaData() throws SQLException 247 { checkOpen(); try { return _conn.getMetaData(); } catch (SQLException e) { handleException(e); return null; } } 248 249 public int getTransactionIsolation() throws SQLException 250 { checkOpen(); try { return _conn.getTransactionIsolation(); } catch (SQLException e) { handleException(e); return -1; } } 251 252 public Map getTypeMap() throws SQLException 253 { checkOpen(); try { return _conn.getTypeMap(); } catch (SQLException e) { handleException(e); return null; } } 254 255 public SQLWarning getWarnings() throws SQLException 256 { checkOpen(); try { return _conn.getWarnings(); } catch (SQLException e) { handleException(e); return null; } } 257 258 public boolean isReadOnly() throws SQLException 259 { checkOpen(); try { return _conn.isReadOnly(); } catch (SQLException e) { handleException(e); return false; } } 260 261 public String nativeSQL(String sql) throws SQLException 262 { checkOpen(); try { return _conn.nativeSQL(sql); } catch (SQLException e) { handleException(e); return null; } } 263 264 public void rollback() throws SQLException 265 { checkOpen(); try { _conn.rollback(); } catch (SQLException e) { handleException(e); } } 266 267 public void setAutoCommit(boolean autoCommit) throws SQLException 268 { checkOpen(); try { _conn.setAutoCommit(autoCommit); } catch (SQLException e) { handleException(e); } } 269 270 public void setCatalog(String catalog) throws SQLException 271 { checkOpen(); try { _conn.setCatalog(catalog); } catch (SQLException e) { handleException(e); } } 272 273 public void setReadOnly(boolean readOnly) throws SQLException 274 { checkOpen(); try { _conn.setReadOnly(readOnly); } catch (SQLException e) { handleException(e); } } 275 276 public void setTransactionIsolation(int level) throws SQLException 277 { checkOpen(); try { _conn.setTransactionIsolation(level); } catch (SQLException e) { handleException(e); } } 278 279 public void setTypeMap(Map map) throws SQLException 280 { checkOpen(); try { _conn.setTypeMap(map); } catch (SQLException e) { handleException(e); } } 281 282 public boolean isClosed() throws SQLException { 283 if(_closed || _conn.isClosed()) { 284 return true; 285 } 286 return false; 287 } 288 289 protected void checkOpen() throws SQLException { 290 if(_closed) { 291 throw new SQLException ("Connection is closed."); 292 } 293 } 294 295 protected void activate() { 296 _closed = false; 297 setLastUsed(); 298 if(_conn instanceof DelegatingConnection) { 299 ((DelegatingConnection)_conn).activate(); 300 } 301 } 302 303 protected void passivate() throws SQLException { 304 try { 305 List statements = getTrace(); 308 if( statements != null) { 309 Statement [] set = new Statement [statements.size()]; 310 statements.toArray(set); 311 for (int i = 0; i < set.length; i++) { 312 set[i].close(); 313 } 314 clearTrace(); 315 } 316 setLastUsed(0); 317 if(_conn instanceof DelegatingConnection) { 318 ((DelegatingConnection)_conn).passivate(); 319 } 320 } 321 finally { 322 _closed = true; 323 } 324 } 325 326 329 330 331 public int getHoldability() throws SQLException 332 { checkOpen(); try { return _conn.getHoldability(); } catch (SQLException e) { handleException(e); return 0; } } 333 334 public void setHoldability(int holdability) throws SQLException 335 { checkOpen(); try { _conn.setHoldability(holdability); } catch (SQLException e) { handleException(e); } } 336 337 public java.sql.Savepoint setSavepoint() throws SQLException 338 { checkOpen(); try { return _conn.setSavepoint(); } catch (SQLException e) { handleException(e); return null; } } 339 340 public java.sql.Savepoint setSavepoint(String name) throws SQLException 341 { checkOpen(); try { return _conn.setSavepoint(name); } catch (SQLException e) { handleException(e); return null; } } 342 343 public void rollback(java.sql.Savepoint savepoint) throws SQLException 344 { checkOpen(); try { _conn.rollback(savepoint); } catch (SQLException e) { handleException(e); } } 345 346 public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException 347 { checkOpen(); try { _conn.releaseSavepoint(savepoint); } catch (SQLException e) { handleException(e); } } 348 349 public Statement createStatement(int resultSetType, 350 int resultSetConcurrency, 351 int resultSetHoldability) throws SQLException { 352 checkOpen(); 353 try { 354 return new DelegatingStatement(this, _conn.createStatement( 355 resultSetType, resultSetConcurrency, resultSetHoldability)); 356 } 357 catch (SQLException e) { 358 handleException(e); 359 return null; 360 } 361 } 362 363 public PreparedStatement prepareStatement(String sql, int resultSetType, 364 int resultSetConcurrency, 365 int resultSetHoldability) throws SQLException { 366 checkOpen(); 367 try { 368 return new DelegatingPreparedStatement(this, _conn.prepareStatement( 369 sql, resultSetType, resultSetConcurrency, resultSetHoldability)); 370 } 371 catch (SQLException e) { 372 handleException(e); 373 return null; 374 } 375 } 376 377 public CallableStatement prepareCall(String sql, int resultSetType, 378 int resultSetConcurrency, 379 int resultSetHoldability) throws SQLException { 380 checkOpen(); 381 try { 382 return new DelegatingCallableStatement(this, _conn.prepareCall( 383 sql, resultSetType, resultSetConcurrency, resultSetHoldability)); 384 } 385 catch (SQLException e) { 386 handleException(e); 387 return null; 388 } 389 } 390 391 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { 392 checkOpen(); 393 try { 394 return new DelegatingPreparedStatement(this, _conn.prepareStatement( 395 sql, autoGeneratedKeys)); 396 } 397 catch (SQLException e) { 398 handleException(e); 399 return null; 400 } 401 } 402 403 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException { 404 checkOpen(); 405 try { 406 return new DelegatingPreparedStatement(this, _conn.prepareStatement( 407 sql, columnIndexes)); 408 } 409 catch (SQLException e) { 410 handleException(e); 411 return null; 412 } 413 } 414 415 public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException { 416 checkOpen(); 417 try { 418 return new DelegatingPreparedStatement(this, _conn.prepareStatement( 419 sql, columnNames)); 420 } 421 catch (SQLException e) { 422 handleException(e); 423 return null; 424 } 425 } 426 427 } 428 | Popular Tags |