Class StatementWrapper
java.lang.Object
org.tentackle.dbms.StatementWrapper
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
PreparedStatementWrapper
A wrapper for sql statements.
Will catch and report SQLExceptions and keep track of being used only once after
Will catch and report SQLExceptions and keep track of being used only once after
Db.createStatement().- Author:
- harald
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanflag if statement has been cancelled.protected final ManagedConnectionthe managed connection.protected WeakReference<ResultSetWrapper> pending open result set.protected booleanflag to tell that parallel execution should not be logged.protected booleanflag if statement is marked ready for being consumed.protected booleanflag if statement is currently running.protected Stringthe SQL string.protected Stringoptional stacktrace that invoked the last query or update.protected Statementthe sql statement, null if closed. -
Constructor Summary
ConstructorsConstructorDescriptionStatementWrapper(ManagedConnection con, Statement stmt) Creates a wrapper for a sql statement. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the given SQL command to the current list of commands.protected voidAsserts db is writable.protected voidAsserts the statement is open.voidcancel()Cancels a statement.voidEmpties thisStatementobject's current list of batched SQL commands.voidclose()Closes this statement.voidconsume()Consume a statement without executing it.
The method is invoked if a rollback is performed prior to consuming the statement.protected voidDetach the session from the connection.
Statements detach the session on executeUpdate or on close() in theResultSetWrapperafter executeQuery.int[]executeBatch(boolean finish) Submits a batch of commands.executeQuery(String sql) Executes a query.executeQuery(String sql, boolean withinTx) Executes a query.protected ResultSetexecuteQueryImpl(String sql) Implementation of executeQuery.intexecuteUpdate(String sql) Executes the given SQL statement.protected intexecuteUpdateImpl(String sql) Implementation of executeUpdate.protected voidDetaches the Db after a severe error.Same asgetSession()but nullsafe.
Throws PersistenceException if statement no more attached to connection.Gets the connection.intRetrieves the direction for fetching rows from database tables that is the default for result sets generated from thisStatementobject.intRetrieves the number of result set rows that is the default fetch size forResultSetobjects generated from thisStatementobject.intRetrieves the maximum number of rows that aResultSetobject produced by thisStatementobject can contain.Gets the currently attached session.getSql()Gets the sql string.Gets the optional stacktrace that invoked the last query or update.Gets the wrapped statement.booleanReturns whether this statement has been cancelled.
May be invoked from any thread.booleanisClosed()Determines whether this statement is closed.booleanReturns whether this statement is marked ready.
Used to determine unused pending statements in servers when closing dead sessions.booleanReturns whether parallel execution on same connection should be logged.booleanReturns whether the statement is being executed.
May be invoked from any thread.voidMarks the statement to be ready for being consumed by aDbattached to aConnectionManager.
This is an additional measure to enforce the programming rule that a statement is being used only once afterDb.createStatement()(for non-prepared statements) or afterDb.getPreparedStatement(StatementKey, boolean, int, int, SqlSupplier)for prepared statements.voidsetFetchDirection(int direction) Gives the driver a hint as to the direction in which rows will be processed inResultSetobjects created using thisStatementobject.voidsetFetchSize(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 forResultSetobjects generated by thisStatement.voidsetMaxRows(int max) Sets the limit for the maximum number of rows that anyResultSetobject generated by thisStatementobject can contain to the given number.voidsetParallelOk(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 voidsetRunning(boolean running) Sets the running flag of this statements.
Updates the running statements inManagedConnection.voidsetStackTrace(String stackTrace) Sets an optional stacktrace that invoked the last query or update.
If set, the stacktrace will be logged intoString().toString()voidUn-marks (consumes) this statement previously marked ready.
-
Field Details
-
con
the managed connection. -
stmt
the sql statement, null if closed. -
sql
the SQL string. -
ready
protected boolean readyflag if statement is marked ready for being consumed. -
running
protected boolean runningflag if statement is currently running. -
cancelled
protected boolean cancelledflag if statement has been cancelled. -
parallelOk
protected boolean parallelOkflag to tell that parallel execution should not be logged. -
stackTrace
optional stacktrace that invoked the last query or update. -
openResultSet
pending open result set.
-
-
Constructor Details
-
StatementWrapper
Creates a wrapper for a sql statement.- Parameters:
con- the connectionstmt- the sql statement
-
-
Method Details
-
getConnection
-
getStatement
-
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
Gets the optional stacktrace that invoked the last query or update.- Returns:
- the stacktrace as a string, null if none (default)
-
setStackTrace
Sets an optional stacktrace that invoked the last query or update.
If set, the stacktrace will be logged intoString().- Parameters:
stackTrace- the stacktrace as a string, null to clear
-
getSession
Gets the currently attached session.- Returns:
- the db, null if statement not attached
-
getAttachedSession
Same asgetSession()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 aDbattached to aConnectionManager.
This is an additional measure to enforce the programming rule that a statement is being used only once afterDb.createStatement()(for non-prepared statements) or afterDb.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
Statementsay: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
-
toString
-
detachSession
protected void detachSession()Detach the session from the connection.
Statements detach the session on executeUpdate or on close() in theResultSetWrapperafter executeQuery. -
executeUpdateImpl
Implementation of executeUpdate.- Parameters:
sql- the sql string- Returns:
- the number of affected rows
- Throws:
SQLException- if update failed
-
executeUpdate
Executes the given SQL statement.- Parameters:
sql- an sql-statement- Returns:
- the row count
-
addBatch
Adds the given SQL command to the current list of commands.- Parameters:
sql- the SQL statement
-
clearBatch
public void clearBatch()Empties thisStatementobject'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
Implementation of executeQuery.- Parameters:
sql- the sql string- Returns:
- the result set
- Throws:
SQLException- if query failed
-
executeQuery
Executes a query.- Parameters:
sql- is the query sql stringwithinTx- is true if start a transaction for this query.- Returns:
- the result set as a ResultSetWrapper
-
executeQuery
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 inManagedConnection.- 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
-
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 forResultSetobjects generated by thisStatement. 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 forResultSetobjects generated from thisStatementobject. If thisStatementobject has not set a fetch size by calling the methodsetFetchSize, the return value is implementation-specific.- Returns:
- the default fetch size for result sets generated
from this
Statementobject - See Also:
-
setMaxRows
public void setMaxRows(int max) Sets the limit for the maximum number of rows that anyResultSetobject generated by thisStatementobject 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 aResultSetobject produced by thisStatementobject can contain. If this limit is exceeded, the excess rows are silently dropped.- Returns:
- the current maximum number of rows for a
ResultSetobject produced by thisStatementobject; 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 inResultSetobjects created using thisStatementobject. The default value isResultSet.FETCH_FORWARD.Note that this method sets the default fetch direction for result sets generated by this
Statementobject. 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 thisStatementobject. If thisStatementobject has not set a fetch direction by calling the methodsetFetchDirection, the return value is implementation-specific.- Returns:
- the default fetch direction for result sets generated
from this
Statementobject - 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.
-