001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.dbcp2.managed; 019import java.sql.Connection; 020 021import javax.management.ObjectName; 022 023import org.apache.commons.dbcp2.Constants; 024import org.apache.commons.dbcp2.DelegatingPreparedStatement; 025import org.apache.commons.dbcp2.PStmtKey; 026import org.apache.commons.dbcp2.PoolableConnection; 027import org.apache.commons.dbcp2.PoolableConnectionFactory; 028import org.apache.commons.dbcp2.PoolingConnection; 029import org.apache.commons.pool2.KeyedObjectPool; 030import org.apache.commons.pool2.PooledObject; 031import org.apache.commons.pool2.impl.GenericKeyedObjectPool; 032import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; 033import org.apache.commons.pool2.impl.DefaultPooledObject; 034 035/** 036 * A {@link PoolableConnectionFactory} that creates {@link PoolableManagedConnection}s. 037 * 038 * @since 2.0 039 */ 040public class PoolableManagedConnectionFactory extends PoolableConnectionFactory { 041 042 /** Transaction registry associated with connections created by this factory */ 043 private final TransactionRegistry transactionRegistry; 044 045 /** 046 * Create a PoolableManagedConnectionFactory and attach it to a connection pool. 047 * 048 * @param connFactory XAConnectionFactory 049 */ 050 public PoolableManagedConnectionFactory(final XAConnectionFactory connFactory, 051 final ObjectName dataSourceJmxName) { 052 super(connFactory, dataSourceJmxName); 053 this.transactionRegistry = connFactory.getTransactionRegistry(); 054 } 055 056 /** 057 * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}. 058 * Throws <code>IllegalStateException</code> if the connection factory returns null. 059 * Also initializes the connection using configured initialization sql (if provided) 060 * and sets up a prepared statement pool associated with the PoolableManagedConnection 061 * if statement pooling is enabled. 062 */ 063 @Override 064 synchronized public PooledObject<PoolableConnection> makeObject() throws Exception { 065 Connection conn = getConnectionFactory().createConnection(); 066 if (conn == null) { 067 throw new IllegalStateException("Connection factory returned null from createConnection"); 068 } 069 initializeConnection(conn); 070 if (getPoolStatements()) { 071 conn = new PoolingConnection(conn); 072 final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig(); 073 config.setMaxTotalPerKey(-1); 074 config.setBlockWhenExhausted(false); 075 config.setMaxWaitMillis(0); 076 config.setMaxIdlePerKey(1); 077 config.setMaxTotal(getMaxOpenPreparedStatements()); 078 final ObjectName dataSourceJmxName = getDataSourceJmxName(); 079 final long connIndex = getConnectionIndex().getAndIncrement(); 080 if (dataSourceJmxName != null) { 081 final StringBuilder base = new StringBuilder(dataSourceJmxName.toString()); 082 base.append(Constants.JMX_CONNECTION_BASE_EXT); 083 base.append(Long.toString(connIndex)); 084 config.setJmxNameBase(base.toString()); 085 config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX); 086 } else { 087 config.setJmxEnabled(false); 088 } 089 final KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> stmtPool = 090 new GenericKeyedObjectPool<>((PoolingConnection)conn, config); 091 ((PoolingConnection)conn).setStatementPool(stmtPool); 092 ((PoolingConnection) conn).setCacheState(getCacheState()); 093 } 094 final PoolableManagedConnection pmc = 095 new PoolableManagedConnection(transactionRegistry, conn, getPool(), 096 getDisconnectionSqlCodes(), isFastFailValidation()); 097 pmc.setCacheState(getCacheState()); 098 return new DefaultPooledObject<PoolableConnection>(pmc); 099 } 100}