1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.dbcp.dbcp2;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicLong;
import javax.management.ObjectName;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.dbcp.pool2.KeyedObjectPool;
import org.apache.tomcat.dbcp.pool2.ObjectPool;
import org.apache.tomcat.dbcp.pool2.PooledObject;
import org.apache.tomcat.dbcp.pool2.PooledObjectFactory;
import org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject;
import org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool;
import org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig;
/**
* A {@link PooledObjectFactory} that creates
* {@link PoolableConnection}s.
*
* @author Rodney Waldhoff
* @author Glenn L. Nielsen
* @author James House
* @author Dirk Verbeeck
* @since 2.0
*/
public class PoolableConnectionFactory
implements PooledObjectFactory<PoolableConnection> {
private static final Log log =
LogFactory.getLog(PoolableConnectionFactory.class);
/**
* Create a new {@code PoolableConnectionFactory}.
* @param connFactory the {@link ConnectionFactory} from which to obtain
* base {@link Connection}s
*/
public PoolableConnectionFactory(ConnectionFactory connFactory,
ObjectName dataSourceJmxName) {
_connFactory = connFactory;
this.dataSourceJmxName = dataSourceJmxName;
}
/**
* Sets the query I use to {@link #validateObject validate} {@link Connection}s.
* Should return at least one row. If not specified,
* {@link Connection#isValid(int)} will be used to validate connections.
*
* @param validationQuery a query to use to {@link #validateObject validate} {@link Connection}s.
*/
public void setValidationQuery(String validationQuery) {
_validationQuery = validationQuery;
}
/**
* Sets the validation query timeout, the amount of time, in seconds, that
* connection validation will wait for a response from the database when
* executing a validation query. Use a value less than or equal to 0 for
* no timeout.
*
* @param timeout new validation query timeout value in seconds
*/
public void setValidationQueryTimeout(int timeout) {
_validationQueryTimeout = timeout;
}
/**
* Sets the SQL statements I use to initialize newly created {@link Connection}s.
* Using {@code null} turns off connection initialization.
* @param connectionInitSqls SQL statement to initialize {@link Connection}s.
*/
public void setConnectionInitSql(Collection<String> connectionInitSqls) {
_connectionInitSqls = connectionInitSqls;
}
/**
* Sets the {@link ObjectPool} in which to pool {@link Connection}s.
* @param pool the {@link ObjectPool} in which to pool those {@link Connection}s
*/
public synchronized void setPool(ObjectPool<PoolableConnection> pool) {
if(null != _pool && pool != _pool) {
try {
_pool.close();
} catch(Exception e) {
// ignored !?!
}
}
_pool = pool;
}
/**
* Returns the {@link ObjectPool} in which {@link Connection}s are pooled.
* @return the connection pool
*/
public synchronized ObjectPool<PoolableConnection> getPool() {
return _pool;
}
/**
* Sets the default "read only" setting for borrowed {@link Connection}s
* @param defaultReadOnly the default "read only" setting for borrowed {@link Connection}s
*/
public void setDefaultReadOnly(Boolean defaultReadOnly) {
_defaultReadOnly = defaultReadOnly;
}
/**
* Sets the default "auto commit" setting for borrowed {@link Connection}s
* @param defaultAutoCommit the default "auto commit" setting for borrowed {@link Connection}s
*/
public void setDefaultAutoCommit(Boolean defaultAutoCommit) {
_defaultAutoCommit = defaultAutoCommit;
}
/**
* Sets the default "Transaction Isolation" setting for borrowed {@link Connection}s
* @param defaultTransactionIsolation the default "Transaction Isolation" setting for returned {@link Connection}s
*/
public void setDefaultTransactionIsolation(int defaultTransactionIsolation) {
_defaultTransactionIsolation = defaultTransactionIsolation;
}
/**
* Sets the default "catalog" setting for borrowed {@link Connection}s
* @param defaultCatalog the default "catalog" setting for borrowed {@link Connection}s
*/
public void setDefaultCatalog(String defaultCatalog) {
_defaultCatalog = defaultCatalog;
}
public void setCacheState(boolean cacheState) {
this._cacheState = cacheState;
}
public void setPoolStatements(boolean poolStatements) {
this.poolStatements = poolStatements;
}
public void setMaxOpenPrepatedStatements(int maxOpenPreparedStatements) {
this.maxOpenPreparedStatements = maxOpenPreparedStatements;
}
/**
* Sets the maximum lifetime in milliseconds of a connection after which the
* connection will always fail activation, passivation and validation. A
* value of zero or less indicates an infinite lifetime. The default value
* is -1.
*/
public void setMaxConnLifetimeMillis(long maxConnLifetimeMillis) {
this.maxConnLifetimeMillis = maxConnLifetimeMillis;
}
public boolean isEnableAutoCommitOnReturn() {
return enableAutoCommitOnReturn;
}
public void setEnableAutoCommitOnReturn(boolean enableAutoCommitOnReturn) {
this.enableAutoCommitOnReturn = enableAutoCommitOnReturn;
}
public boolean isRollbackOnReturn() {
return rollbackOnReturn;
}
public void setRollbackOnReturn(boolean rollbackOnReturn) {
this.rollbackOnReturn = rollbackOnReturn;
}
public Integer getDefaultQueryTimeout() {
return defaultQueryTimeout;
}
public void setDefaultQueryTimeout(Integer defaultQueryTimeout) {
this.defaultQueryTimeout = defaultQueryTimeout;
}
@Override
public PooledObject<PoolableConnection> makeObject() throws Exception {
Connection conn = _connFactory.createConnection();
if (conn == null) {
throw new IllegalStateException("Connection factory returned null from createConnection");
}
try {
initializeConnection(conn);
} catch (SQLException sqle) {
// Make sure the connection is closed
try {
conn.close();
} catch (SQLException ignore) {
// ignore
}
// Rethrow original exception so it is visible to caller
throw sqle;
}
long connIndex = connectionIndex.getAndIncrement();
if(poolStatements) {
conn = new PoolingConnection(conn);
GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
config.setMaxTotalPerKey(-1);
config.setBlockWhenExhausted(false);
config.setMaxWaitMillis(0);
config.setMaxIdlePerKey(1);
config.setMaxTotal(maxOpenPreparedStatements);
if (dataSourceJmxName != null) {
StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
base.append(Constants.JMX_CONNECTION_BASE_EXT);
base.append(Long.toString(connIndex));
config.setJmxNameBase(base.toString());
config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
}
KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> stmtPool =
new GenericKeyedObjectPool<>((PoolingConnection)conn, config);
((PoolingConnection)conn).setStatementPool(stmtPool);
((PoolingConnection) conn).setCacheState(_cacheState);
}
// Register this connection with JMX
ObjectName connJmxName;
if (dataSourceJmxName == null) {
connJmxName = null;
} else {
connJmxName = new ObjectName(dataSourceJmxName.toString() +
Constants.JMX_CONNECTION_BASE_EXT + connIndex);
}
PoolableConnection pc = new PoolableConnection(conn,_pool, connJmxName);
return new DefaultPooledObject<>(pc);
}
protected void initializeConnection(Connection conn) throws SQLException {
Collection<String> sqls = _connectionInitSqls;
if(conn.isClosed()) {
throw new SQLException("initializeConnection: connection closed");
}
if(null != sqls) {
try (Statement stmt = conn.createStatement();) {
for (String sql : sqls) {
if (sql == null) {
throw new NullPointerException(
"null connectionInitSqls element");
}
stmt.execute(sql);
}
}
}
}
@Override
public void destroyObject(PooledObject<PoolableConnection> p)
throws Exception {
p.getObject().reallyClose();
}
@Override
public boolean validateObject(PooledObject<PoolableConnection> p) {
try {
validateLifetime(p);
validateConnection(p.getObject());
return true;
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(Utils.getMessage(
"poolableConnectionFactory.validateObject.fail"), e);
}
return false;
}
}
public void validateConnection(PoolableConnection conn) throws SQLException {
if(conn.isClosed()) {
throw new SQLException("validateConnection: connection closed");
}
conn.validate(_validationQuery, _validationQueryTimeout);
}
@Override
public void passivateObject(PooledObject<PoolableConnection> p)
throws Exception {
validateLifetime(p);
PoolableConnection conn = p.getObject();
Boolean connAutoCommit = null;
if (rollbackOnReturn) {
connAutoCommit = Boolean.valueOf(conn.getAutoCommit());
if(!connAutoCommit.booleanValue() && !conn.isReadOnly()) {
conn.rollback();
}
}
conn.clearWarnings();
// DBCP-97 / DBCP-399 / DBCP-351 Idle connections in the pool should
// have autoCommit enabled
if (enableAutoCommitOnReturn) {
if (connAutoCommit == null) {
connAutoCommit = Boolean.valueOf(conn.getAutoCommit());
}
if(!connAutoCommit.booleanValue()) {
conn.setAutoCommit(true);
}
}
conn.passivate();
}
@Override
public void activateObject(PooledObject<PoolableConnection> p)
throws Exception {
validateLifetime(p);
PoolableConnection conn = p.getObject();
conn.activate();
if (_defaultAutoCommit != null &&
conn.getAutoCommit() != _defaultAutoCommit.booleanValue()) {
conn.setAutoCommit(_defaultAutoCommit.booleanValue());
}
if (_defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION &&
conn.getTransactionIsolation() != _defaultTransactionIsolation) {
conn.setTransactionIsolation(_defaultTransactionIsolation);
}
if (_defaultReadOnly != null &&
conn.isReadOnly() != _defaultReadOnly.booleanValue()) {
conn.setReadOnly(_defaultReadOnly.booleanValue());
}
if (_defaultCatalog != null &&
!_defaultCatalog.equals(conn.getCatalog())) {
conn.setCatalog(_defaultCatalog);
}
conn.setDefaultQueryTimeout(defaultQueryTimeout);
}
private void validateLifetime(PooledObject<PoolableConnection> p)
throws Exception {
if (maxConnLifetimeMillis > 0) {
long lifetime = System.currentTimeMillis() - p.getCreateTime();
if (lifetime > maxConnLifetimeMillis) {
throw new Exception(Utils.getMessage(
"connectionFactory.lifetimeExceeded",
Long.valueOf(lifetime),
Long.valueOf(maxConnLifetimeMillis)));
}
}
}
protected ConnectionFactory getConnectionFactory() {
return _connFactory;
}
protected boolean getPoolStatements() {
return poolStatements;
}
protected int getMaxOpenPreparedStatements() {
return maxOpenPreparedStatements;
}
protected boolean getCacheState() {
return _cacheState;
}
private final ConnectionFactory _connFactory;
private final ObjectName dataSourceJmxName;
private volatile String _validationQuery = null;
private volatile int _validationQueryTimeout = -1;
private Collection<String> _connectionInitSqls = null;
private volatile ObjectPool<PoolableConnection> _pool = null;
private Boolean _defaultReadOnly = null;
private Boolean _defaultAutoCommit = null;
private boolean enableAutoCommitOnReturn = true;
private boolean rollbackOnReturn = true;
private int _defaultTransactionIsolation = UNKNOWN_TRANSACTIONISOLATION;
private String _defaultCatalog;
private boolean _cacheState;
private boolean poolStatements = false;
private int maxOpenPreparedStatements =
GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY;
private long maxConnLifetimeMillis = -1;
private final AtomicLong connectionIndex = new AtomicLong(0);
private Integer defaultQueryTimeout = null;
/**
* Internal constant to indicate the level is not set.
*/
static final int UNKNOWN_TRANSACTIONISOLATION = -1;
}
|