1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.dbcp;
19
20 import java.sql.Connection;
21 import java.sql.PreparedStatement;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.util.List;
25
26 import org.apache.commons.pool.KeyedObjectPool;
27
28 /**
29 * A {@link DelegatingPreparedStatement} that cooperates with
30 * {@link PoolingConnection} to implement a pool of {@link PreparedStatement}s.
31 * <p>
32 * My {@link #close} method returns me to my containing pool. (See {@link PoolingConnection}.)
33 *
34 * @see PoolingConnection
35 * @author Rodney Waldhoff
36 * @author Glenn L. Nielsen
37 * @author James House
38 * @author Dirk Verbeeck
39 * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
40 */
41 public class PoolablePreparedStatement extends DelegatingPreparedStatement implements PreparedStatement {
42 /**
43 * The {@link KeyedObjectPool} from which I was obtained.
44 */
45 protected KeyedObjectPool _pool = null;
46
47 /**
48 * My "key" as used by {@link KeyedObjectPool}.
49 */
50 protected Object _key = null;
51
52 /**
53 * Constructor
54 * @param stmt my underlying {@link PreparedStatement}
55 * @param key my key" as used by {@link KeyedObjectPool}
56 * @param pool the {@link KeyedObjectPool} from which I was obtained.
57 * @param conn the {@link Connection} from which I was created
58 */
59 public PoolablePreparedStatement(PreparedStatement stmt, Object key, KeyedObjectPool pool, Connection conn) {
60 super((DelegatingConnection) conn, stmt);
61 _pool = pool;
62 _key = key;
63
64 // Remove from trace now because this statement will be
65 // added by the activate method.
66 if(_conn != null) {
67 _conn.removeTrace(this);
68 }
69 }
70
71 /**
72 * Return me to my pool.
73 */
74 public void close() throws SQLException {
75 if(isClosed()) {
76 throw new SQLException("Already closed");
77 } else {
78 try {
79 _pool.returnObject(_key,this);
80 } catch(SQLException e) {
81 throw e;
82 } catch(RuntimeException e) {
83 throw e;
84 } catch(Exception e) {
85 throw new SQLNestedException("Cannot close preparedstatement (return to pool failed)", e);
86 }
87 }
88 }
89
90 protected void activate() throws SQLException{
91 _closed = false;
92 if(_conn != null) {
93 _conn.addTrace(this);
94 }
95 super.activate();
96 }
97
98 protected void passivate() throws SQLException {
99 _closed = true;
100 if(_conn != null) {
101 _conn.removeTrace(this);
102 }
103
104 // The JDBC spec requires that a statment close any open
105 // ResultSet's when it is closed.
106 // FIXME The PreparedStatement we're wrapping should handle this for us.
107 // See bug 17301 for what could happen when ResultSets are closed twice.
108 List resultSets = getTrace();
109 if( resultSets != null) {
110 ResultSet[] set = (ResultSet[]) resultSets.toArray(new ResultSet[resultSets.size()]);
111 for (int i = 0; i < set.length; i++) {
112 set[i].close();
113 }
114 clearTrace();
115 }
116
117 super.passivate();
118 }
119
120 }