1 4 package org.ofbiz.minerva.pool.jdbc; 5 6 import java.sql.CallableStatement ; 7 import java.sql.Connection ; 8 import java.sql.DatabaseMetaData ; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.sql.SQLWarning ; 13 import java.sql.Savepoint ; 14 import java.sql.Statement ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Vector ; 21 22 import org.ofbiz.minerva.pool.PoolEvent; 23 import org.ofbiz.minerva.pool.PoolEventListener; 24 import org.ofbiz.minerva.pool.PooledObject; 25 import org.ofbiz.minerva.pool.cache.LeastRecentlyUsedCache; 26 import org.ofbiz.minerva.pool.cache.ObjectCache; 27 28 37 public class ConnectionInPool implements PooledObject, ConnectionWrapper { 38 39 private final static String CLOSED = "Connection has been closed!"; 40 public final static int PS_CACHE_UNLIMITED = 0; 41 public final static int PS_CACHE_DISABLED = -1; 42 public final static HashMap psCaches = new HashMap (); 43 44 private Connection con; 45 private HashSet statements; 46 private Vector listeners; 47 private int preparedStatementCacheSize = 0; 48 private ObjectCache preparedStatementCache; 49 50 54 public ConnectionInPool(Connection con) { 55 this.con = con; 56 preparedStatementCache = (ObjectCache) psCaches.get(con); 57 if (preparedStatementCache == null) { 58 PreparedStatementFactory factory = new PreparedStatementFactory(con); 59 preparedStatementCache = new LeastRecentlyUsedCache(factory, preparedStatementCacheSize); 60 psCaches.put(con, preparedStatementCache); 61 } 62 statements = new HashSet (); 63 listeners = new Vector (); 64 } 65 66 74 public ConnectionInPool(Connection con, int psCacheSize) { 75 this.con = con; 76 if (psCacheSize >= 0) { 77 preparedStatementCache = (ObjectCache) psCaches.get(con); 78 if (preparedStatementCache == null) { 79 PreparedStatementFactory factory = new PreparedStatementFactory(con); 80 preparedStatementCache = new LeastRecentlyUsedCache(factory, preparedStatementCacheSize); 81 psCaches.put(con, preparedStatementCache); 82 } 83 } 84 setPSCacheSize(psCacheSize); 85 statements = new HashSet (); 86 listeners = new Vector (); 87 } 88 89 96 public void setPSCacheSize(int maxSize) { 97 preparedStatementCacheSize = maxSize; 98 if (maxSize >= 0 && preparedStatementCache != null) 99 preparedStatementCache.setSize(maxSize); 100 } 101 102 106 public int getPSCacheSize() { 107 return preparedStatementCacheSize; 108 } 109 110 113 public void addPoolEventListener(PoolEventListener listener) { 114 listeners.addElement(listener); 115 } 116 117 120 public void removePoolEventListener(PoolEventListener listener) { 121 listeners.remove(listener); 122 } 123 124 130 public Connection getUnderlyingConnection() { 131 return con; 132 } 133 134 138 public void shutdown() { 139 con = null; 140 statements = null; 141 listeners = null; 142 } 143 144 147 public void setLastUsed() { 148 firePoolEvent(new PoolEvent(this, PoolEvent.OBJECT_USED)); 149 } 150 151 154 public void setError(SQLException e) { 155 firePoolEvent(new PoolEvent(this, PoolEvent.OBJECT_ERROR)); 156 } 157 158 161 public void setCatastrophicError(SQLException e) { 162 PoolEvent pe = new PoolEvent(this, PoolEvent.OBJECT_ERROR); 163 pe.setCatastrophic(); 164 firePoolEvent(pe); 165 } 166 167 172 public void statementClosed(Statement st) { 173 statements.remove(st); 174 if ((con != null) && (st instanceof PreparedStatementInPool)) { 175 PreparedStatementInPool ps = (PreparedStatementInPool) st; 177 PreparedStatement ups = ps.getUnderlyingPreparedStatement(); 178 if (preparedStatementCacheSize >= 0) { 179 preparedStatementCache.returnObject(ps.getSql(), ups); 180 } else { 181 try { 182 ups.close(); 183 } catch (SQLException e) { 184 } 185 } 186 199 } 200 } 201 202 207 public void reset() throws SQLException { 208 Collection copy = (Collection ) statements.clone(); 209 Iterator it = copy.iterator(); 210 while (it.hasNext()) 211 try { 212 ((Statement ) it.next()).close(); 213 } catch (SQLException e) { 214 } 215 if (!con.getAutoCommit()) 216 con.rollback(); 217 con = null; 218 } 219 220 223 protected void firePoolEvent(PoolEvent evt) { 224 Vector local = (Vector ) listeners.clone(); 225 for (int i = local.size() - 1; i >= 0; i--) 226 if (evt.getType() == PoolEvent.OBJECT_CLOSED) 227 ((PoolEventListener) local.elementAt(i)).objectClosed(evt); 228 else if (evt.getType() == PoolEvent.OBJECT_ERROR) 229 ((PoolEventListener) local.elementAt(i)).objectError(evt); 230 else 231 ((PoolEventListener) local.elementAt(i)).objectUsed(evt); 232 } 233 234 public Statement createStatement() throws SQLException { 236 if (con == null) throw new SQLException (CLOSED); 237 try { 238 StatementInPool st = new StatementInPool(con.createStatement(), this); 239 statements.add(st); 240 return st; 241 } catch (SQLException e) { 242 setError(e); 243 throw e; 244 } 245 } 246 247 public PreparedStatement prepareStatement(String sql) throws SQLException { 248 return prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 249 } 250 251 public CallableStatement prepareCall(String sql) throws SQLException { 252 if (con == null) throw new SQLException (CLOSED); 253 try { 254 return con.prepareCall(sql); 255 } catch (SQLException e) { 256 setError(e); 257 throw e; 258 } 259 } 260 261 public String nativeSQL(String sql) throws SQLException { 262 if (con == null) throw new SQLException (CLOSED); 263 try { 264 return con.nativeSQL(sql); 265 } catch (SQLException e) { 266 setError(e); 267 throw e; 268 } 269 } 270 271 public void setAutoCommit(boolean autoCommit) throws SQLException { 272 if (con == null) throw new SQLException (CLOSED); 273 try { 274 con.setAutoCommit(autoCommit); 275 } catch (SQLException e) { 276 setError(e); 277 throw e; 278 } 279 } 280 281 public boolean getAutoCommit() throws SQLException { 282 if (con == null) throw new SQLException (CLOSED); 283 try { 284 return con.getAutoCommit(); 285 } catch (SQLException e) { 286 setError(e); 287 throw e; 288 } 289 } 290 291 public void commit() throws SQLException { 292 if (con == null) throw new SQLException (CLOSED); 293 try { 294 con.commit(); 295 } catch (SQLException e) { 296 setCatastrophicError(e); 297 throw e; 298 } 299 } 300 301 public void rollback() throws SQLException { 302 if (con == null) throw new SQLException (CLOSED); 303 try { 304 con.rollback(); 305 } catch (SQLException e) { 306 setCatastrophicError(e); 307 throw e; 308 } 309 } 310 311 public void close() throws SQLException { 312 if (con == null) throw new SQLException (CLOSED); 313 firePoolEvent(new PoolEvent(this, PoolEvent.OBJECT_CLOSED)); 314 shutdown(); 315 } 316 317 public boolean isClosed() throws SQLException { 318 if (con == null) return true; 319 try { 320 return con.isClosed(); 321 } catch (SQLException e) { 322 setError(e); 323 throw e; 324 } 325 } 326 327 public DatabaseMetaData getMetaData() throws SQLException { 328 if (con == null) throw new SQLException (CLOSED); 329 try { 330 return con.getMetaData(); 331 } catch (SQLException e) { 332 setError(e); 333 throw e; 334 } 335 } 336 337 public void setReadOnly(boolean readOnly) throws SQLException { 338 if (con == null) throw new SQLException (CLOSED); 339 try { 340 con.setReadOnly(readOnly); 341 } catch (SQLException e) { 342 setError(e); 343 throw e; 344 } 345 } 346 347 public boolean isReadOnly() throws SQLException { 348 if (con == null) throw new SQLException (CLOSED); 349 try { 350 return con.isReadOnly(); 351 } catch (SQLException e) { 352 setError(e); 353 throw e; 354 } 355 } 356 357 public void setCatalog(String catalog) throws SQLException { 358 if (con == null) throw new SQLException (CLOSED); 359 try { 360 con.setCatalog(catalog); 361 } catch (SQLException e) { 362 setError(e); 363 throw e; 364 } 365 } 366 367 public String getCatalog() throws SQLException { 368 if (con == null) throw new SQLException (CLOSED); 369 try { 370 return con.getCatalog(); 371 } catch (SQLException e) { 372 setError(e); 373 throw e; 374 } 375 } 376 377 public void setTransactionIsolation(int level) throws SQLException { 378 if (con == null) throw new SQLException (CLOSED); 379 try { 380 con.setTransactionIsolation(level); 381 } catch (SQLException e) { 382 setError(e); 383 throw e; 384 } 385 } 386 387 public int getTransactionIsolation() throws SQLException { 388 if (con == null) throw new SQLException (CLOSED); 389 try { 390 return con.getTransactionIsolation(); 391 } catch (SQLException e) { 392 setError(e); 393 throw e; 394 } 395 } 396 397 public SQLWarning getWarnings() throws SQLException { 398 if (con == null) throw new SQLException (CLOSED); 399 try { 400 return con.getWarnings(); 401 } catch (SQLException e) { 402 setError(e); 403 throw e; 404 } 405 } 406 407 public void clearWarnings() throws SQLException { 408 if (con == null) throw new SQLException (CLOSED); 409 try { 410 con.clearWarnings(); 411 } catch (SQLException e) { 412 setError(e); 413 throw e; 414 } 415 } 416 417 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 418 if (con == null) throw new SQLException (CLOSED); 419 try { 420 StatementInPool st = new StatementInPool(con.createStatement(resultSetType, resultSetConcurrency), this); 421 statements.add(st); 422 return st; 423 } catch (SQLException e) { 424 setError(e); 425 throw e; 426 } 427 } 428 429 430 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 431 if (con == null) throw new SQLException (CLOSED); 432 try { 433 PreparedStatementInPool wrapper = null; 434 if (preparedStatementCacheSize >= 0) { 435 PreparedStatement ps = (PreparedStatement ) preparedStatementCache.useObject(sql); 436 if (ps == null) 437 throw new SQLException ("Unable to create PreparedStatement!"); 438 wrapper = new PreparedStatementInPool(ps, this, sql); 439 } else { 440 wrapper = new PreparedStatementInPool(con.prepareStatement(sql, resultSetType, resultSetConcurrency), this, sql); 441 } 442 statements.add(wrapper); 443 return wrapper; 444 } catch (SQLException e) { 445 setError(e); 446 throw e; 447 } 448 } 449 450 455 456 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 457 if (con == null) throw new SQLException (CLOSED); 458 try { 459 return con.prepareCall(sql, resultSetType, resultSetConcurrency); 460 } catch (SQLException e) { 461 setError(e); 462 throw e; 463 } 464 } 465 466 public Map getTypeMap() throws SQLException { 467 if (con == null) throw new SQLException (CLOSED); 468 try { 469 return con.getTypeMap(); 470 } catch (SQLException e) { 471 setError(e); 472 throw e; 473 } 474 } 475 476 public void setTypeMap(Map map) throws SQLException { 477 if (con == null) throw new SQLException (CLOSED); 478 try { 479 con.setTypeMap(map); 480 } catch (SQLException e) { 481 setError(e); 482 throw e; 483 } 484 } 485 486 487 489 492 public void setHoldability(int arg0) throws SQLException { 493 495 } 496 497 500 public int getHoldability() throws SQLException { 501 return 0; 503 } 504 505 508 public Savepoint setSavepoint() throws SQLException { 509 return null; 511 } 512 513 516 public Savepoint setSavepoint(String arg0) throws SQLException { 517 return null; 519 } 520 521 524 public void rollback(Savepoint arg0) throws SQLException { 525 527 } 528 529 532 public void releaseSavepoint(Savepoint arg0) throws SQLException { 533 535 } 536 537 540 public Statement createStatement(int arg0, int arg1, int arg2) throws SQLException { 541 return null; 543 } 544 545 548 public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException { 549 return null; 551 } 552 553 556 public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException { 557 return null; 559 } 560 561 564 public PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException { 565 return null; 567 } 568 569 572 public PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException { 573 return null; 575 } 576 577 580 public PreparedStatement prepareStatement(String arg0, String [] arg1) throws SQLException { 581 return null; 583 } 584 } 585 | Popular Tags |