| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.commons.dbcp; |
| 19 | |
|
| 20 | |
import java.sql.CallableStatement; |
| 21 | |
import java.sql.Connection; |
| 22 | |
import java.sql.PreparedStatement; |
| 23 | |
import java.sql.SQLException; |
| 24 | |
|
| 25 | |
import java.util.NoSuchElementException; |
| 26 | |
|
| 27 | |
import org.apache.commons.pool.KeyedObjectPool; |
| 28 | |
import org.apache.commons.pool.KeyedPoolableObjectFactory; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public class PoolingConnection extends DelegatingConnection implements Connection, KeyedPoolableObjectFactory { |
| 46 | |
|
| 47 | 569 | protected KeyedObjectPool _pstmtPool = null; |
| 48 | |
|
| 49 | |
|
| 50 | |
private static final byte STATEMENT_PREPAREDSTMT = 0; |
| 51 | |
|
| 52 | |
|
| 53 | |
private static final byte STATEMENT_CALLABLESTMT = 1; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public PoolingConnection(Connection c) { |
| 61 | 0 | super(c); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public PoolingConnection(Connection c, KeyedObjectPool pool) { |
| 70 | 569 | super(c); |
| 71 | 569 | _pstmtPool = pool; |
| 72 | 569 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public synchronized void close() throws SQLException { |
| 80 | 529 | if(null != _pstmtPool) { |
| 81 | 527 | KeyedObjectPool oldpool = _pstmtPool; |
| 82 | 527 | _pstmtPool = null; |
| 83 | |
try { |
| 84 | 527 | oldpool.close(); |
| 85 | 0 | } catch(RuntimeException e) { |
| 86 | 0 | throw e; |
| 87 | 0 | } catch(SQLException e) { |
| 88 | 0 | throw e; |
| 89 | 0 | } catch(Exception e) { |
| 90 | 0 | throw (SQLException) new SQLException("Cannot close connection").initCause(e); |
| 91 | 527 | } |
| 92 | |
} |
| 93 | 529 | getInnermostDelegate().close(); |
| 94 | 527 | } |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public PreparedStatement prepareStatement(String sql) throws SQLException { |
| 102 | 3772 | if (null == _pstmtPool) { |
| 103 | 2 | throw new SQLException( |
| 104 | |
"Statement pool is null - closed or invalid PoolingConnection."); |
| 105 | |
} |
| 106 | |
try { |
| 107 | 3770 | return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql))); |
| 108 | 2 | } catch(NoSuchElementException e) { |
| 109 | 2 | throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e); |
| 110 | 0 | } catch(RuntimeException e) { |
| 111 | 0 | throw e; |
| 112 | 0 | } catch(Exception e) { |
| 113 | 0 | throw new SQLNestedException("Borrow prepareStatement from pool failed", e); |
| 114 | |
} |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { |
| 125 | 12 | if (null == _pstmtPool) { |
| 126 | 0 | throw new SQLException( |
| 127 | |
"Statement pool is null - closed or invalid PoolingConnection."); |
| 128 | |
} |
| 129 | |
try { |
| 130 | 12 | return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql,resultSetType,resultSetConcurrency))); |
| 131 | 0 | } catch(NoSuchElementException e) { |
| 132 | 0 | throw (SQLException) new SQLException("MaxOpenPreparedStatements limit reached").initCause(e); |
| 133 | 0 | } catch(RuntimeException e) { |
| 134 | 0 | throw e; |
| 135 | 0 | } catch(Exception e) { |
| 136 | 0 | throw (SQLException) new SQLException("Borrow prepareStatement from pool failed").initCause(e); |
| 137 | |
} |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public CallableStatement prepareCall(String sql) throws SQLException { |
| 148 | |
try { |
| 149 | 84 | return (CallableStatement) (_pstmtPool.borrowObject(createKey(sql, STATEMENT_CALLABLESTMT))); |
| 150 | 0 | } catch (NoSuchElementException e) { |
| 151 | 0 | throw new SQLNestedException("MaxOpenCallableStatements limit reached", e); |
| 152 | 0 | } catch (RuntimeException e) { |
| 153 | 0 | throw e; |
| 154 | 0 | } catch (Exception e) { |
| 155 | 0 | throw new SQLNestedException("Borrow callableStatement from pool failed", e); |
| 156 | |
} |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { |
| 169 | |
try { |
| 170 | 6 | return (CallableStatement) (_pstmtPool.borrowObject(createKey(sql, resultSetType, |
| 171 | |
resultSetConcurrency, STATEMENT_CALLABLESTMT))); |
| 172 | 0 | } catch (NoSuchElementException e) { |
| 173 | 0 | throw new SQLNestedException("MaxOpenCallableStatements limit reached", e); |
| 174 | 0 | } catch (RuntimeException e) { |
| 175 | 0 | throw e; |
| 176 | 0 | } catch (Exception e) { |
| 177 | 0 | throw new SQLNestedException("Borrow callableStatement from pool failed", e); |
| 178 | |
} |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
protected Object createKey(String sql, int resultSetType, int resultSetConcurrency) { |
| 214 | 12 | String catalog = null; |
| 215 | |
try { |
| 216 | 12 | catalog = getCatalog(); |
| 217 | 12 | } catch (SQLException e) {} |
| 218 | 12 | return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency); |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
protected Object createKey(String sql, int resultSetType, int resultSetConcurrency, byte stmtType) { |
| 229 | 6 | String catalog = null; |
| 230 | |
try { |
| 231 | 6 | catalog = getCatalog(); |
| 232 | 6 | } catch (SQLException e) {} |
| 233 | 6 | return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, stmtType); |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
protected Object createKey(String sql) { |
| 241 | 3770 | String catalog = null; |
| 242 | |
try { |
| 243 | 3770 | catalog = getCatalog(); |
| 244 | 3770 | } catch (SQLException e) {} |
| 245 | 3770 | return new PStmtKey(normalizeSQL(sql), catalog); |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
protected Object createKey(String sql, byte stmtType) { |
| 254 | 84 | String catalog = null; |
| 255 | |
try { |
| 256 | 84 | catalog = getCatalog(); |
| 257 | 84 | } catch (SQLException e) {} |
| 258 | 84 | return new PStmtKey(normalizeSQL(sql), catalog, stmtType); |
| 259 | |
} |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
protected String normalizeSQL(String sql) { |
| 266 | 3872 | return sql.trim(); |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
public Object makeObject(Object obj) throws Exception { |
| 279 | 228 | if(null == obj || !(obj instanceof PStmtKey)) { |
| 280 | 0 | throw new IllegalArgumentException("Prepared statement key is null or invalid."); |
| 281 | |
} else { |
| 282 | 228 | PStmtKey key = (PStmtKey)obj; |
| 283 | 228 | if( null == key._resultSetType && null == key._resultSetConcurrency ) { |
| 284 | 210 | if (key._stmtType == STATEMENT_PREPAREDSTMT ) { |
| 285 | 134 | return new PoolablePreparedStatement(getDelegate().prepareStatement( key._sql), key, _pstmtPool, this); |
| 286 | |
} else { |
| 287 | 76 | return new PoolableCallableStatement(getDelegate().prepareCall( key._sql), key, _pstmtPool, this); |
| 288 | |
} |
| 289 | |
} else { |
| 290 | 18 | if(key._stmtType == STATEMENT_PREPAREDSTMT) { |
| 291 | 12 | return new PoolablePreparedStatement(getDelegate().prepareStatement( |
| 292 | |
key._sql, key._resultSetType.intValue(),key._resultSetConcurrency.intValue()), key, _pstmtPool, this); |
| 293 | |
} else { |
| 294 | 6 | return new PoolableCallableStatement( getDelegate().prepareCall( |
| 295 | |
key._sql,key._resultSetType.intValue(), key._resultSetConcurrency.intValue()), key, _pstmtPool, this); |
| 296 | |
} |
| 297 | |
} |
| 298 | |
} |
| 299 | |
} |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public void destroyObject(Object key, Object obj) throws Exception { |
| 310 | 143 | if(obj instanceof DelegatingPreparedStatement) { |
| 311 | 143 | ((DelegatingPreparedStatement)obj).getInnermostDelegate().close(); |
| 312 | |
} else { |
| 313 | 0 | ((PreparedStatement)obj).close(); |
| 314 | |
} |
| 315 | 143 | } |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
public boolean validateObject(Object key, Object obj) { |
| 326 | 5096 | return true; |
| 327 | |
} |
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
public void activateObject(Object key, Object obj) throws Exception { |
| 337 | 3870 | ((DelegatingPreparedStatement)obj).activate(); |
| 338 | 3870 | } |
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
public void passivateObject(Object key, Object obj) throws Exception { |
| 349 | 3856 | ((PreparedStatement)obj).clearParameters(); |
| 350 | 3856 | ((DelegatingPreparedStatement)obj).passivate(); |
| 351 | 3856 | } |
| 352 | |
|
| 353 | |
public String toString() { |
| 354 | 2 | if (_pstmtPool != null ) { |
| 355 | 0 | return "PoolingConnection: " + _pstmtPool.toString(); |
| 356 | |
} else { |
| 357 | 2 | return "PoolingConnection: null"; |
| 358 | |
} |
| 359 | |
} |
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
static class PStmtKey { |
| 365 | |
|
| 366 | |
|
| 367 | 3872 | protected String _sql = null; |
| 368 | |
|
| 369 | |
|
| 370 | 3872 | protected Integer _resultSetType = null; |
| 371 | |
|
| 372 | |
|
| 373 | 3872 | protected Integer _resultSetConcurrency = null; |
| 374 | |
|
| 375 | |
|
| 376 | 3872 | protected String _catalog = null; |
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | 3872 | protected byte _stmtType = STATEMENT_PREPAREDSTMT; |
| 383 | |
|
| 384 | 0 | PStmtKey(String sql) { |
| 385 | 0 | _sql = sql; |
| 386 | 0 | } |
| 387 | |
|
| 388 | 3770 | PStmtKey(String sql, String catalog) { |
| 389 | 3770 | _sql = sql; |
| 390 | 3770 | _catalog = catalog; |
| 391 | 3770 | } |
| 392 | |
|
| 393 | 84 | PStmtKey(String sql, String catalog, byte stmtType) { |
| 394 | 84 | _sql = sql; |
| 395 | 84 | _catalog = catalog; |
| 396 | 84 | _stmtType = stmtType; |
| 397 | 84 | } |
| 398 | |
|
| 399 | 0 | PStmtKey(String sql, int resultSetType, int resultSetConcurrency) { |
| 400 | 0 | _sql = sql; |
| 401 | 0 | _resultSetType = new Integer(resultSetType); |
| 402 | 0 | _resultSetConcurrency = new Integer(resultSetConcurrency); |
| 403 | 0 | } |
| 404 | |
|
| 405 | 12 | PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency) { |
| 406 | 12 | _sql = sql; |
| 407 | 12 | _catalog = catalog; |
| 408 | 12 | _resultSetType = new Integer(resultSetType); |
| 409 | 12 | _resultSetConcurrency = new Integer(resultSetConcurrency); |
| 410 | 12 | } |
| 411 | |
|
| 412 | 6 | PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency, byte stmtType) { |
| 413 | 6 | _sql = sql; |
| 414 | 6 | _catalog = catalog; |
| 415 | 6 | _resultSetType = new Integer(resultSetType); |
| 416 | 6 | _resultSetConcurrency = new Integer(resultSetConcurrency); |
| 417 | 6 | _stmtType = stmtType; |
| 418 | 6 | } |
| 419 | |
|
| 420 | |
public boolean equals(Object that) { |
| 421 | |
try { |
| 422 | 4593 | PStmtKey key = (PStmtKey)that; |
| 423 | 4593 | return( ((null == _sql && null == key._sql) || _sql.equals(key._sql)) && |
| 424 | |
((null == _catalog && null == key._catalog) || _catalog.equals(key._catalog)) && |
| 425 | |
((null == _resultSetType && null == key._resultSetType) || _resultSetType.equals(key._resultSetType)) && |
| 426 | |
((null == _resultSetConcurrency && null == key._resultSetConcurrency) || _resultSetConcurrency.equals(key._resultSetConcurrency)) && |
| 427 | |
(_stmtType == key._stmtType) |
| 428 | |
); |
| 429 | 0 | } catch(ClassCastException e) { |
| 430 | 0 | return false; |
| 431 | 62 | } catch(NullPointerException e) { |
| 432 | 62 | return false; |
| 433 | |
} |
| 434 | |
} |
| 435 | |
|
| 436 | |
public int hashCode() { |
| 437 | 8433 | if (_catalog==null) |
| 438 | 5461 | return(null == _sql ? 0 : _sql.hashCode()); |
| 439 | |
else |
| 440 | 2972 | return(null == _sql ? _catalog.hashCode() : (_catalog + _sql).hashCode()); |
| 441 | |
} |
| 442 | |
|
| 443 | |
public String toString() { |
| 444 | 0 | StringBuffer buf = new StringBuffer(); |
| 445 | 0 | buf.append("PStmtKey: sql="); |
| 446 | 0 | buf.append(_sql); |
| 447 | 0 | buf.append(", catalog="); |
| 448 | 0 | buf.append(_catalog); |
| 449 | 0 | buf.append(", resultSetType="); |
| 450 | 0 | buf.append(_resultSetType); |
| 451 | 0 | buf.append(", resultSetConcurrency="); |
| 452 | 0 | buf.append(_resultSetConcurrency); |
| 453 | 0 | buf.append(", statmentType="); |
| 454 | 0 | buf.append(_stmtType); |
| 455 | 0 | return buf.toString(); |
| 456 | |
} |
| 457 | |
} |
| 458 | |
} |