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