PoolableCallableStatement.java

  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. package org.apache.commons.dbcp2;

  18. import java.sql.CallableStatement;
  19. import java.sql.Connection;
  20. import java.sql.SQLException;

  21. import org.apache.commons.pool2.KeyedObjectPool;

  22. /**
  23.  * A {@link DelegatingCallableStatement} that cooperates with {@link PoolingConnection} to implement a pool of
  24.  * {@link CallableStatement}s.
  25.  * <p>
  26.  * The {@link #close} method returns this statement to its containing pool. (See {@link PoolingConnection}.)
  27.  *
  28.  * @see PoolingConnection
  29.  * @since 2.0
  30.  */
  31. public class PoolableCallableStatement extends DelegatingCallableStatement {

  32.     /**
  33.      * The {@link KeyedObjectPool} from which this CallableStatement was obtained.
  34.      */
  35.     private final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pool;

  36.     /**
  37.      * Key for this statement in the containing {@link KeyedObjectPool}.
  38.      */
  39.     private final PStmtKey key;

  40.     /**
  41.      * Constructs a new instance.
  42.      *
  43.      * @param callableStatement
  44.      *            the underlying {@link CallableStatement}
  45.      * @param key
  46.      *            the key for this statement in the {@link KeyedObjectPool}
  47.      * @param pool
  48.      *            the {@link KeyedObjectPool} from which this CallableStatement was obtained
  49.      * @param connection
  50.      *            the {@link DelegatingConnection} that created this CallableStatement
  51.      */
  52.     public PoolableCallableStatement(final CallableStatement callableStatement, final PStmtKey key,
  53.             final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pool,
  54.             final DelegatingConnection<Connection> connection) {
  55.         super(connection, callableStatement);
  56.         this.pool = pool;
  57.         this.key = key;

  58.         // Remove from trace now because this statement will be
  59.         // added by the activate method.
  60.         removeThisTrace(connection);
  61.     }

  62.     /**
  63.      * Activates after retrieval from the pool. Adds a trace for this CallableStatement to the Connection that created
  64.      * it.
  65.      *
  66.      * @since 2.4.0 made public, was protected in 2.3.0.
  67.      */
  68.     @Override
  69.     public void activate() throws SQLException {
  70.         setClosedInternal(false);
  71.         add(getConnectionInternal(), this);
  72.         super.activate();
  73.     }

  74.     /**
  75.      * Returns the CallableStatement to the pool. If {{@link #isClosed()}, this is a No-op.
  76.      */
  77.     @Override
  78.     public void close() throws SQLException {
  79.         // calling close twice should have no effect
  80.         if (!isClosed()) {
  81.             try {
  82.                 pool.returnObject(key, this);
  83.             } catch (final SQLException | RuntimeException e) {
  84.                 throw e;
  85.             } catch (final Exception e) {
  86.                 throw new SQLException("Cannot close CallableStatement (return to pool failed)", e);
  87.             }
  88.         }
  89.     }

  90.     /**
  91.      * Passivates to prepare for return to the pool. Removes the trace associated with this CallableStatement from the
  92.      * Connection that created it. Also closes any associated ResultSets.
  93.      *
  94.      * @since 2.4.0 made public, was protected in 2.3.0.
  95.      */
  96.     @Override
  97.     public void passivate() throws SQLException {
  98.         prepareToReturn();
  99.     }

  100. }