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 */ 018 package org.apache.commons.dbcp.managed; 019 020 import org.apache.commons.pool.ObjectPool; 021 import org.apache.commons.dbcp.PoolingDataSource; 022 023 import java.sql.Connection; 024 import java.sql.SQLException; 025 026 /** 027 * The ManagedDataSource is a PoolingDataSource that creates ManagedConnections. 028 * 029 * @author Dain Sundstrom 030 * @version $Revision: 892307 $ 031 */ 032 public class ManagedDataSource extends PoolingDataSource { 033 private TransactionRegistry transactionRegistry; 034 035 /** 036 * Creates an uninitialized datasource. Before this data source can be used a pool and 037 * transaction registry must be set. 038 */ 039 public ManagedDataSource() { 040 } 041 042 /** 043 * Creates a ManagedDataSource which obtains connections from the specified pool and 044 * manages them using the specified transaction registry. The TransactionRegistry must 045 * be the transaction registry obtained from the XAConnectionFactory used to create 046 * the connection pool. If not an error will occure when attempting to use the connection 047 * in a global transaction because the XAResource object associated with the connection 048 * will be unavailable. 049 * 050 * @param pool the connection pool 051 * @param transactionRegistry the transaction registry obtained from the 052 * XAConnectionFactory used to create the connection pool object factory 053 */ 054 public ManagedDataSource(ObjectPool pool, TransactionRegistry transactionRegistry) { 055 super(pool); 056 this.transactionRegistry = transactionRegistry; 057 } 058 059 /** 060 * Sets the transaction registry from the XAConnectionFactory used to create the pool. 061 * The transaction registry can only be set once using either a connector or this setter 062 * method. 063 * @param transactionRegistry the transaction registry acquired from the XAConnectionFactory 064 * used to create the pool 065 */ 066 public void setTransactionRegistry(TransactionRegistry transactionRegistry) { 067 if(this.transactionRegistry != null) throw new IllegalStateException("TransactionRegistry already set"); 068 if(transactionRegistry == null) throw new NullPointerException("TransactionRegistry is null"); 069 070 this.transactionRegistry = transactionRegistry; 071 } 072 073 public Connection getConnection() throws SQLException { 074 if (_pool == null) throw new IllegalStateException("Pool has not been set"); 075 if (transactionRegistry == null) throw new IllegalStateException("TransactionRegistry has not been set"); 076 077 Connection connection = new ManagedConnection(_pool, transactionRegistry, isAccessToUnderlyingConnectionAllowed()); 078 return connection; 079 } 080 }