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 * @version $Id: ManagedDataSource.java 1650024 2015-01-07 09:49:58Z tn $ 032 * @since 2.0 033 */ 034public class ManagedDataSource<C extends Connection> extends PoolingDataSource<C> { 035 private TransactionRegistry transactionRegistry; 036 037 /** 038 * Creates a ManagedDataSource which obtains connections from the specified pool and 039 * manages them using the specified transaction registry. The TransactionRegistry must 040 * be the transaction registry obtained from the XAConnectionFactory used to create 041 * the connection pool. If not, an error will occur when attempting to use the connection 042 * in a global transaction because the XAResource object associated with the connection 043 * will be unavailable. 044 * 045 * @param pool the connection pool 046 * @param transactionRegistry the transaction registry obtained from the 047 * XAConnectionFactory used to create the connection pool object factory 048 */ 049 public ManagedDataSource(ObjectPool<C> pool, 050 TransactionRegistry transactionRegistry) { 051 super(pool); 052 this.transactionRegistry = transactionRegistry; 053 } 054 055 /** 056 * Sets the transaction registry from the XAConnectionFactory used to create the pool. 057 * The transaction registry can only be set once using either a connector or this setter 058 * method. 059 * @param transactionRegistry the transaction registry acquired from the XAConnectionFactory 060 * used to create the pool 061 */ 062 public void setTransactionRegistry(TransactionRegistry transactionRegistry) { 063 if(this.transactionRegistry != null) { 064 throw new IllegalStateException("TransactionRegistry already set"); 065 } 066 if(transactionRegistry == null) { 067 throw new NullPointerException("TransactionRegistry is null"); 068 } 069 070 this.transactionRegistry = transactionRegistry; 071 } 072 073 @Override 074 public Connection getConnection() throws SQLException { 075 if (getPool() == null) { 076 throw new IllegalStateException("Pool has not been set"); 077 } 078 if (transactionRegistry == null) { 079 throw new IllegalStateException("TransactionRegistry has not been set"); 080 } 081 082 Connection connection = new ManagedConnection<>(getPool(), transactionRegistry, isAccessToUnderlyingConnectionAllowed()); 083 return connection; 084 } 085}