de.is24.util.monitoring.database
Class MonitoringDataSource

java.lang.Object
  extended by de.is24.util.monitoring.database.MonitoringDataSource
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource

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

Additionally, the returned Statements are wrapped and attempt to log the SQL being executed in case an exception is thrown.

If you want to use this Class, you need to require springframework core.

Author:
Sebastian Kirsch
See Also:
getConnection(), getConnection(String, String)

Nested Class Summary
private  class MonitoringDataSource.MonitoringConnection
          The MonitoringConnection is integral part of the MonitoringDataSource.
(package private) static class MonitoringDataSource.SqlExceptionPredicate
           
private static class MonitoringDataSource.SqlLoggingInvocationHandler
           
 
Field Summary
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 Summary
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.
 
Method Summary
 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOGGER

private static final org.apache.log4j.Logger LOGGER

original

private final DataSource original
The wrapped DataSource.


monitorBaseName

private final String monitorBaseName
The base name for all monitor values.


maxConnections

private final AtomicInteger maxConnections
This field is used to trace the maximum number of connections being use in parallel.


currentConnections

private final AtomicInteger currentConnections
This field is a helper to determine the correct for maxConnections.


loggingFilters

final Set<com.google.common.base.Predicate<SQLException>> loggingFilters
These predicates indicate if a the executed SQL should be logged at INFO level.

Constructor Detail

MonitoringDataSource

public MonitoringDataSource(DataSource dataSource,
                            String monitorBaseName)
Creates a new instance of MonitoringDataSource.

Parameters:
dataSource - the DataSource to wrap
monitorBaseName - the monitor name to insert such that "de.is24.common.database.MonitoringDataSource.monitorBaseName.monitoringKey" is reported
Throws:
IllegalArgumentException - if dataSource is null

MonitoringDataSource

public MonitoringDataSource(DataSource dataSource)
Creates a new instance of MonitoringDataSource without a specific monitoring base name.

Parameters:
dataSource - the DataSource to wrap
Throws:
IllegalArgumentException - if dataSource is null
Method Detail

addExceptionLogFilter

public void addExceptionLogFilter(com.google.common.base.Predicate<SQLException> predicate)

setExceptionLogFilters

public void setExceptionLogFilters(String configuration)

configureSqlExceptionPredicates

private void configureSqlExceptionPredicates(String configuration)

doConnectionMonitoring

private void doConnectionMonitoring(long startingInstant,
                                    String monitorSuffix)
Handles the monitoring for retrieving connections and adapts the max connection counter if appropriate.

Parameters:
startingInstant - the instant a database connection was requested
monitorSuffix - the suffix for the monitor name to increase

monitorFailedConnectionAttempt

private void monitorFailedConnectionAttempt()
Increases the error count for the getConnection method.


getConnection

private Connection getConnection(Callable<Connection> callable,
                                 String monitorSuffix)
                          throws SQLException
Executes the specified Callable to fetch a connection. Monitors occurring exceptions/errors.

Parameters:
callable - the Callable that actually fetches a Connection
monitorSuffix - the suffix for the monitor name to increase (forwarded to doConnectionMonitoring(long, String))
Returns:
a database Connection
Throws:
SQLException - if fetching a Connection fails
RuntimeException

toString

public String toString()
Returns a String representation of this object.

Overrides:
toString in class Object
Returns:
a String representing this instance

getConnection

public Connection getConnection()
                         throws SQLException

Delegates the call to the wrapped data source, wraps the returned connection and counts the connections returned.

Specified by:
getConnection in interface DataSource
Returns:
a database Connection
Throws:
SQLException - as a result of the delegation

getConnection

public 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.

Specified by:
getConnection in interface DataSource
Parameters:
username - the name of the database user on whose behalf the connection isbeing made
password - the user's password
Returns:
a database Connection
Throws:
SQLException - as a result of the delegation

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Specified by:
getLogWriter in interface CommonDataSource
Throws:
SQLException

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Specified by:
getLoginTimeout in interface CommonDataSource
Throws:
SQLException

getParentLogger

public Logger getParentLogger()
                       throws SQLFeatureNotSupportedException
Throws:
SQLFeatureNotSupportedException

setLogWriter

public void setLogWriter(PrintWriter out)
                  throws SQLException
Specified by:
setLogWriter in interface CommonDataSource
Throws:
SQLException

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws SQLException
Specified by:
setLoginTimeout in interface CommonDataSource
Throws:
SQLException

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException
Specified by:
isWrapperFor in interface Wrapper
Throws:
SQLException

unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException
Specified by:
unwrap in interface Wrapper
Throws:
SQLException

delegateViaReflectionDuringMigrationFromJava6ToJava7

private <T> T delegateViaReflectionDuringMigrationFromJava6ToJava7(String methodName,
                                                                   Object target,
                                                                   Class<T> expectedReturnType,
                                                                   Object... args)


Copyright © 2013 Immobilien Scout GmbH. All Rights Reserved.