1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.util.List ; 24 25 import org.apache.commons.pool.KeyedObjectPool; 26 27 40 public class PoolablePreparedStatement extends DelegatingPreparedStatement implements PreparedStatement { 41 44 protected KeyedObjectPool _pool = null; 45 46 49 protected Object _key = null; 50 51 58 public PoolablePreparedStatement(PreparedStatement stmt, Object key, KeyedObjectPool pool, Connection conn) { 59 super((DelegatingConnection) conn, stmt); 60 _pool = pool; 61 _key = key; 62 63 if(_conn != null) { 66 _conn.removeTrace(this); 67 } 68 } 69 70 73 public void close() throws SQLException { 74 if(isClosed()) { 75 throw new SQLException ("Already closed"); 76 } else { 77 try { 78 _pool.returnObject(_key,this); 79 } catch(SQLException e) { 80 throw e; 81 } catch(RuntimeException e) { 82 throw e; 83 } catch(Exception e) { 84 throw new SQLNestedException("Cannot close preparedstatement (return to pool failed)", e); 85 } 86 } 87 } 88 89 protected void activate() throws SQLException { 90 _closed = false; 91 if(_conn != null) { 92 _conn.addTrace(this); 93 } 94 super.activate(); 95 } 96 97 protected void passivate() throws SQLException { 98 _closed = true; 99 if(_conn != null) { 100 _conn.removeTrace(this); 101 } 102 103 List resultSets = getTrace(); 108 if( resultSets != null) { 109 ResultSet [] set = (ResultSet []) resultSets.toArray(new ResultSet [resultSets.size()]); 110 for (int i = 0; i < set.length; i++) { 111 set[i].close(); 112 } 113 clearTrace(); 114 } 115 116 super.passivate(); 117 } 118 119 } 120 | Popular Tags |