001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing, software
013 *  distributed under the License is distributed on an "AS IS" BASIS,
014 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 *  See the License for the specific language governing permissions and
016 *  limitations under the License.
017 */
018package org.apache.commons.dbcp2.managed;
019
020import org.apache.commons.pool2.ObjectPool;
021import org.apache.commons.dbcp2.PoolingDataSource;
022
023import java.sql.Connection;
024import java.sql.SQLException;
025
026/**
027 * The ManagedDataSource is a PoolingDataSource that creates ManagedConnections.
028 *
029 * @author Dain Sundstrom
030 * @param <C> 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
038     * manages them using the specified transaction registry.  The TransactionRegistry must
039     * be the transaction registry obtained from the XAConnectionFactory used to create
040     * the connection pool.  If not an error will occure when attempting to use the connection
041     * in a global transaction because the XAResource object associated with the connection
042     * will be unavailable.
043     *
044     * @param pool the connection pool
045     * @param transactionRegistry the transaction registry obtained from the
046     * XAConnectionFactory used to create the connection pool object factory
047     */
048    public ManagedDataSource(ObjectPool<C> pool,
049            TransactionRegistry transactionRegistry) {
050        super(pool);
051        this.transactionRegistry = transactionRegistry;
052    }
053
054    /**
055     * Sets the transaction registry from the XAConnectionFactory used to create the pool.
056     * The transaction registry can only be set once using either a connector or this setter
057     * method.
058     * @param transactionRegistry the transaction registry acquired from the XAConnectionFactory
059     * used to create the pool
060     */
061    public void setTransactionRegistry(TransactionRegistry transactionRegistry) {
062        if(this.transactionRegistry != null) {
063            throw new IllegalStateException("TransactionRegistry already set");
064        }
065        if(transactionRegistry == null) {
066            throw new NullPointerException("TransactionRegistry is null");
067        }
068
069        this.transactionRegistry = transactionRegistry;
070    }
071
072    @Override
073    public Connection getConnection() throws SQLException {
074        if (getPool() == null) {
075            throw new IllegalStateException("Pool has not been set");
076        }
077        if (transactionRegistry == null) {
078            throw new IllegalStateException("TransactionRegistry has not been set");
079        }
080
081        Connection connection = new ManagedConnection<>(getPool(), transactionRegistry, isAccessToUnderlyingConnectionAllowed());
082        return connection;
083    }
084}