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.util.Objects;
022
023import org.apache.commons.dbcp2.PoolingDataSource;
024import org.apache.commons.pool2.ObjectPool;
025
026/**
027 * The ManagedDataSource is a PoolingDataSource that creates ManagedConnections.
028 *
029 * @param <C>
030 *            The kind of {@link Connection} to manage.
031 * @since 2.0
032 */
033public class ManagedDataSource<C extends Connection> extends PoolingDataSource<C> {
034    private TransactionRegistry transactionRegistry;
035
036    /**
037     * Creates a ManagedDataSource which obtains connections from the specified pool and manages them using the
038     * specified transaction registry. The TransactionRegistry must be the transaction registry obtained from the
039     * XAConnectionFactory used to create the connection pool. If not, an error will occur when attempting to use the
040     * connection in a global transaction because the XAResource object associated with the connection will be
041     * unavailable.
042     *
043     * @param pool
044     *            the connection pool
045     * @param transactionRegistry
046     *            the transaction registry obtained from the XAConnectionFactory used to create the connection pool
047     *            object factory
048     */
049    public ManagedDataSource(final ObjectPool<C> pool, final TransactionRegistry transactionRegistry) {
050        super(pool);
051        this.transactionRegistry = transactionRegistry;
052    }
053
054    @Override
055    public Connection getConnection() throws SQLException {
056        if (getPool() == null) {
057            throw new IllegalStateException("Pool has not been set");
058        }
059        if (transactionRegistry == null) {
060            throw new IllegalStateException("TransactionRegistry has not been set");
061        }
062
063        return new ManagedConnection<>(getPool(), transactionRegistry, isAccessToUnderlyingConnectionAllowed());
064    }
065
066    /**
067     * Gets the transaction registry.
068     *
069     * @return The transaction registry.
070     * @see #setTransactionRegistry(TransactionRegistry)
071     * @since 2.6.0
072     */
073    public TransactionRegistry getTransactionRegistry() {
074        return transactionRegistry;
075    }
076
077    /**
078     * Sets the transaction registry from the XAConnectionFactory used to create the pool. The transaction registry can
079     * only be set once using either a connector or this setter method.
080     *
081     * @param transactionRegistry
082     *            the transaction registry acquired from the XAConnectionFactory used to create the pool
083     */
084    public void setTransactionRegistry(final TransactionRegistry transactionRegistry) {
085        if (this.transactionRegistry != null) {
086            throw new IllegalStateException("TransactionRegistry already set");
087        }
088        Objects.requireNonNull(transactionRegistry, "transactionRegistry");
089
090        this.transactionRegistry = transactionRegistry;
091    }
092}