/* * 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;
/** * A {@link Driver} implementation that obtains {@link Connection}s from a registered {@link ObjectPool}. * * @since 2.0
*/ publicclass PoolingDriver implements Driver {
/** * PoolGuardConnectionWrapper is a Connection wrapper that makes sure a closed connection cannot be used anymore. * * @since 2.0
*/ privateclass PoolGuardConnectionWrapper extends DelegatingConnection<Connection> {
// version numbers protectedstaticfinalint MAJOR_VERSION = 1;
protectedstaticfinalint MINOR_VERSION = 0;
/** Controls access to the underlying connection */ privatefinalboolean accessToUnderlyingConnectionAllowed;
/** * Constructs a new driver with {@code accessToUnderlyingConnectionAllowed} enabled.
*/ public PoolingDriver() { this(true);
}
/** * For unit testing purposes. * * @param accessToUnderlyingConnectionAllowed * Do {@link DelegatingConnection}s created by this driver permit access to the delegate?
*/ protected PoolingDriver(finalboolean accessToUnderlyingConnectionAllowed) { this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed;
}
/** * Closes a named pool. * * @param name * The pool name. * @throws SQLException * Thrown when a problem is caught closing the pool.
*/ publicsynchronizedvoid closePool(final String name) throws SQLException { final ObjectPool<? extends Connection> pool = pools.get(name); if (pool != null) {
pools.remove(name); try {
pool.close();
} catch (final Exception e) { thrownew SQLException("Error closing pool " + name, e);
}
}
}
@Override public Connection connect(final String url, final Properties info) throws SQLException { if (acceptsURL(url)) { final ObjectPool<? extends Connection> pool = getConnectionPool(url.substring(URL_PREFIX_LEN));
try { final Connection conn = pool.borrowObject(); if (conn == null) { returnnull;
} returnnew PoolGuardConnectionWrapper(pool, conn);
} catch (final NoSuchElementException e) { thrownew SQLException("Cannot get a connection, pool error: " + e.getMessage(), e);
} catch (final SQLException | RuntimeException e) { throw e;
} catch (final Exception e) { thrownew SQLException("Cannot get a connection, general error: " + e.getMessage(), e);
}
} returnnull;
}
/** * Gets the connection pool for the given name. * * @param name * The pool name * @return The pool * @throws SQLException * Thrown when the named pool is not registered.
*/ publicsynchronized ObjectPool<? extends Connection> getConnectionPool(final String name) throws SQLException { final ObjectPool<? extends Connection> pool = pools.get(name); if (null == pool) { thrownew SQLException("Pool not registered: " + name);
} return pool;
}
@Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { thrownew SQLFeatureNotSupportedException();
}
/** * Gets the pool names. * * @return the pool names.
*/ publicsynchronized String[] getPoolNames() { return pools.keySet().toArray(Utils.EMPTY_STRING_ARRAY);
}
@Override public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) { return EMPTY_DRIVER_PROPERTY_INFO_ARRAY;
} /** * Invalidates the given connection. * * @param conn * connection to invalidate * @throws SQLException * if the connection is not a {@code PoolGuardConnectionWrapper} or an error occurs invalidating * the connection
*/ publicvoid invalidateConnection(final Connection conn) throws SQLException { if (!(conn instanceof PoolGuardConnectionWrapper)) { thrownew SQLException("Invalid connection class");
} final PoolGuardConnectionWrapper pgconn = (PoolGuardConnectionWrapper) conn;
@SuppressWarnings("unchecked") final ObjectPool<Connection> pool = (ObjectPool<Connection>) pgconn.pool; try {
pool.invalidateObject(pgconn.getDelegateInternal());
} catch (final Exception ignored) { // Ignored.
}
}
/** * Returns the value of the accessToUnderlyingConnectionAllowed property. * * @return true if access to the underlying is allowed, false otherwise.
*/ protectedboolean isAccessToUnderlyingConnectionAllowed() { return accessToUnderlyingConnectionAllowed;
}
/** * Registers a named pool. * * @param name * The pool name. * @param pool * The pool.
*/ publicsynchronizedvoid registerPool(final String name, final ObjectPool<? extends Connection> pool) {
pools.put(name, pool);
}
}
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.