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 */
017
018package org.apache.commons.dbcp2.managed;
019
020import java.sql.Connection;
021
022import javax.management.ObjectName;
023
024import org.apache.commons.dbcp2.Constants;
025import org.apache.commons.dbcp2.DelegatingPreparedStatement;
026import org.apache.commons.dbcp2.PStmtKey;
027import org.apache.commons.dbcp2.PoolableConnection;
028import org.apache.commons.dbcp2.PoolableConnectionFactory;
029import org.apache.commons.dbcp2.PoolingConnection;
030import org.apache.commons.pool2.KeyedObjectPool;
031import org.apache.commons.pool2.PooledObject;
032import org.apache.commons.pool2.impl.DefaultPooledObject;
033import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
034import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
035
036/**
037 * A {@link PoolableConnectionFactory} that creates {@link PoolableManagedConnection}s.
038 *
039 * @since 2.0
040 */
041public class PoolableManagedConnectionFactory extends PoolableConnectionFactory {
042
043    /** Transaction registry associated with connections created by this factory */
044    private final TransactionRegistry transactionRegistry;
045
046    /**
047     * Creates a PoolableManagedConnectionFactory and attach it to a connection pool.
048     *
049     * @param connFactory
050     *            XAConnectionFactory
051     * @param dataSourceJmxName
052     *            The data source name.
053     */
054    public PoolableManagedConnectionFactory(final XAConnectionFactory connFactory, final ObjectName dataSourceJmxName) {
055        super(connFactory, dataSourceJmxName);
056        this.transactionRegistry = connFactory.getTransactionRegistry();
057    }
058
059    /**
060     * @return The transaction registry.
061     * @since 2.6.0
062     */
063    public TransactionRegistry getTransactionRegistry() {
064        return transactionRegistry;
065    }
066
067    /**
068     * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}. Throws
069     * <code>IllegalStateException</code> if the connection factory returns null. Also initializes the connection using
070     * configured initialization SQL (if provided) and sets up a prepared statement pool associated with the
071     * PoolableManagedConnection if statement pooling is enabled.
072     */
073    @Override
074    public synchronized PooledObject<PoolableConnection> makeObject() throws Exception {
075        Connection conn = getConnectionFactory().createConnection();
076        if (conn == null) {
077            throw new IllegalStateException("Connection factory returned null from createConnection");
078        }
079        initializeConnection(conn);
080        if (getPoolStatements()) {
081            conn = new PoolingConnection(conn);
082            final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
083            config.setMaxTotalPerKey(-1);
084            config.setBlockWhenExhausted(false);
085            config.setMaxWaitMillis(0);
086            config.setMaxIdlePerKey(1);
087            config.setMaxTotal(getMaxOpenPreparedStatements());
088            final ObjectName dataSourceJmxName = getDataSourceJmxName();
089            final long connIndex = getConnectionIndex().getAndIncrement();
090            if (dataSourceJmxName != null) {
091                final StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
092                base.append(Constants.JMX_CONNECTION_BASE_EXT);
093                base.append(Long.toString(connIndex));
094                config.setJmxNameBase(base.toString());
095                config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
096            } else {
097                config.setJmxEnabled(false);
098            }
099            final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>(
100                    (PoolingConnection) conn, config);
101            ((PoolingConnection) conn).setStatementPool(stmtPool);
102            ((PoolingConnection) conn).setCacheState(getCacheState());
103        }
104        final PoolableManagedConnection pmc = new PoolableManagedConnection(transactionRegistry, conn, getPool(),
105                getDisconnectionSqlCodes(), isFastFailValidation());
106        pmc.setCacheState(getCacheState());
107        return new DefaultPooledObject<>(pmc);
108    }
109}