Class StatementWrapper

java.lang.Object
org.tentackle.dbms.StatementWrapper
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
PreparedStatementWrapper

public class StatementWrapper extends Object implements AutoCloseable
A wrapper for sql statements.
Will catch and report SQLExceptions and keep track of being used only once after Db.createStatement().
Author:
harald
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
    flag if statement has been cancelled.
    protected final ManagedConnection
    the managed connection.
    pending open result set.
    protected boolean
    flag to tell that parallel execution should not be logged.
    protected boolean
    flag if statement is marked ready for being consumed.
    protected boolean
    flag if statement is currently running.
    protected String
    the SQL string.
    protected String
    optional stacktrace that invoked the last query or update.
    protected Statement
    the sql statement, null if closed.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a wrapper for a sql statement.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds the given SQL command to the current list of commands.
    protected void
    Asserts db is writable.
    protected void
    Asserts the statement is open.
    void
    Cancels a statement.
    void
    Empties this Statement object's current list of batched SQL commands.
    void
    Closes this statement.
    void
    Consume a statement without executing it.
    The method is invoked if a rollback is performed prior to consuming the statement.
    protected void
    Detach the session from the connection.
    Statements detach the session on executeUpdate or on close() in the ResultSetWrapper after executeQuery.
    int[]
    executeBatch(boolean finish)
    Submits a batch of commands.
    Executes a query.
    executeQuery(String sql, boolean withinTx)
    Executes a query.
    protected ResultSet
    Implementation of executeQuery.
    int
    Executes the given SQL statement.
    protected int
    Implementation of executeUpdate.
    protected void
    Detaches the Db after a severe error.
    Same as getSession() but nullsafe.
    Throws PersistenceException if statement no more attached to connection.
    Gets the connection.
    int
    Retrieves the direction for fetching rows from database tables that is the default for result sets generated from this Statement object.
    int
    Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated from this Statement object.
    int
    Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain.
    Gets the currently attached session.
    Gets the sql string.
    Gets the optional stacktrace that invoked the last query or update.
    Gets the wrapped statement.
    boolean
    Returns whether this statement has been cancelled.
    May be invoked from any thread.
    boolean
    Determines whether this statement is closed.
    boolean
    Returns whether this statement is marked ready.
    Used to determine unused pending statements in servers when closing dead sessions.
    boolean
    Returns whether parallel execution on same connection should be logged.
    boolean
    Returns whether the statement is being executed.
    May be invoked from any thread.
    void
    Marks the statement to be ready for being consumed by a Db attached to a ConnectionManager.
    This is an additional measure to enforce the programming rule that a statement is being used only once after Db.createStatement() (for non-prepared statements) or after Db.getPreparedStatement(StatementKey, boolean, int, int, SqlSupplier) for prepared statements.
    void
    setFetchDirection(int direction)
    Gives the driver a hint as to the direction in which rows will be processed in ResultSet objects created using this Statement object.
    void
    setFetchSize(int rows)
    Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects generated by this Statement.
    void
    setMaxRows(int max)
    Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number.
    void
    setParallelOk(boolean parallelOk)
    Sets whether parallel execution on same connection should be logged.
    Multiple open result sets on a single connection usually means, that other selects are submitted while processing the result set.
    protected void
    setRunning(boolean running)
    Sets the running flag of this statements.
    Updates the running statements in ManagedConnection.
    void
    setStackTrace(String stackTrace)
    Sets an optional stacktrace that invoked the last query or update.
    If set, the stacktrace will be logged in toString().
     
    void
    Un-marks (consumes) this statement previously marked ready.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • con

      protected final ManagedConnection con
      the managed connection.
    • stmt

      protected Statement stmt
      the sql statement, null if closed.
    • sql

      protected String sql
      the SQL string.
    • ready

      protected boolean ready
      flag if statement is marked ready for being consumed.
    • running

      protected boolean running
      flag if statement is currently running.
    • cancelled

      protected boolean cancelled
      flag if statement has been cancelled.
    • parallelOk

      protected boolean parallelOk
      flag to tell that parallel execution should not be logged.
    • stackTrace

      protected String stackTrace
      optional stacktrace that invoked the last query or update.
    • openResultSet

      protected WeakReference<ResultSetWrapper> openResultSet
      pending open result set.
  • Constructor Details

    • StatementWrapper

      public StatementWrapper(ManagedConnection con, Statement stmt)
      Creates a wrapper for a sql statement.
      Parameters:
      con - the connection
      stmt - the sql statement
  • Method Details

    • getConnection

      public ManagedConnection getConnection()
      Gets the connection.
      Returns:
      the connection
    • getStatement

      public Statement getStatement()
      Gets the wrapped statement.
      Returns:
      the statement, null if closed
    • isParallelOk

      public boolean isParallelOk()
      Returns whether parallel execution on same connection should be logged.
      Returns:
      true if parallel is select ok
      See Also:
    • setParallelOk

      public void setParallelOk(boolean parallelOk)
      Sets whether parallel execution on same connection should be logged.
      Multiple open result sets on a single connection usually means, that other selects are submitted while processing the result set. Under rare conditions this might be ok, but usually it's not.
      Parameters:
      parallelOk - true if parallel select is ok
    • getStackTrace

      public String getStackTrace()
      Gets the optional stacktrace that invoked the last query or update.
      Returns:
      the stacktrace as a string, null if none (default)
    • setStackTrace

      public void setStackTrace(String stackTrace)
      Sets an optional stacktrace that invoked the last query or update.
      If set, the stacktrace will be logged in toString().
      Parameters:
      stackTrace - the stacktrace as a string, null to clear
    • getSession

      public Db getSession()
      Gets the currently attached session.
      Returns:
      the db, null if statement not attached
    • getAttachedSession

      public Db getAttachedSession()
      Same as getSession() but nullsafe.
      Throws PersistenceException if statement no more attached to connection.
      Returns:
      the session, never null
    • markReady

      public void markReady()
      Marks the statement to be ready for being consumed by a Db attached to a ConnectionManager.
      This is an additional measure to enforce the programming rule that a statement is being used only once after Db.createStatement() (for non-prepared statements) or after Db.getPreparedStatement(StatementKey, boolean, int, int, SqlSupplier) for prepared statements.

      If a statement is marked ready more than once, i.e. an open result exists (which would be closed according to the JDBC specs), a PersistenceException is thrown. The specs in Statement say:

      By default, only one ResultSet object per Statement object can be open at the same time.
      Therefore, if the reading of one ResultSet object is interleaved with the reading of another,
      each must have been generated by different Statement objects.
      All execution methods in the Statement interface implicitly close a statement's current
      ResultSet object if an open one exists.
      
      Without this additional measure a "ResultSet closed" exception will be thrown by the JDBC-driver on the next usage of the first result set, and you wouldn't have any clue which result set forced the closing.
    • unmarkReady

      public void unmarkReady()
      Un-marks (consumes) this statement previously marked ready.
    • isMarkedReady

      public boolean isMarkedReady()
      Returns whether this statement is marked ready.
      Used to determine unused pending statements in servers when closing dead sessions.
      Returns:
      true if pending
    • consume

      public void consume()
      Consume a statement without executing it.
      The method is invoked if a rollback is performed prior to consuming the statement.
    • getSql

      public String getSql()
      Gets the sql string.
      Returns:
      the sql string
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • detachSession

      protected void detachSession()
      Detach the session from the connection.
      Statements detach the session on executeUpdate or on close() in the ResultSetWrapper after executeQuery.
    • executeUpdateImpl

      protected int executeUpdateImpl(String sql) throws SQLException
      Implementation of executeUpdate.
      Parameters:
      sql - the sql string
      Returns:
      the number of affected rows
      Throws:
      SQLException - if update failed
    • executeUpdate

      public int executeUpdate(String sql)
      Executes the given SQL statement.
      Parameters:
      sql - an sql-statement
      Returns:
      the row count
    • addBatch

      public void addBatch(String sql)
      Adds the given SQL command to the current list of commands.
      Parameters:
      sql - the SQL statement
    • clearBatch

      public void clearBatch()
      Empties this Statement object's current list of batched SQL commands.
    • executeBatch

      public int[] executeBatch(boolean finish)
      Submits a batch of commands.
      Parameters:
      finish - true if this is the last invocation
      Returns:
      an array of update counts containing one element for each command in the batch
    • executeQueryImpl

      protected ResultSet executeQueryImpl(String sql) throws SQLException
      Implementation of executeQuery.
      Parameters:
      sql - the sql string
      Returns:
      the result set
      Throws:
      SQLException - if query failed
    • executeQuery

      public ResultSetWrapper executeQuery(String sql, boolean withinTx)
      Executes a query.
      Parameters:
      sql - is the query sql string
      withinTx - is true if start a transaction for this query.
      Returns:
      the result set as a ResultSetWrapper
    • executeQuery

      public ResultSetWrapper executeQuery(String sql)
      Executes a query.
      Parameters:
      sql - is the query sql string
      Returns:
      the result set as a ResultSetWrapper
    • setRunning

      protected void setRunning(boolean running)
      Sets the running flag of this statements.
      Updates the running statements in ManagedConnection.
      Parameters:
      running - true if running, false if finished
    • isRunning

      public boolean isRunning()
      Returns whether the statement is being executed.
      May be invoked from any thread.
      Returns:
      true if statement is running
    • cancel

      public void cancel()
      Cancels a statement.
    • isCancelled

      public boolean isCancelled()
      Returns whether this statement has been cancelled.
      May be invoked from any thread.
      Returns:
      true if cancelled
    • close

      public void close()
      Closes this statement.
      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Determines whether this statement is closed.
      Returns:
      true if statement closed
    • setFetchSize

      public void setFetchSize(int rows)
      Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects generated by this Statement. If the value specified is zero, then the hint is ignored. The default value is zero.
      Parameters:
      rows - the number of rows to fetch
      See Also:
    • getFetchSize

      public int getFetchSize()
      Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated from this Statement object. If this Statement object has not set a fetch size by calling the method setFetchSize, the return value is implementation-specific.
      Returns:
      the default fetch size for result sets generated from this Statement object
      See Also:
    • setMaxRows

      public void setMaxRows(int max)
      Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number. If the limit is exceeded, the excess rows are silently dropped.
      Parameters:
      max - the new max rows limit; zero means there is no limit
      See Also:
    • getMaxRows

      public int getMaxRows()
      Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain. If this limit is exceeded, the excess rows are silently dropped.
      Returns:
      the current maximum number of rows for a ResultSet object produced by this Statement object; zero means there is no limit
      See Also:
    • setFetchDirection

      public void setFetchDirection(int direction)
      Gives the driver a hint as to the direction in which rows will be processed in ResultSet objects created using this Statement object. The default value is ResultSet.FETCH_FORWARD.

      Note that this method sets the default fetch direction for result sets generated by this Statement object. Each result set has its own methods for getting and setting its own fetch direction.

      Parameters:
      direction - the initial direction for processing rows
      See Also:
    • getFetchDirection

      public int getFetchDirection()
      Retrieves the direction for fetching rows from database tables that is the default for result sets generated from this Statement object. If this Statement object has not set a fetch direction by calling the method setFetchDirection, the return value is implementation-specific.
      Returns:
      the default fetch direction for result sets generated from this Statement object
      See Also:
    • assertOpen

      protected void assertOpen()
      Asserts the statement is open.
    • assertNotReadOnly

      protected void assertNotReadOnly()
      Asserts db is writable.
    • forceDetached

      protected void forceDetached()
      Detaches the Db after a severe error.

      Invoked to clean up any pending statements, rollback the transaction (if any) and detach the db after a severe error. This is a cleanup measure in case the application does not handle exceptions properly.