public class MonitoringDataSource extends Object implements DataSource
The MonitoringDataSource wraps a given DataSource
and the subsequently returned Connections. The combination of
MonitoringDataSource and MonitoringDataSource.MonitoringConnection monitor
getConnection()) andgetConnection(String, String)
Additionally, the returned Statements are wrapped and attempt to log
the SQL being executed in case an exception is thrown.
getConnection(),
getConnection(String, String)| Modifier and Type | Class and Description |
|---|---|
private class |
MonitoringDataSource.MonitoringConnection
The
MonitoringConnection is integral part of the MonitoringDataSource. |
(package private) static class |
MonitoringDataSource.SqlExceptionPredicate |
private static class |
MonitoringDataSource.SqlLoggingInvocationHandler |
| Modifier and Type | Field and Description |
|---|---|
private AtomicInteger |
currentConnections
This field is a helper to determine the correct for
maxConnections. |
private static org.apache.log4j.Logger |
LOGGER |
(package private) Set<com.google.common.base.Predicate<SQLException>> |
loggingFilters
These predicates indicate if a the executed SQL should be logged at INFO level.
|
private AtomicInteger |
maxConnections
This field is used to trace the maximum number of connections being use in parallel.
|
private String |
monitorBaseName
The base name for all monitor values.
|
private DataSource |
original
The wrapped
DataSource. |
| Constructor and Description |
|---|
MonitoringDataSource(DataSource dataSource)
Creates a new instance of
MonitoringDataSource
without a specific monitoring base name. |
MonitoringDataSource(DataSource dataSource,
String monitorBaseName)
Creates a new instance of
MonitoringDataSource. |
| Modifier and Type | Method and Description |
|---|---|
void |
addExceptionLogFilter(com.google.common.base.Predicate<SQLException> predicate) |
private void |
configureSqlExceptionPredicates(String configuration) |
private <T> T |
delegateViaReflectionDuringMigrationFromJava6ToJava7(String methodName,
Object target,
Class<T> expectedReturnType,
Object... args) |
private void |
doConnectionMonitoring(long startingInstant,
String monitorSuffix)
Handles the monitoring for retrieving connections and adapts the max connection counter if appropriate.
|
Connection |
getConnection()
Delegates the call to the wrapped data source, wraps the returned
connection and counts the connections returned.
|
private Connection |
getConnection(Callable<Connection> callable,
String monitorSuffix)
Executes the specified
Callable to fetch a connection. |
Connection |
getConnection(String username,
String password)
Delegates the call to the wrapped data source, wraps the returned
connection and counts the connections returned.
|
int |
getLoginTimeout() |
PrintWriter |
getLogWriter() |
Logger |
getParentLogger() |
boolean |
isWrapperFor(Class<?> iface) |
private void |
monitorFailedConnectionAttempt()
Increases the error count for the getConnection method.
|
void |
setExceptionLogFilters(String configuration) |
void |
setLoginTimeout(int seconds) |
void |
setLogWriter(PrintWriter out) |
String |
toString()
Returns a String representation of this object.
|
<T> T |
unwrap(Class<T> iface) |
private static final org.apache.log4j.Logger LOGGER
private final DataSource original
DataSource.private final String monitorBaseName
private final AtomicInteger maxConnections
private final AtomicInteger currentConnections
maxConnections.final Set<com.google.common.base.Predicate<SQLException>> loggingFilters
public MonitoringDataSource(DataSource dataSource, String monitorBaseName)
MonitoringDataSource.dataSource - the DataSource to wrapmonitorBaseName - the monitor name to insert such that "de.is24.common.database.MonitoringDataSource.monitorBaseName.monitoringKey" is reportedIllegalArgumentException - if dataSource is nullpublic MonitoringDataSource(DataSource dataSource)
MonitoringDataSource
without a specific monitoring base name.dataSource - the DataSource to wrapIllegalArgumentException - if dataSource is nullpublic void addExceptionLogFilter(com.google.common.base.Predicate<SQLException> predicate)
public void setExceptionLogFilters(String configuration)
private void configureSqlExceptionPredicates(String configuration)
private void doConnectionMonitoring(long startingInstant,
String monitorSuffix)
startingInstant - the instant a database connection was requestedmonitorSuffix - the suffix for the monitor name to increaseprivate void monitorFailedConnectionAttempt()
private Connection getConnection(Callable<Connection> callable, String monitorSuffix) throws SQLException
Callable to fetch a connection.
Monitors occurring exceptions/errors.callable - the Callable that actually fetches a ConnectionmonitorSuffix - the suffix for the monitor name to increase (forwarded to doConnectionMonitoring(long, String))ConnectionSQLException - if fetching a Connection failsRuntimeExceptionpublic String toString()
public Connection getConnection() throws SQLException
Delegates the call to the wrapped data source, wraps the returned connection and counts the connections returned.
getConnection in interface DataSourceConnectionSQLException - as a result of the delegationpublic Connection getConnection(String username, String password) throws SQLException
Delegates the call to the wrapped data source, wraps the returned connection and counts the connections returned.
getConnection in interface DataSourceusername - the name of the database user on whose behalf the connection isbeing madepassword - the user's passwordConnectionSQLException - as a result of the delegationpublic PrintWriter getLogWriter() throws SQLException
getLogWriter in interface CommonDataSourceSQLExceptionpublic int getLoginTimeout()
throws SQLException
getLoginTimeout in interface CommonDataSourceSQLExceptionpublic Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger in interface CommonDataSourceSQLFeatureNotSupportedExceptionpublic void setLogWriter(PrintWriter out) throws SQLException
setLogWriter in interface CommonDataSourceSQLExceptionpublic void setLoginTimeout(int seconds)
throws SQLException
setLoginTimeout in interface CommonDataSourceSQLExceptionpublic boolean isWrapperFor(Class<?> iface) throws SQLException
isWrapperFor in interface WrapperSQLExceptionpublic <T> T unwrap(Class<T> iface) throws SQLException
unwrap in interface WrapperSQLExceptionCopyright © 2013 Immobilien Scout GmbH. All Rights Reserved.