View Javadoc

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  
18  package org.apache.commons.dbcp.managed;
19  import java.sql.Connection;
20  import java.util.Collection;
21  
22  import org.apache.commons.dbcp.AbandonedConfig;
23  import org.apache.commons.dbcp.PoolableConnectionFactory;
24  import org.apache.commons.dbcp.PoolingConnection;
25  import org.apache.commons.pool.KeyedObjectPool;
26  import org.apache.commons.pool.KeyedObjectPoolFactory;
27  import org.apache.commons.pool.ObjectPool;
28  
29  /**
30   * A {@link PoolableConnectionFactory} that creates {@link PoolableManagedConnection}s.
31   * 
32   * @version $Revision$ $Date$
33   */
34  public class PoolableManagedConnectionFactory extends PoolableConnectionFactory {
35  
36      /** Transaction registry associated with connections created by this factory */
37      private final TransactionRegistry transactionRegistry;
38      
39      /**
40       * Create a PoolableManagedConnectionFactory and attach it to a connection pool.
41       * 
42       * @param connFactory XAConnectionFactory
43       * @param pool connection pool 
44       * @param stmtPoolFactory the {@link KeyedObjectPoolFactory} to use to create {@link KeyedObjectPool}s for pooling
45       * {@link java.sql.PreparedStatement}s, or <tt>null</tt> to disable {@link java.sql.PreparedStatement} pooling
46       * @param validationQuery a query to use to {@link #validateObject validate} {@link Connection}s.
47       * Should return at least one row. Using <tt>null</tt> turns off validation.
48       * @param defaultReadOnly the default "read only" setting for borrowed {@link Connection}s
49       * @param defaultAutoCommit the default "auto commit" setting for returned {@link Connection}s
50       */
51      public PoolableManagedConnectionFactory(XAConnectionFactory connFactory,
52              ObjectPool pool, KeyedObjectPoolFactory stmtPoolFactory,
53              String validationQuery, boolean defaultReadOnly,
54              boolean defaultAutoCommit) {
55          super(connFactory, pool, stmtPoolFactory, validationQuery,
56                  defaultReadOnly, defaultAutoCommit);
57          this.transactionRegistry = connFactory.getTransactionRegistry();
58      }
59      
60      /**
61       * Create a PoolableManagedConnectionFactory and attach it to a connection pool.
62       * 
63       * @param connFactory XAConnectionFactory
64       * @param pool connection pool 
65       * @param stmtPoolFactory the {@link KeyedObjectPoolFactory} to use to create {@link KeyedObjectPool}s for pooling
66       * {@link java.sql.PreparedStatement}s, or <tt>null</tt> to disable {@link java.sql.PreparedStatement} pooling
67       * @param validationQuery a query to use to {@link #validateObject validate} {@link Connection}s.
68       * Should return at least one row. Using <tt>null</tt> turns off validation.
69       * @param validationQueryTimeout the number of seconds that validation queries will wait for database response
70       * before failing. Use a value less than or equal to 0 for no timeout.
71       * @param connectionInitSqls a Collection of SQL statements to initialize {@link Connection}s.
72       * Using <tt>null</tt> turns off initialization.
73       * @param defaultReadOnly the default "read only" setting for borrowed {@link Connection}s
74       * @param defaultAutoCommit the default "auto commit" setting for returned {@link Connection}s
75       * @param defaultTransactionIsolation the default "Transaction Isolation" setting for returned {@link Connection}s
76       * @param defaultCatalog the default "catalog" setting for returned {@link Connection}s
77       * @param config the AbandonedConfig if tracing SQL objects
78       */
79      public PoolableManagedConnectionFactory(XAConnectionFactory connFactory,
80              ObjectPool pool,
81              KeyedObjectPoolFactory stmtPoolFactory,
82              String validationQuery,
83              int validationQueryTimeout,
84              Collection connectionInitSqls,
85              Boolean defaultReadOnly,
86              boolean defaultAutoCommit,
87              int defaultTransactionIsolation,
88              String defaultCatalog,
89              AbandonedConfig config) {
90          super(connFactory, pool, stmtPoolFactory, validationQuery, validationQueryTimeout, connectionInitSqls,
91                  defaultReadOnly, defaultAutoCommit, defaultTransactionIsolation, defaultCatalog, config);
92          this.transactionRegistry = connFactory.getTransactionRegistry();
93      }
94  
95      /**
96       * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}.
97       * Throws <code>IllegalStateException</code> if the connection factory returns null.
98       * Also initializes the connection using configured initialization sql (if provided)
99       * and sets up a prepared statement pool associated with the PoolableManagedConnection
100      * if statement pooling is enabled.
101      */
102     synchronized public Object makeObject() throws Exception {
103         Connection conn = _connFactory.createConnection();
104         if (conn == null) {
105             throw new IllegalStateException("Connection factory returned null from createConnection");
106         }
107         initializeConnection(conn);
108         if(null != _stmtPoolFactory) {
109             KeyedObjectPool stmtpool = _stmtPoolFactory.createPool();
110             conn = new PoolingConnection(conn,stmtpool);
111             stmtpool.setFactory((PoolingConnection)conn);
112         }
113         return new PoolableManagedConnection(transactionRegistry,conn,_pool,_config);
114     }
115 
116 }