001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.dbcp2.managed;
018
019import java.sql.Connection;
020import java.sql.SQLException;
021import java.time.Duration;
022
023import javax.management.ObjectName;
024
025import org.apache.commons.dbcp2.Constants;
026import org.apache.commons.dbcp2.DelegatingPreparedStatement;
027import org.apache.commons.dbcp2.PStmtKey;
028import org.apache.commons.dbcp2.PoolableConnection;
029import org.apache.commons.dbcp2.PoolableConnectionFactory;
030import org.apache.commons.dbcp2.PoolingConnection;
031import org.apache.commons.pool2.KeyedObjectPool;
032import org.apache.commons.pool2.PooledObject;
033import org.apache.commons.pool2.impl.DefaultPooledObject;
034import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
035import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
036
037/**
038 * A {@link PoolableConnectionFactory} that creates {@link PoolableManagedConnection}s.
039 *
040 * @since 2.0
041 */
042public class PoolableManagedConnectionFactory extends PoolableConnectionFactory {
043
044    /** Transaction registry associated with connections created by this factory */
045    private final TransactionRegistry transactionRegistry;
046
047    /**
048     * Creates a PoolableManagedConnectionFactory and attach it to a connection pool.
049     *
050     * @param connFactory
051     *            XAConnectionFactory
052     * @param dataSourceJmxName
053     *            The data source name.
054     */
055    public PoolableManagedConnectionFactory(final XAConnectionFactory connFactory, final ObjectName dataSourceJmxName) {
056        super(connFactory, dataSourceJmxName);
057        this.transactionRegistry = connFactory.getTransactionRegistry();
058    }
059
060    /**
061     * @return The transaction registry.
062     * @since 2.6.0
063     */
064    public TransactionRegistry getTransactionRegistry() {
065        return transactionRegistry;
066    }
067
068    /**
069     * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}. Throws
070     * {@code IllegalStateException} if the connection factory returns null. Also initializes the connection using
071     * configured initialization SQL (if provided) and sets up a prepared statement pool associated with the
072     * PoolableManagedConnection if statement pooling is enabled.
073     */
074    @SuppressWarnings("resource") // Connection is released elsewhere.
075    @Override
076    public synchronized PooledObject<PoolableConnection> makeObject() throws SQLException {
077        Connection conn = getConnectionFactory().createConnection();
078        if (conn == null) {
079            throw new IllegalStateException("Connection factory returned null from createConnection");
080        }
081        initializeConnection(conn);
082        if (getPoolStatements()) {
083            conn = new PoolingConnection(conn);
084            final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
085            config.setMaxTotalPerKey(-1);
086            config.setBlockWhenExhausted(false);
087            config.setMaxWait(Duration.ZERO);
088            config.setMaxIdlePerKey(1);
089            config.setMaxTotal(getMaxOpenPreparedStatements());
090            final ObjectName dataSourceJmxName = getDataSourceJmxName();
091            final long connIndex = getConnectionIndex().getAndIncrement();
092            if (dataSourceJmxName != null) {
093                final StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
094                base.append(Constants.JMX_CONNECTION_BASE_EXT);
095                base.append(connIndex);
096                config.setJmxNameBase(base.toString());
097                config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
098            } else {
099                config.setJmxEnabled(false);
100            }
101            final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>(
102                    (PoolingConnection) conn, config);
103            ((PoolingConnection) conn).setStatementPool(stmtPool);
104            ((PoolingConnection) conn).setCacheState(getCacheState());
105        }
106        final PoolableManagedConnection pmc = new PoolableManagedConnection(transactionRegistry, conn, getPool(),
107                getDisconnectionSqlCodes(), isFastFailValidation());
108        pmc.setCacheState(getCacheState());
109        return new DefaultPooledObject<>(pmc);
110    }
111}