All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.monitor.db.internal.StasiStatement Maven / Gradle / Ivy

/*
 * $Id: StasiStatement.java,v 1.3 2014/04/30 20:03:11 oboehm Exp $
 *
 * Copyright (c) 2014 by Oliver Boehm
 *
 * Licensed 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 16.03.2014 by oliver ([email protected])
 */

package patterntesting.runtime.monitor.db.internal;

import java.sql.*;

import patterntesting.runtime.log.*;
import patterntesting.runtime.monitor.ProfileMonitor;
import patterntesting.runtime.monitor.db.SqlStatistic;
import patterntesting.runtime.util.Converter;

/**
 * A simple wrapper for {@link Statement} to be able to find resource problems
 * while reading and writing to the database. It allows us also to measure times
 * of SQL statements.
 * 

* Why the name "Stasi..."? The Stasi was the official state security service of * Eastern Germany which controls the people (like NSA in the U.S. or KGB in * Russia, see also Wikipedia). * The StasiStatement controls the embedded {@link Statement} - therefore the * name. *

* * @author oliver * @version $Revision: 1.3 $ * @since 1.4.1 (16.03.2014) */ public class StasiStatement implements Statement { private static final PerfLogger log = new PerfLogger(StasiStatement.class); private final LogWatch logWatch = new LogWatch(); private final Statement statement; /** * Instantiates a new proxy statement. * * @param statement the statement */ public StasiStatement(final Statement statement) { this.statement = statement; } /** * Gets the statement. * * @return the statement */ protected final Statement getStatement() { return this.statement; } /** * Adds the batch. * * @param sql the sql * @throws SQLException the sQL exception * @see java.sql.Statement#addBatch(java.lang.String) */ public final void addBatch(final String sql) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { statement.addBatch(sql); log.stop(mon, sql); } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Cancel. * * @throws SQLException the sQL exception * @see java.sql.Statement#cancel() */ public final void cancel() throws SQLException { statement.cancel(); log.trace("{} cancelled.", statement); } /** * Clear batch. * * @throws SQLException the sQL exception * @see java.sql.Statement#clearBatch() */ public final void clearBatch() throws SQLException { statement.clearBatch(); log.trace("Batch cleared."); } /** * Clear warnings. * * @throws SQLException the sQL exception * @see java.sql.Statement#clearWarnings() */ public final void clearWarnings() throws SQLException { statement.clearWarnings(); log.trace("Warnings cleared."); } /** * Close. * * @throws SQLException the sQL exception * @see java.sql.Statement#close() */ public void close() throws SQLException { statement.close(); log.debug("Statement {} was closed after {}.", this.statement, this.logWatch); } /** * Execute. * * @param sql the sql * @param autoGeneratedKeys the auto generated keys * @return true, if successful * @throws SQLException the sQL exception * @see java.sql.Statement#execute(java.lang.String, int) */ public final boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { boolean ok = statement.execute(sql, autoGeneratedKeys); log.stop(mon, sql, ok); return ok; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute. * * @param sql the sql * @param columnIndexes the column indexes * @return true, if successful * @throws SQLException the sQL exception * @see java.sql.Statement#execute(java.lang.String, int[]) */ public final boolean execute(final String sql, final int[] columnIndexes) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { boolean ok = statement.execute(sql, columnIndexes); log.stop(mon, sql, ok); return ok; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute. * * @param sql the sql * @param columnNames the column names * @return true, if successful * @throws SQLException the sQL exception * @see java.sql.Statement#execute(java.lang.String, java.lang.String[]) */ public final boolean execute(final String sql, final String[] columnNames) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { boolean ok = statement.execute(sql, columnNames); log.stop(mon, sql, ok); return ok; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute. * * @param sql the sql * @return true, if successful * @throws SQLException the sQL exception * @see java.sql.Statement#execute(java.lang.String) */ public final boolean execute(final String sql) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { boolean ok = statement.execute(sql); log.stop(mon, sql, ok); return ok; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute batch. * * @return the int[] * @throws SQLException the sQL exception * @see java.sql.Statement#executeBatch() */ public final int[] executeBatch() throws SQLException { LogWatch watch = new LogWatch(); int[] ret = statement.executeBatch(); if (log.isDebugEnabled()) { log.debug("Batch execution returns with {} after {}.", Converter.toString(ret), watch); } return ret; } /** * Execute query. * * @param sql the sql * @return the result set * @throws SQLException the sQL exception * @see java.sql.Statement#executeQuery(java.lang.String) */ public final ResultSet executeQuery(final String sql) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { ResultSet rs = new StasiResultSet(statement.executeQuery(sql)); log.stop(mon, sql, rs); return rs; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute update. * * @param sql the sql * @param autoGeneratedKeys the auto generated keys * @return the int * @throws SQLException the sQL exception * @see java.sql.Statement#executeUpdate(java.lang.String, int) */ public final int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { int ret = statement.executeUpdate(sql, autoGeneratedKeys); log.stop(mon, sql, ret); return ret; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute update. * * @param sql the sql * @param columnIndexes the column indexes * @return the int * @throws SQLException the sQL exception * @see java.sql.Statement#executeUpdate(java.lang.String, int[]) */ public final int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { int ret = statement.executeUpdate(sql, columnIndexes); log.stop(mon, sql, ret); return ret; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute update. * * @param sql the sql * @param columnNames the column names * @return the int * @throws SQLException the sQL exception * @see java.sql.Statement#executeUpdate(java.lang.String, java.lang.String[]) */ public final int executeUpdate(final String sql, final String[] columnNames) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { int ret = statement.executeUpdate(sql, columnNames); log.stop(mon, sql, ret); return ret; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * Execute update. * * @param sql the sql * @return the int * @throws SQLException the sQL exception * @see java.sql.Statement#executeUpdate(java.lang.String) */ public final int executeUpdate(final String sql) throws SQLException { ProfileMonitor mon = SqlStatistic.start(sql); try { int ret = statement.executeUpdate(sql); log.stop(mon, sql, ret); return ret; } catch (SQLException ex) { throw enrichedSQLException(mon, sql, ex); } } /** * For better error analysis the original {@link SQLException} will be * enriched with some additional infos. * * @param mon the mon * @param sql the sql * @param original the original * @return the sQL exception */ protected SQLException enrichedSQLException(final ProfileMonitor mon, final String sql, final SQLException original) { mon.stop(); return new SQLException("SQL \"" + sql + "\" failed after " + mon.getLastTime(), original); } /** * Gets the connection. * * @return the connection * @throws SQLException the sQL exception * @see java.sql.Statement#getConnection() */ public final Connection getConnection() throws SQLException { return statement.getConnection(); } /** * Gets the fetch direction. * * @return the fetch direction * @throws SQLException the sQL exception * @see java.sql.Statement#getFetchDirection() */ public final int getFetchDirection() throws SQLException { return statement.getFetchDirection(); } /** * Gets the fetch size. * * @return the fetch size * @throws SQLException the sQL exception * @see java.sql.Statement#getFetchSize() */ public final int getFetchSize() throws SQLException { return statement.getFetchSize(); } /** * Gets the generated keys. * * @return the generated keys * @throws SQLException the sQL exception * @see java.sql.Statement#getGeneratedKeys() */ public final ResultSet getGeneratedKeys() throws SQLException { return new StasiResultSet(statement.getGeneratedKeys()); } /** * Gets the max field size. * * @return the max field size * @throws SQLException the sQL exception * @see java.sql.Statement#getMaxFieldSize() */ public final int getMaxFieldSize() throws SQLException { return statement.getMaxFieldSize(); } /** * Gets the max rows. * * @return the max rows * @throws SQLException the sQL exception * @see java.sql.Statement#getMaxRows() */ public final int getMaxRows() throws SQLException { return statement.getMaxRows(); } /** * Gets the more results. * * @return the more results * @throws SQLException the sQL exception * @see java.sql.Statement#getMoreResults() */ public final boolean getMoreResults() throws SQLException { return statement.getMoreResults(); } /** * Gets the more results. * * @param current the current * @return the more results * @throws SQLException the sQL exception * @see java.sql.Statement#getMoreResults(int) */ public final boolean getMoreResults(final int current) throws SQLException { return statement.getMoreResults(current); } /** * Gets the query timeout. * * @return the query timeout * @throws SQLException the sQL exception * @see java.sql.Statement#getQueryTimeout() */ public final int getQueryTimeout() throws SQLException { return statement.getQueryTimeout(); } /** * Gets the result set. * * @return the result set * @throws SQLException the sQL exception * @see java.sql.Statement#getResultSet() */ public final ResultSet getResultSet() throws SQLException { return new StasiResultSet(statement.getResultSet()); } /** * Gets the result set concurrency. * * @return the result set concurrency * @throws SQLException the sQL exception * @see java.sql.Statement#getResultSetConcurrency() */ public final int getResultSetConcurrency() throws SQLException { return statement.getResultSetConcurrency(); } /** * Gets the result set holdability. * * @return the result set holdability * @throws SQLException the sQL exception * @see java.sql.Statement#getResultSetHoldability() */ public final int getResultSetHoldability() throws SQLException { return statement.getResultSetHoldability(); } /** * Gets the result set type. * * @return the result set type * @throws SQLException the sQL exception * @see java.sql.Statement#getResultSetType() */ public final int getResultSetType() throws SQLException { return statement.getResultSetType(); } /** * Gets the update count. * * @return the update count * @throws SQLException the sQL exception * @see java.sql.Statement#getUpdateCount() */ public final int getUpdateCount() throws SQLException { return statement.getUpdateCount(); } /** * Gets the warnings. * * @return the warnings * @throws SQLException the sQL exception * @see java.sql.Statement#getWarnings() */ public final SQLWarning getWarnings() throws SQLException { return statement.getWarnings(); } /** * Checks if is closed. * * @return true, if is closed * @throws SQLException the sQL exception * @see java.sql.Statement#isClosed() */ public final boolean isClosed() throws SQLException { return statement.isClosed(); } /** * Checks if is poolable. * * @return true, if is poolable * @throws SQLException the sQL exception * @see java.sql.Statement#isPoolable() */ public final boolean isPoolable() throws SQLException { return statement.isPoolable(); } /** * Checks if is wrapper for. * * @param arg0 the arg0 * @return true, if is wrapper for * @throws SQLException the sQL exception * @see java.sql.Wrapper#isWrapperFor(java.lang.Class) */ public final boolean isWrapperFor(final Class arg0) throws SQLException { return statement.isWrapperFor(arg0); } /** * Sets the cursor name. * * @param name the new cursor name * @throws SQLException the sQL exception * @see java.sql.Statement#setCursorName(java.lang.String) */ public final void setCursorName(final String name) throws SQLException { statement.setCursorName(name); } /** * Sets the escape processing. * * @param enable the new escape processing * @throws SQLException the sQL exception * @see java.sql.Statement#setEscapeProcessing(boolean) */ public final void setEscapeProcessing(final boolean enable) throws SQLException { statement.setEscapeProcessing(enable); } /** * Sets the fetch direction. * * @param direction the new fetch direction * @throws SQLException the sQL exception * @see java.sql.Statement#setFetchDirection(int) */ public final void setFetchDirection(final int direction) throws SQLException { statement.setFetchDirection(direction); } /** * Sets the fetch size. * * @param rows the new fetch size * @throws SQLException the sQL exception * @see java.sql.Statement#setFetchSize(int) */ public final void setFetchSize(final int rows) throws SQLException { statement.setFetchSize(rows); } /** * Sets the max field size. * * @param max the new max field size * @throws SQLException the sQL exception * @see java.sql.Statement#setMaxFieldSize(int) */ public final void setMaxFieldSize(final int max) throws SQLException { statement.setMaxFieldSize(max); } /** * Sets the max rows. * * @param max the new max rows * @throws SQLException the sQL exception * @see java.sql.Statement#setMaxRows(int) */ public final void setMaxRows(final int max) throws SQLException { statement.setMaxRows(max); } /** * Sets the poolable. * * @param poolable the new poolable * @throws SQLException the sQL exception * @see java.sql.Statement#setPoolable(boolean) */ public final void setPoolable(final boolean poolable) throws SQLException { statement.setPoolable(poolable); } /** * Sets the query timeout. * * @param seconds the new query timeout * @throws SQLException the sQL exception * @see java.sql.Statement#setQueryTimeout(int) */ public final void setQueryTimeout(final int seconds) throws SQLException { statement.setQueryTimeout(seconds); } /** * Unwrap. * * @param the generic type * @param arg0 the arg0 * @return the t * @throws SQLException the sQL exception * @see java.sql.Wrapper#unwrap(java.lang.Class) */ public final T unwrap(final Class arg0) throws SQLException { return statement.unwrap(arg0); } /** * Close on completion. This method is not required for Java 6 and below * but for Java 7. To be compatible with Java 6 this method has no * implementation. * * @throws SQLException the SQL exception * @since Java 7 */ public final void closeOnCompletion() throws SQLException { throw new UnsupportedOperationException("not yet implemented"); } /** * Checks if is close on completion. This method is not required for Java 6 * and below but for Java 7. To be compatible with Java 6 this method has no * implementation. * * @return true, if is close on completion * @throws SQLException the SQL exception * @since Java 7 */ public final boolean isCloseOnCompletion() throws SQLException { throw new UnsupportedOperationException("not yet implemented"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy