1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.*; 20 import java.util.NoSuchElementException ; 21 22 import org.apache.commons.pool.*; 23 24 37 public class PoolingConnection extends DelegatingConnection implements Connection, KeyedPoolableObjectFactory { 38 39 protected KeyedObjectPool _pstmtPool = null; 40 41 45 public PoolingConnection(Connection c) { 46 super(c); 47 } 48 49 54 public PoolingConnection(Connection c, KeyedObjectPool pool) { 55 super(c); 56 _pstmtPool = pool; 57 } 58 59 60 64 public synchronized void close() throws SQLException { 65 if(null != _pstmtPool) { 66 KeyedObjectPool oldpool = _pstmtPool; 67 _pstmtPool = null; 68 try { 69 oldpool.close(); 70 } catch(RuntimeException e) { 71 throw e; 72 } catch(SQLException e) { 73 throw e; 74 } catch(Exception e) { 75 throw new SQLNestedException("Cannot close connection", e); 76 } 77 } 78 getInnermostDelegate().close(); 79 } 80 81 85 public synchronized PreparedStatement prepareStatement(String sql) throws SQLException { 86 try { 87 return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql))); 88 } catch(NoSuchElementException e) { 89 throw new SQLNestedException("MaxOpenPreparedStatements limit reached", e); 90 } catch(RuntimeException e) { 91 throw e; 92 } catch(Exception e) { 93 throw new SQLNestedException("Borrow prepareStatement from pool failed", e); 94 } 95 } 96 97 101 public synchronized PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 102 try { 103 return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql,resultSetType,resultSetConcurrency))); 104 } catch(NoSuchElementException e) { 105 throw new SQLNestedException("MaxOpenPreparedStatements limit reached", e); 106 } catch(RuntimeException e) { 107 throw e; 108 } catch(Exception e) { 109 throw new SQLNestedException("Borrow prepareStatement from pool failed", e); 110 } 111 } 112 113 116 117 118 120 143 144 145 146 149 protected Object createKey(String sql, int resultSetType, int resultSetConcurrency) { 150 String catalog = null; 151 try { 152 catalog = getCatalog(); 153 } catch (Exception e) {} 154 return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency); 155 } 156 157 160 protected Object createKey(String sql) { 161 String catalog = null; 162 try { 163 catalog = getCatalog(); 164 } catch (Exception e) {} 165 return new PStmtKey(normalizeSQL(sql), catalog); 166 } 167 168 172 protected String normalizeSQL(String sql) { 173 return sql.trim(); 174 } 175 176 181 public Object makeObject(Object obj) throws Exception { 182 if(null == obj || !(obj instanceof PStmtKey)) { 183 throw new IllegalArgumentException (); 184 } else { 185 PStmtKey key = (PStmtKey)obj; 187 if(null == key._resultSetType && null == key._resultSetConcurrency) { 188 return new PoolablePreparedStatement(getDelegate().prepareStatement(key._sql),key,_pstmtPool,this); 189 } else { 190 return new PoolablePreparedStatement(getDelegate().prepareStatement(key._sql,key._resultSetType.intValue(),key._resultSetConcurrency.intValue()),key,_pstmtPool,this); 191 } 192 } 193 } 194 195 201 public void destroyObject(Object key, Object obj) throws Exception { 202 if(obj instanceof DelegatingPreparedStatement) { 204 ((DelegatingPreparedStatement)obj).getInnermostDelegate().close(); 205 } else { 206 ((PreparedStatement)obj).close(); 207 } 208 } 209 210 217 public boolean validateObject(Object key, Object obj) { 218 return true; 219 } 220 221 227 public void activateObject(Object key, Object obj) throws Exception { 228 ((DelegatingPreparedStatement)obj).activate(); 229 } 230 231 237 public void passivateObject(Object key, Object obj) throws Exception { 238 ((PreparedStatement)obj).clearParameters(); 239 ((DelegatingPreparedStatement)obj).passivate(); 240 } 241 242 public String toString() { 243 return "PoolingConnection: " + _pstmtPool.toString(); 244 } 245 246 249 class PStmtKey { 250 protected String _sql = null; 251 protected Integer _resultSetType = null; 252 protected Integer _resultSetConcurrency = null; 253 protected String _catalog = null; 254 255 PStmtKey(String sql) { 256 _sql = sql; 257 } 258 259 PStmtKey(String sql, String catalog) { 260 _sql = sql; 261 _catalog = catalog; 262 } 263 264 PStmtKey(String sql, int resultSetType, int resultSetConcurrency) { 265 _sql = sql; 266 _resultSetType = new Integer (resultSetType); 267 _resultSetConcurrency = new Integer (resultSetConcurrency); 268 } 269 270 PStmtKey(String sql, String catalog, int resultSetType, int resultSetConcurrency) { 271 _sql = sql; 272 _catalog = catalog; 273 _resultSetType = new Integer (resultSetType); 274 _resultSetConcurrency = new Integer (resultSetConcurrency); 275 } 276 277 public boolean equals(Object that) { 278 try { 279 PStmtKey key = (PStmtKey)that; 280 return( ((null == _sql && null == key._sql) || _sql.equals(key._sql)) && 281 ((null == _catalog && null == key._catalog) || _catalog.equals(key._catalog)) && 282 ((null == _resultSetType && null == key._resultSetType) || _resultSetType.equals(key._resultSetType)) && 283 ((null == _resultSetConcurrency && null == key._resultSetConcurrency) || _resultSetConcurrency.equals(key._resultSetConcurrency)) 284 ); 285 } catch(ClassCastException e) { 286 return false; 287 } catch(NullPointerException e) { 288 return false; 289 } 290 } 291 292 public int hashCode() { 293 if (_catalog==null) 294 return(null == _sql ? 0 : _sql.hashCode()); 295 else 296 return(null == _sql ? _catalog.hashCode() : (_catalog + _sql).hashCode()); 297 } 298 299 public String toString() { 300 StringBuffer buf = new StringBuffer (); 301 buf.append("PStmtKey: sql="); 302 buf.append(_sql); 303 buf.append(", catalog="); 304 buf.append(_catalog); 305 buf.append(", resultSetType="); 306 buf.append(_resultSetType); 307 buf.append(", resultSetConcurrency="); 308 buf.append(_resultSetConcurrency); 309 return buf.toString(); 310 } 311 } 312 } 313 | Popular Tags |