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