PoolablePreparedStatement.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.tomcat.dbcp.dbcp2;

  18. import java.sql.PreparedStatement;
  19. import java.sql.SQLException;

  20. import org.apache.tomcat.dbcp.pool2.KeyedObjectPool;

  21. /**
  22.  * A {@link DelegatingPreparedStatement} that cooperates with {@link PoolingConnection} to implement a pool of
  23.  * {@link PreparedStatement}s.
  24.  * <p>
  25.  * My {@link #close} method returns me to my containing pool. (See {@link PoolingConnection}.)
  26.  * </p>
  27.  *
  28.  * @param <K>
  29.  *            the key type
  30.  *
  31.  * @see PoolingConnection
  32.  * @since 2.0
  33.  */
  34. public class PoolablePreparedStatement<K> extends DelegatingPreparedStatement {

  35.     /**
  36.      * The {@link KeyedObjectPool} from which I was obtained.
  37.      */
  38.     private final KeyedObjectPool<K, PoolablePreparedStatement<K>> pool;

  39.     /**
  40.      * My "key" as used by {@link KeyedObjectPool}.
  41.      */
  42.     private final K key;

  43.     private volatile boolean batchAdded;

  44.     /**
  45.      * Constructs a new instance.
  46.      *
  47.      * @param stmt
  48.      *            my underlying {@link PreparedStatement}
  49.      * @param key
  50.      *            my key as used by {@link KeyedObjectPool}
  51.      * @param pool
  52.      *            the {@link KeyedObjectPool} from which I was obtained.
  53.      * @param conn
  54.      *            the {@link java.sql.Connection Connection} from which I was created
  55.      */
  56.     public PoolablePreparedStatement(final PreparedStatement stmt, final K key,
  57.             final KeyedObjectPool<K, PoolablePreparedStatement<K>> pool, final DelegatingConnection<?> conn) {
  58.         super(conn, stmt);
  59.         this.pool = pool;
  60.         this.key = key;

  61.         // Remove from trace now because this statement will be
  62.         // added by the activate method.
  63.         removeThisTrace(conn);
  64.     }

  65.     @Override
  66.     public void activate() throws SQLException {
  67.         setClosedInternal(false);
  68.         add(getConnectionInternal(), this);
  69.         super.activate();
  70.     }

  71.     /**
  72.      * Add batch.
  73.      */
  74.     @Override
  75.     public void addBatch() throws SQLException {
  76.         super.addBatch();
  77.         batchAdded = true;
  78.     }

  79.     /**
  80.      * Clear Batch.
  81.      */
  82.     @Override
  83.     public void clearBatch() throws SQLException {
  84.         batchAdded = false;
  85.         super.clearBatch();
  86.     }

  87.     /**
  88.      * Return me to my pool.
  89.      */
  90.     @Override
  91.     public void close() throws SQLException {
  92.         // calling close twice should have no effect
  93.         if (!isClosed()) {
  94.             try {
  95.                 pool.returnObject(key, this);
  96.             } catch (final SQLException | RuntimeException e) {
  97.                 throw e;
  98.             } catch (final Exception e) {
  99.                 throw new SQLException("Cannot close preparedstatement (return to pool failed)", e);
  100.             }
  101.         }
  102.     }

  103.     /**
  104.      * Package-protected for tests.
  105.      *
  106.      * @return The key.
  107.      */
  108.     K getKey() {
  109.         return key;
  110.     }

  111.     @Override
  112.     public void passivate() throws SQLException {
  113.         // DBCP-372. clearBatch with throw an exception if called when the
  114.         // connection is marked as closed.
  115.         if (batchAdded) {
  116.             clearBatch();
  117.         }
  118.         prepareToReturn();
  119.     }
  120. }