| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ManagedDataSource |
|
| 3.0;3 |
| 1 | /** | |
| 2 | * | |
| 3 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 4 | * contributor license agreements. See the NOTICE file distributed with | |
| 5 | * this work for additional information regarding copyright ownership. | |
| 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 7 | * (the "License"); you may not use this file except in compliance with | |
| 8 | * the License. You may obtain a copy of the License at | |
| 9 | * | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | * | |
| 12 | * Unless required by applicable law or agreed to in writing, software | |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 15 | * See the License for the specific language governing permissions and | |
| 16 | * limitations under the License. | |
| 17 | */ | |
| 18 | package org.apache.commons.dbcp.managed; | |
| 19 | ||
| 20 | import org.apache.commons.pool.ObjectPool; | |
| 21 | import org.apache.commons.dbcp.PoolingDataSource; | |
| 22 | ||
| 23 | import java.sql.Connection; | |
| 24 | import java.sql.SQLException; | |
| 25 | ||
| 26 | /** | |
| 27 | * The ManagedDataSource is a PoolingDataSource that creates ManagedConnections. | |
| 28 | * | |
| 29 | * @author Dain Sundstrom | |
| 30 | * @version $Revision$ | |
| 31 | */ | |
| 32 | public class ManagedDataSource extends PoolingDataSource { | |
| 33 | private TransactionRegistry transactionRegistry; | |
| 34 | ||
| 35 | /** | |
| 36 | * Creates an uninitialized datasource. Before this data source can be used a pool and | |
| 37 | * transaction registry must be set. | |
| 38 | */ | |
| 39 | 0 | public ManagedDataSource() { |
| 40 | 0 | } |
| 41 | ||
| 42 | /** | |
| 43 | * Creates a ManagedDataSource which obtains connections from the specified pool and | |
| 44 | * manages them using the specified transaction registry. The TransactionRegistry must | |
| 45 | * be the transaction registry obtained from the XAConnectionFactory used to create | |
| 46 | * the connection pool. If not an error will occure when attempting to use the connection | |
| 47 | * in a global transaction because the XAResource object associated with the connection | |
| 48 | * will be unavailable. | |
| 49 | * | |
| 50 | * @param pool the connection pool | |
| 51 | * @param transactionRegistry the transaction registry obtained from the | |
| 52 | * XAConnectionFactory used to create the connection pool object factory | |
| 53 | */ | |
| 54 | public ManagedDataSource(ObjectPool pool, TransactionRegistry transactionRegistry) { | |
| 55 | 206 | super(pool); |
| 56 | 206 | this.transactionRegistry = transactionRegistry; |
| 57 | 206 | } |
| 58 | ||
| 59 | /** | |
| 60 | * Sets the transaction registry from the XAConnectionFactory used to create the pool. | |
| 61 | * The transaction registry can only be set once using either a connector or this setter | |
| 62 | * method. | |
| 63 | * @param transactionRegistry the transaction registry acquired from the XAConnectionFactory | |
| 64 | * used to create the pool | |
| 65 | */ | |
| 66 | public void setTransactionRegistry(TransactionRegistry transactionRegistry) { | |
| 67 | 0 | if(this.transactionRegistry != null) throw new IllegalStateException("TransactionRegistry already set"); |
| 68 | 0 | if(transactionRegistry == null) throw new NullPointerException("TransactionRegistry is null"); |
| 69 | ||
| 70 | 0 | this.transactionRegistry = transactionRegistry; |
| 71 | 0 | } |
| 72 | ||
| 73 | public Connection getConnection() throws SQLException { | |
| 74 | 4548 | if (_pool == null) throw new IllegalStateException("Pool has not been set"); |
| 75 | 4548 | if (transactionRegistry == null) throw new IllegalStateException("TransactionRegistry has not been set"); |
| 76 | ||
| 77 | 4548 | Connection connection = new ManagedConnection(_pool, transactionRegistry, isAccessToUnderlyingConnectionAllowed()); |
| 78 | 4540 | return connection; |
| 79 | } | |
| 80 | } |