PoolableManagedConnectionFactory.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.managed;

  18. import java.sql.Connection;
  19. import java.sql.SQLException;
  20. import java.time.Duration;

  21. import javax.management.ObjectName;

  22. import org.apache.commons.dbcp2.Constants;
  23. import org.apache.commons.dbcp2.DelegatingPreparedStatement;
  24. import org.apache.commons.dbcp2.PStmtKey;
  25. import org.apache.commons.dbcp2.PoolableConnection;
  26. import org.apache.commons.dbcp2.PoolableConnectionFactory;
  27. import org.apache.commons.dbcp2.PoolingConnection;
  28. import org.apache.commons.pool2.KeyedObjectPool;
  29. import org.apache.commons.pool2.PooledObject;
  30. import org.apache.commons.pool2.impl.DefaultPooledObject;
  31. import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
  32. import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;

  33. /**
  34.  * A {@link PoolableConnectionFactory} that creates {@link PoolableManagedConnection}s.
  35.  *
  36.  * @since 2.0
  37.  */
  38. public class PoolableManagedConnectionFactory extends PoolableConnectionFactory {

  39.     /** Transaction registry associated with connections created by this factory */
  40.     private final TransactionRegistry transactionRegistry;

  41.     /**
  42.      * Creates a PoolableManagedConnectionFactory and attach it to a connection pool.
  43.      *
  44.      * @param connFactory
  45.      *            XAConnectionFactory
  46.      * @param dataSourceJmxName
  47.      *            The data source name.
  48.      */
  49.     public PoolableManagedConnectionFactory(final XAConnectionFactory connFactory, final ObjectName dataSourceJmxName) {
  50.         super(connFactory, dataSourceJmxName);
  51.         this.transactionRegistry = connFactory.getTransactionRegistry();
  52.     }

  53.     /**
  54.      * Gets the transaction registry.
  55.      *
  56.      * @return The transaction registry.
  57.      * @since 2.6.0
  58.      */
  59.     public TransactionRegistry getTransactionRegistry() {
  60.         return transactionRegistry;
  61.     }

  62.     /**
  63.      * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}. Throws
  64.      * {@code IllegalStateException} if the connection factory returns null. Also initializes the connection using
  65.      * configured initialization SQL (if provided) and sets up a prepared statement pool associated with the
  66.      * PoolableManagedConnection if statement pooling is enabled.
  67.      */
  68.     @SuppressWarnings("resource") // Connection is released elsewhere.
  69.     @Override
  70.     public synchronized PooledObject<PoolableConnection> makeObject() throws SQLException {
  71.         Connection conn = getConnectionFactory().createConnection();
  72.         if (conn == null) {
  73.             throw new IllegalStateException("Connection factory returned null from createConnection");
  74.         }
  75.         initializeConnection(conn);
  76.         if (getPoolStatements()) {
  77.             conn = new PoolingConnection(conn);
  78.             final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
  79.             config.setMaxTotalPerKey(-1);
  80.             config.setBlockWhenExhausted(false);
  81.             config.setMaxWait(Duration.ZERO);
  82.             config.setMaxIdlePerKey(1);
  83.             config.setMaxTotal(getMaxOpenPreparedStatements());
  84.             final ObjectName dataSourceJmxName = getDataSourceJmxName();
  85.             final long connIndex = getConnectionIndex().getAndIncrement();
  86.             if (dataSourceJmxName != null) {
  87.                 final StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
  88.                 base.append(Constants.JMX_CONNECTION_BASE_EXT);
  89.                 base.append(connIndex);
  90.                 config.setJmxNameBase(base.toString());
  91.                 config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
  92.             } else {
  93.                 config.setJmxEnabled(false);
  94.             }
  95.             final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>(
  96.                     (PoolingConnection) conn, config);
  97.             ((PoolingConnection) conn).setStatementPool(stmtPool);
  98.             ((PoolingConnection) conn).setCacheState(getCacheState());
  99.         }
  100.         final PoolableManagedConnection pmc = new PoolableManagedConnection(transactionRegistry, conn, getPool(),
  101.                 getDisconnectionSqlCodes(), getDisconnectionIgnoreSqlCodes(), isFastFailValidation());
  102.         pmc.setCacheState(getCacheState());
  103.         return new DefaultPooledObject<>(pmc);
  104.     }
  105. }