Author: nicolas Date: Fri Apr 3 08:45:56 2009 New Revision: 761572 URL: http://svn.apache.org/viewvc?rev=761572&view=rev Log: JDBC monitoring (experimental)
Added: commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/ commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredCallableStatement.java commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredConnection.java commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredDataSource.java commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredPreparedStatement.java commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredStatement.java Added: commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredCallableStatement.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredCallableStatement.java?rev=761572&view=auto ============================================================================== --- commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredCallableStatement.java (added) +++ commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredCallableStatement.java Fri Apr 3 08:45:56 2009 @@ -0,0 +1,519 @@ +package org.apache.commons.monitoring.instrumentation.jdbc; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Date; +import java.sql.Ref; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Map; + +import org.apache.commons.monitoring.Repository; + +/** + * @author ndeloof + * + */ +public class MonitoredCallableStatement + extends MonitoredPreparedStatement + implements CallableStatement +{ + private CallableStatement statement; + + /** + * @param statement + * @param sql + * @param repository + */ + public MonitoredCallableStatement( CallableStatement statement, String sql, Repository repository ) + { + super( statement, sql, repository ); + this.statement = statement; + } + + // --- delegate methods --- + + public Array getArray( int i ) + throws SQLException + { + return statement.getArray( i ); + } + + public Array getArray( String parameterName ) + throws SQLException + { + return statement.getArray( parameterName ); + } + + @Deprecated + public BigDecimal getBigDecimal( int parameterIndex, int scale ) + throws SQLException + { + return statement.getBigDecimal( parameterIndex, scale ); + } + + public BigDecimal getBigDecimal( int parameterIndex ) + throws SQLException + { + return statement.getBigDecimal( parameterIndex ); + } + + public BigDecimal getBigDecimal( String parameterName ) + throws SQLException + { + return statement.getBigDecimal( parameterName ); + } + + public Blob getBlob( int i ) + throws SQLException + { + return statement.getBlob( i ); + } + + public Blob getBlob( String parameterName ) + throws SQLException + { + return statement.getBlob( parameterName ); + } + + public boolean getBoolean( int parameterIndex ) + throws SQLException + { + return statement.getBoolean( parameterIndex ); + } + + public boolean getBoolean( String parameterName ) + throws SQLException + { + return statement.getBoolean( parameterName ); + } + + public byte getByte( int parameterIndex ) + throws SQLException + { + return statement.getByte( parameterIndex ); + } + + public byte getByte( String parameterName ) + throws SQLException + { + return statement.getByte( parameterName ); + } + + public byte[] getBytes( int parameterIndex ) + throws SQLException + { + return statement.getBytes( parameterIndex ); + } + + public byte[] getBytes( String parameterName ) + throws SQLException + { + return statement.getBytes( parameterName ); + } + + public Clob getClob( int i ) + throws SQLException + { + return statement.getClob( i ); + } + + public Clob getClob( String parameterName ) + throws SQLException + { + return statement.getClob( parameterName ); + } + + public Date getDate( int parameterIndex, Calendar cal ) + throws SQLException + { + return statement.getDate( parameterIndex, cal ); + } + + public Date getDate( int parameterIndex ) + throws SQLException + { + return statement.getDate( parameterIndex ); + } + + public Date getDate( String parameterName, Calendar cal ) + throws SQLException + { + return statement.getDate( parameterName, cal ); + } + + public Date getDate( String parameterName ) + throws SQLException + { + return statement.getDate( parameterName ); + } + + public double getDouble( int parameterIndex ) + throws SQLException + { + return statement.getDouble( parameterIndex ); + } + + public double getDouble( String parameterName ) + throws SQLException + { + return statement.getDouble( parameterName ); + } + + public float getFloat( int parameterIndex ) + throws SQLException + { + return statement.getFloat( parameterIndex ); + } + + public float getFloat( String parameterName ) + throws SQLException + { + return statement.getFloat( parameterName ); + } + + public int getInt( int parameterIndex ) + throws SQLException + { + return statement.getInt( parameterIndex ); + } + + public int getInt( String parameterName ) + throws SQLException + { + return statement.getInt( parameterName ); + } + + public long getLong( int parameterIndex ) + throws SQLException + { + return statement.getLong( parameterIndex ); + } + + public long getLong( String parameterName ) + throws SQLException + { + return statement.getLong( parameterName ); + } + + public Object getObject( int i, Map<String, Class<?>> map ) + throws SQLException + { + return statement.getObject( i, map ); + } + + public Object getObject( int parameterIndex ) + throws SQLException + { + return statement.getObject( parameterIndex ); + } + + public Object getObject( String parameterName, Map<String, Class<?>> map ) + throws SQLException + { + return statement.getObject( parameterName, map ); + } + + public Object getObject( String parameterName ) + throws SQLException + { + return statement.getObject( parameterName ); + } + + public Ref getRef( int i ) + throws SQLException + { + return statement.getRef( i ); + } + + public Ref getRef( String parameterName ) + throws SQLException + { + return statement.getRef( parameterName ); + } + + public short getShort( int parameterIndex ) + throws SQLException + { + return statement.getShort( parameterIndex ); + } + + public short getShort( String parameterName ) + throws SQLException + { + return statement.getShort( parameterName ); + } + + public String getString( int parameterIndex ) + throws SQLException + { + return statement.getString( parameterIndex ); + } + + public String getString( String parameterName ) + throws SQLException + { + return statement.getString( parameterName ); + } + + public Time getTime( int parameterIndex, Calendar cal ) + throws SQLException + { + return statement.getTime( parameterIndex, cal ); + } + + public Time getTime( int parameterIndex ) + throws SQLException + { + return statement.getTime( parameterIndex ); + } + + public Time getTime( String parameterName, Calendar cal ) + throws SQLException + { + return statement.getTime( parameterName, cal ); + } + + public Time getTime( String parameterName ) + throws SQLException + { + return statement.getTime( parameterName ); + } + + public Timestamp getTimestamp( int parameterIndex, Calendar cal ) + throws SQLException + { + return statement.getTimestamp( parameterIndex, cal ); + } + + public Timestamp getTimestamp( int parameterIndex ) + throws SQLException + { + return statement.getTimestamp( parameterIndex ); + } + + public Timestamp getTimestamp( String parameterName, Calendar cal ) + throws SQLException + { + return statement.getTimestamp( parameterName, cal ); + } + + public Timestamp getTimestamp( String parameterName ) + throws SQLException + { + return statement.getTimestamp( parameterName ); + } + + public URL getURL( int parameterIndex ) + throws SQLException + { + return statement.getURL( parameterIndex ); + } + + public URL getURL( String parameterName ) + throws SQLException + { + return statement.getURL( parameterName ); + } + + public void registerOutParameter( int parameterIndex, int sqlType, int scale ) + throws SQLException + { + statement.registerOutParameter( parameterIndex, sqlType, scale ); + } + + public void registerOutParameter( int paramIndex, int sqlType, String typeName ) + throws SQLException + { + statement.registerOutParameter( paramIndex, sqlType, typeName ); + } + + public void registerOutParameter( int parameterIndex, int sqlType ) + throws SQLException + { + statement.registerOutParameter( parameterIndex, sqlType ); + } + + public void registerOutParameter( String parameterName, int sqlType, int scale ) + throws SQLException + { + statement.registerOutParameter( parameterName, sqlType, scale ); + } + + public void registerOutParameter( String parameterName, int sqlType, String typeName ) + throws SQLException + { + statement.registerOutParameter( parameterName, sqlType, typeName ); + } + + public void registerOutParameter( String parameterName, int sqlType ) + throws SQLException + { + statement.registerOutParameter( parameterName, sqlType ); + } + + public void setAsciiStream( String parameterName, InputStream x, int length ) + throws SQLException + { + statement.setAsciiStream( parameterName, x, length ); + } + + public void setBigDecimal( String parameterName, BigDecimal x ) + throws SQLException + { + statement.setBigDecimal( parameterName, x ); + } + + public void setBinaryStream( String parameterName, InputStream x, int length ) + throws SQLException + { + statement.setBinaryStream( parameterName, x, length ); + } + + public void setBoolean( String parameterName, boolean x ) + throws SQLException + { + statement.setBoolean( parameterName, x ); + } + + public void setByte( String parameterName, byte x ) + throws SQLException + { + statement.setByte( parameterName, x ); + } + + public void setBytes( String parameterName, byte[] x ) + throws SQLException + { + statement.setBytes( parameterName, x ); + } + + public void setCharacterStream( String parameterName, Reader reader, int length ) + throws SQLException + { + statement.setCharacterStream( parameterName, reader, length ); + } + + public void setDate( String parameterName, Date x, Calendar cal ) + throws SQLException + { + statement.setDate( parameterName, x, cal ); + } + + public void setDate( String parameterName, Date x ) + throws SQLException + { + statement.setDate( parameterName, x ); + } + + public void setDouble( String parameterName, double x ) + throws SQLException + { + statement.setDouble( parameterName, x ); + } + + public void setFloat( String parameterName, float x ) + throws SQLException + { + statement.setFloat( parameterName, x ); + } + + public void setInt( String parameterName, int x ) + throws SQLException + { + statement.setInt( parameterName, x ); + } + + public void setLong( String parameterName, long x ) + throws SQLException + { + statement.setLong( parameterName, x ); + } + + public void setNull( String parameterName, int sqlType, String typeName ) + throws SQLException + { + statement.setNull( parameterName, sqlType, typeName ); + } + + public void setNull( String parameterName, int sqlType ) + throws SQLException + { + statement.setNull( parameterName, sqlType ); + } + + public void setObject( String parameterName, Object x, int targetSqlType, int scale ) + throws SQLException + { + statement.setObject( parameterName, x, targetSqlType, scale ); + } + + public void setObject( String parameterName, Object x, int targetSqlType ) + throws SQLException + { + statement.setObject( parameterName, x, targetSqlType ); + } + + public void setObject( String parameterName, Object x ) + throws SQLException + { + statement.setObject( parameterName, x ); + } + + public void setShort( String parameterName, short x ) + throws SQLException + { + statement.setShort( parameterName, x ); + } + + public void setString( String parameterName, String x ) + throws SQLException + { + statement.setString( parameterName, x ); + } + + public void setTime( String parameterName, Time x, Calendar cal ) + throws SQLException + { + statement.setTime( parameterName, x, cal ); + } + + public void setTime( String parameterName, Time x ) + throws SQLException + { + statement.setTime( parameterName, x ); + } + + public void setTimestamp( String parameterName, Timestamp x, Calendar cal ) + throws SQLException + { + statement.setTimestamp( parameterName, x, cal ); + } + + public void setTimestamp( String parameterName, Timestamp x ) + throws SQLException + { + statement.setTimestamp( parameterName, x ); + } + + public void setURL( String parameterName, URL val ) + throws SQLException + { + statement.setURL( parameterName, val ); + } + + public boolean wasNull() + throws SQLException + { + return statement.wasNull(); + } + +} Added: commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredConnection.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredConnection.java?rev=761572&view=auto ============================================================================== --- commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredConnection.java (added) +++ commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredConnection.java Fri Apr 3 08:45:56 2009 @@ -0,0 +1,316 @@ +/* + * 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.commons.monitoring.instrumentation.jdbc; + +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.Savepoint; +import java.sql.Statement; +import java.util.Map; + +import org.apache.commons.monitoring.Repository; + +/** + * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> + */ +public class MonitoredConnection + implements Connection +{ + + /** + * CallBack on connection beeing closed + */ + public interface ConnectionClosedCallBack + { + void onConnectionClosed(); + } + + /** target connection */ + private Connection connection; + + private Repository repository; + + private ConnectionClosedCallBack callBack; + + /** + * @param connection target connection + * @param monitor monitor for opened connections + */ + public MonitoredConnection( Connection connection, Repository repository, ConnectionClosedCallBack callBack ) + { + super(); + this.connection = connection; + this.repository = repository; + this.callBack = callBack; + } + + public void close() + throws SQLException + { + connection.close(); + callBack.onConnectionClosed(); + } + + public Statement createStatement() + throws SQLException + { + return monitor( connection.createStatement() ); + } + + public Statement createStatement( int resultSetType, int resultSetConcurrency, int resultSetHoldability ) + throws SQLException + { + return monitor( connection.createStatement( resultSetType, resultSetConcurrency, resultSetHoldability ) ); + } + + public Statement createStatement( int resultSetType, int resultSetConcurrency ) + throws SQLException + { + return monitor( connection.createStatement( resultSetType, resultSetConcurrency ) ); + } + + public CallableStatement prepareCall( String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability ) + throws SQLException + { + return monitor( connection.prepareCall( sql, resultSetType, resultSetConcurrency, resultSetHoldability ), sql ); + } + + public CallableStatement prepareCall( String sql, int resultSetType, int resultSetConcurrency ) + throws SQLException + { + return monitor( connection.prepareCall( sql, resultSetType, resultSetConcurrency ), sql ); + } + + public CallableStatement prepareCall( String sql ) + throws SQLException + { + return monitor( connection.prepareCall( sql ), sql ); + } + + public PreparedStatement prepareStatement( String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability ) + throws SQLException + { + return monitor( connection.prepareStatement( sql, resultSetType, resultSetConcurrency, resultSetHoldability ), + sql ); + } + + public PreparedStatement prepareStatement( String sql, int resultSetType, int resultSetConcurrency ) + throws SQLException + { + return monitor( connection.prepareStatement( sql, resultSetType, resultSetConcurrency ), sql ); + } + + public PreparedStatement prepareStatement( String sql, int autoGeneratedKeys ) + throws SQLException + { + return monitor( connection.prepareStatement( sql, autoGeneratedKeys ), sql ); + } + + public PreparedStatement prepareStatement( String sql, int[] columnIndexes ) + throws SQLException + { + return monitor( connection.prepareStatement( sql, columnIndexes ), sql ); + } + + public PreparedStatement prepareStatement( String sql, String[] columnNames ) + throws SQLException + { + return monitor( connection.prepareStatement( sql, columnNames ), sql ); + } + + public PreparedStatement prepareStatement( String sql ) + throws SQLException + { + return monitor( connection.prepareStatement( sql ), sql ); + } + + /** + * @param statement traget Statement + * @return monitored Statement + */ + private Statement monitor( Statement statement ) + { + return new MonitoredStatement( statement, repository ); + } + + /** + * @param statement traget PreparedStatement + * @param sql SQL Query + * @return monitored PreparedStatement + */ + private PreparedStatement monitor( PreparedStatement statement, String sql ) + { + return new MonitoredPreparedStatement( statement, sql, repository ); + } + + /** + * @param statement target PreparedStatement + * @param sql SQL Query + * @return Monitored CallableStatement + */ + private CallableStatement monitor( CallableStatement statement, String sql ) + { + return new MonitoredCallableStatement( statement, sql, repository ); + } + + // --- delegates methods --- + + public void clearWarnings() + throws SQLException + { + connection.clearWarnings(); + } + + public void commit() + throws SQLException + { + connection.commit(); + } + + public boolean getAutoCommit() + throws SQLException + { + return connection.getAutoCommit(); + } + + public String getCatalog() + throws SQLException + { + return connection.getCatalog(); + } + + public int getHoldability() + throws SQLException + { + return connection.getHoldability(); + } + + public DatabaseMetaData getMetaData() + throws SQLException + { + return connection.getMetaData(); + } + + public int getTransactionIsolation() + throws SQLException + { + return connection.getTransactionIsolation(); + } + + public Map<String, Class<?>> getTypeMap() + throws SQLException + { + return connection.getTypeMap(); + } + + public SQLWarning getWarnings() + throws SQLException + { + return connection.getWarnings(); + } + + public boolean isClosed() + throws SQLException + { + return connection.isClosed(); + } + + public boolean isReadOnly() + throws SQLException + { + return connection.isReadOnly(); + } + + public String nativeSQL( String sql ) + throws SQLException + { + return connection.nativeSQL( sql ); + } + + public void releaseSavepoint( Savepoint savepoint ) + throws SQLException + { + connection.releaseSavepoint( savepoint ); + } + + public void rollback() + throws SQLException + { + connection.rollback(); + } + + public void rollback( Savepoint savepoint ) + throws SQLException + { + connection.rollback( savepoint ); + } + + public void setAutoCommit( boolean autoCommit ) + throws SQLException + { + connection.setAutoCommit( autoCommit ); + } + + public void setCatalog( String catalog ) + throws SQLException + { + connection.setCatalog( catalog ); + } + + public void setHoldability( int holdability ) + throws SQLException + { + connection.setHoldability( holdability ); + } + + public void setReadOnly( boolean readOnly ) + throws SQLException + { + connection.setReadOnly( readOnly ); + } + + public Savepoint setSavepoint() + throws SQLException + { + return connection.setSavepoint(); + } + + public Savepoint setSavepoint( String name ) + throws SQLException + { + return connection.setSavepoint( name ); + } + + public void setTransactionIsolation( int level ) + throws SQLException + { + connection.setTransactionIsolation( level ); + } + + public void setTypeMap( Map<String, Class<?>> map ) + throws SQLException + { + connection.setTypeMap( map ); + } +} Added: commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredDataSource.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredDataSource.java?rev=761572&view=auto ============================================================================== --- commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredDataSource.java (added) +++ commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredDataSource.java Fri Apr 3 08:45:56 2009 @@ -0,0 +1,180 @@ +/* + * 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.commons.monitoring.instrumentation.jdbc; + +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.SQLException; + +import javax.sql.DataSource; + +import org.apache.commons.monitoring.Monitor; +import org.apache.commons.monitoring.Repository; +import org.apache.commons.monitoring.Role; +import org.apache.commons.monitoring.StopWatch; +import org.apache.commons.monitoring.Unit; +import org.apache.commons.monitoring.Metric.Type; +import org.apache.commons.monitoring.stopwatches.DefaultStopWatch; + +/** + * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> + */ +public class MonitoredDataSource + implements DataSource +{ + + private final static Role OPEN_CONECTIONS = new Role( "open connections", Unit.UNARY, Type.GAUGE ); + + private final static Role CONECTION_DURATION = new Role( "connection duration", Unit.Time.NANOSECOND, Type.COUNTER ); + + /** delegate DataSource */ + private DataSource dataSource; + + /** dataSource name */ + private String dataSourceName = DataSource.class.getName(); + + private Repository repository; + + private Monitor monitor; + + /** + * Constructor + * + * @param dataSource the datasource to monitor + */ + public MonitoredDataSource( DataSource dataSource ) + { + super(); + this.dataSource = dataSource; + } + + /** + * + */ + public MonitoredDataSource() + { + super(); + } + + /** + * @param dataSource the dataSource to set + */ + public void setDataSource( DataSource dataSource ) + { + this.dataSource = dataSource; + } + + /** + * @param dataSourceName the dataSourceName to set + */ + public void setDataSourceName( String dataSourceName ) + { + this.dataSourceName = dataSourceName; + } + + /** + * required + * + * @param repository + */ + public void setRepository( Repository repository ) + { + this.repository = repository; + } + + /** + * @param monitor the monitor to set + */ + public void setMonitor( Monitor monitor ) + { + this.monitor = monitor; + } + + public void init() + { + if ( monitor == null ) + { + monitor = repository.getMonitor( dataSourceName, "jdbc" ); + } + } + + /** + * {...@inheritdoc} + * + * @see javax.sql.DataSource#getConnection() + */ + public Connection getConnection() + throws SQLException + { + Connection connection = dataSource.getConnection(); + return monitor( connection ); + } + + /** + * {...@inheritdoc} + * + * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String) + */ + public Connection getConnection( String username, String password ) + throws SQLException + { + Connection connection = dataSource.getConnection( username, password ); + return monitor( connection ); + } + + private Connection monitor( Connection connection ) + { + // Computes the number of open connections and the connection duration + final StopWatch stopWatch = new DefaultStopWatch( monitor, OPEN_CONECTIONS, CONECTION_DURATION ); + return new MonitoredConnection( connection, repository, new MonitoredConnection.ConnectionClosedCallBack() + { + public void onConnectionClosed() + { + stopWatch.stop(); + } + } ); + } + + + // --- delegate methods --- + + public int getLoginTimeout() + throws SQLException + { + return dataSource.getLoginTimeout(); + } + + public PrintWriter getLogWriter() + throws SQLException + { + return dataSource.getLogWriter(); + } + + public void setLoginTimeout( int seconds ) + throws SQLException + { + dataSource.setLoginTimeout( seconds ); + } + + public void setLogWriter( PrintWriter out ) + throws SQLException + { + dataSource.setLogWriter( out ); + } + +} Added: commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredPreparedStatement.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredPreparedStatement.java?rev=761572&view=auto ============================================================================== --- commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredPreparedStatement.java (added) +++ commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredPreparedStatement.java Fri Apr 3 08:45:56 2009 @@ -0,0 +1,307 @@ +package org.apache.commons.monitoring.instrumentation.jdbc; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.ParameterMetaData; +import java.sql.PreparedStatement; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; + +import org.apache.commons.monitoring.Repository; +import org.apache.commons.monitoring.StopWatch; + +/** + * @author ndeloof + * + */ +public class MonitoredPreparedStatement + extends MonitoredStatement + implements PreparedStatement +{ + private PreparedStatement statement; + + protected String sql; + + /** + * @param statement + * @param repository + */ + public MonitoredPreparedStatement( PreparedStatement statement, String sql, Repository repository ) + { + super( statement, repository ); + this.statement = statement; + this.sql = sql; + } + + // --- delegate methods --- + + public final void addBatch() + throws SQLException + { + statement.addBatch(); + } + + public final void clearParameters() + throws SQLException + { + statement.clearParameters(); + } + + public final boolean execute() + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.execute(); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final ResultSet executeQuery() + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.executeQuery(); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final int executeUpdate() + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.executeUpdate(); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final ResultSetMetaData getMetaData() + throws SQLException + { + return statement.getMetaData(); + } + + public final ParameterMetaData getParameterMetaData() + throws SQLException + { + return statement.getParameterMetaData(); + } + + public final void setArray( int i, Array x ) + throws SQLException + { + statement.setArray( i, x ); + } + + public final void setAsciiStream( int parameterIndex, InputStream x, int length ) + throws SQLException + { + statement.setAsciiStream( parameterIndex, x, length ); + } + + public final void setBigDecimal( int parameterIndex, BigDecimal x ) + throws SQLException + { + statement.setBigDecimal( parameterIndex, x ); + } + + public final void setBinaryStream( int parameterIndex, InputStream x, int length ) + throws SQLException + { + statement.setBinaryStream( parameterIndex, x, length ); + } + + public final void setBlob( int i, Blob x ) + throws SQLException + { + statement.setBlob( i, x ); + } + + public final void setBoolean( int parameterIndex, boolean x ) + throws SQLException + { + statement.setBoolean( parameterIndex, x ); + } + + public final void setByte( int parameterIndex, byte x ) + throws SQLException + { + statement.setByte( parameterIndex, x ); + } + + public final void setBytes( int parameterIndex, byte[] x ) + throws SQLException + { + statement.setBytes( parameterIndex, x ); + } + + public final void setCharacterStream( int parameterIndex, Reader reader, int length ) + throws SQLException + { + statement.setCharacterStream( parameterIndex, reader, length ); + } + + public final void setClob( int i, Clob x ) + throws SQLException + { + statement.setClob( i, x ); + } + + public final void setDate( int parameterIndex, Date x, Calendar cal ) + throws SQLException + { + statement.setDate( parameterIndex, x, cal ); + } + + public final void setDate( int parameterIndex, Date x ) + throws SQLException + { + statement.setDate( parameterIndex, x ); + } + + public final void setDouble( int parameterIndex, double x ) + throws SQLException + { + statement.setDouble( parameterIndex, x ); + } + + public final void setFloat( int parameterIndex, float x ) + throws SQLException + { + statement.setFloat( parameterIndex, x ); + } + + public final void setInt( int parameterIndex, int x ) + throws SQLException + { + statement.setInt( parameterIndex, x ); + } + + public final void setLong( int parameterIndex, long x ) + throws SQLException + { + statement.setLong( parameterIndex, x ); + } + + public final void setNull( int paramIndex, int sqlType, String typeName ) + throws SQLException + { + statement.setNull( paramIndex, sqlType, typeName ); + } + + public final void setNull( int parameterIndex, int sqlType ) + throws SQLException + { + statement.setNull( parameterIndex, sqlType ); + } + + public final void setObject( int parameterIndex, Object x, int targetSqlType, int scale ) + throws SQLException + { + statement.setObject( parameterIndex, x, targetSqlType, scale ); + } + + public final void setObject( int parameterIndex, Object x, int targetSqlType ) + throws SQLException + { + statement.setObject( parameterIndex, x, targetSqlType ); + } + + public final void setObject( int parameterIndex, Object x ) + throws SQLException + { + statement.setObject( parameterIndex, x ); + } + + public final void setRef( int i, Ref x ) + throws SQLException + { + statement.setRef( i, x ); + } + + public final void setShort( int parameterIndex, short x ) + throws SQLException + { + statement.setShort( parameterIndex, x ); + } + + public final void setString( int parameterIndex, String x ) + throws SQLException + { + statement.setString( parameterIndex, x ); + } + + public final void setTime( int parameterIndex, Time x, Calendar cal ) + throws SQLException + { + statement.setTime( parameterIndex, x, cal ); + } + + public final void setTime( int parameterIndex, Time x ) + throws SQLException + { + statement.setTime( parameterIndex, x ); + } + + public final void setTimestamp( int parameterIndex, Timestamp x, Calendar cal ) + throws SQLException + { + statement.setTimestamp( parameterIndex, x, cal ); + } + + public final void setTimestamp( int parameterIndex, Timestamp x ) + throws SQLException + { + statement.setTimestamp( parameterIndex, x ); + } + + public final void setUnicodeStream( int parameterIndex, InputStream x, int length ) + throws SQLException + { + statement.setUnicodeStream( parameterIndex, x, length ); + } + + public final void setURL( int parameterIndex, URL x ) + throws SQLException + { + statement.setURL( parameterIndex, x ); + } + +} Added: commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredStatement.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredStatement.java?rev=761572&view=auto ============================================================================== --- commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredStatement.java (added) +++ commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/jdbc/MonitoredStatement.java Fri Apr 3 08:45:56 2009 @@ -0,0 +1,399 @@ +/* + * 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.commons.monitoring.instrumentation.jdbc; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.Statement; + +import org.apache.commons.monitoring.Monitor; +import org.apache.commons.monitoring.Repository; +import org.apache.commons.monitoring.StopWatch; + +/** + * @author <a href="mailto:ndel...@sourceforge.net">Nicolas De Loof</a> + */ +public class MonitoredStatement + implements Statement +{ + /** delegate statement */ + private Statement statement; + + protected Repository repository; + + public MonitoredStatement( Statement statement, Repository repository ) + { + super(); + this.statement = statement; + this.repository = repository; + } + + protected SQLException monitor( SQLException sqle ) + { + String name = "SQLException:" + sqle.getSQLState() + ":" + sqle.getErrorCode(); + Monitor monitor = repository.getMonitor( name, "jdbc" ); + monitor.getCounter( Monitor.FAILURES ).add( 1 ); + return sqle; + } + + // --- delegate methods --- + + public final void addBatch( String sql ) + throws SQLException + { + statement.addBatch( sql ); + } + + public final void cancel() + throws SQLException + { + statement.cancel(); + } + + public final void clearBatch() + throws SQLException + { + statement.clearBatch(); + } + + public final void clearWarnings() + throws SQLException + { + statement.clearWarnings(); + } + + public final void close() + throws SQLException + { + statement.close(); + } + + public final boolean execute( String sql, int autoGeneratedKeys ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.execute( sql, autoGeneratedKeys ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final boolean execute( String sql, int[] columnIndexes ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.execute( sql, columnIndexes ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final boolean execute( String sql, String[] columnNames ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.execute( sql, columnNames ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final boolean execute( String sql ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.execute( sql ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final int[] executeBatch() + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( "batch", "jdbc" ) ); + try + { + return statement.executeBatch(); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final ResultSet executeQuery( String sql ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.executeQuery( sql ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final int executeUpdate( String sql, int autoGeneratedKeys ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.executeUpdate( sql, autoGeneratedKeys ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final int executeUpdate( String sql, int[] columnIndexes ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.executeUpdate( sql, columnIndexes ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final int executeUpdate( String sql, String[] columnNames ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.executeUpdate( sql, columnNames ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final int executeUpdate( String sql ) + throws SQLException + { + StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) ); + try + { + return statement.executeUpdate( sql ); + } + catch ( SQLException sqle ) + { + throw monitor( sqle ); + } + finally + { + stopWatch.stop(); + } + } + + public final Connection getConnection() + throws SQLException + { + return statement.getConnection(); + } + + public final int getFetchDirection() + throws SQLException + { + return statement.getFetchDirection(); + } + + public final int getFetchSize() + throws SQLException + { + return statement.getFetchSize(); + } + + public final ResultSet getGeneratedKeys() + throws SQLException + { + return statement.getGeneratedKeys(); + } + + public final int getMaxFieldSize() + throws SQLException + { + return statement.getMaxFieldSize(); + } + + public final int getMaxRows() + throws SQLException + { + return statement.getMaxRows(); + } + + public final boolean getMoreResults() + throws SQLException + { + return statement.getMoreResults(); + } + + public final boolean getMoreResults( int current ) + throws SQLException + { + return statement.getMoreResults( current ); + } + + public final int getQueryTimeout() + throws SQLException + { + return statement.getQueryTimeout(); + } + + public final ResultSet getResultSet() + throws SQLException + { + return statement.getResultSet(); + } + + public final int getResultSetConcurrency() + throws SQLException + { + return statement.getResultSetConcurrency(); + } + + public final int getResultSetHoldability() + throws SQLException + { + return statement.getResultSetHoldability(); + } + + public final int getResultSetType() + throws SQLException + { + return statement.getResultSetType(); + } + + public final int getUpdateCount() + throws SQLException + { + return statement.getUpdateCount(); + } + + public final SQLWarning getWarnings() + throws SQLException + { + return statement.getWarnings(); + } + + public final void setCursorName( String name ) + throws SQLException + { + statement.setCursorName( name ); + } + + public final void setEscapeProcessing( boolean enable ) + throws SQLException + { + statement.setEscapeProcessing( enable ); + } + + public final void setFetchDirection( int direction ) + throws SQLException + { + statement.setFetchDirection( direction ); + } + + public final void setFetchSize( int rows ) + throws SQLException + { + statement.setFetchSize( rows ); + } + + public final void setMaxFieldSize( int max ) + throws SQLException + { + statement.setMaxFieldSize( max ); + } + + public final void setMaxRows( int max ) + throws SQLException + { + statement.setMaxRows( max ); + } + + public final void setQueryTimeout( int seconds ) + throws SQLException + { + statement.setQueryTimeout( seconds ); + } +}