public class StatementWrapper extends Object implements AutoCloseable
Db.createStatement().| Modifier and Type | Field and Description |
|---|---|
protected boolean |
cancelled
flag if statement has been cancelled.
|
protected ManagedConnection |
con
the managed connection.
|
protected WeakReference<ResultSetWrapper> |
openResultSet
pending open result set.
|
protected boolean |
ready
flag if statement is marked ready for being consumed.
|
protected boolean |
running
flag if statement is currently running.
|
protected String |
sql
the SQL string.
|
protected Statement |
stmt
the sql statement, null if closed.
|
| Constructor and Description |
|---|
StatementWrapper(ManagedConnection con,
Statement stmt)
Creates a wrapper for an sql statement.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
assertNotReadOnly()
Asserts db is writable.
|
protected void |
assertOpen()
Asserts the statement is open.
|
void |
cancel()
Cancels a statement.
|
void |
close()
Closes this statement.
|
void |
consume()
Consume a statement without executing it.
The method is invoked if a rollback is performed prior to consuming the statement. |
protected void |
detachSession()
Detach the db from the connection.
Statements detach the db on executeUpdate or on close() in the resultsetwrapper after executeQuery. |
ResultSetWrapper |
executeQuery(String sql)
Executes a query.
|
ResultSetWrapper |
executeQuery(String sql,
boolean withinTx)
Executes a query.
|
protected ResultSet |
executeQueryImpl(String sql)
Implementation of executeQuery.
|
int |
executeUpdate(String sql)
Executes the given SQL statement.
|
protected int |
executeUpdateImpl(String sql)
Implementation of executeUpdate.
|
protected void |
forceDetached()
Detaches the Db after a severe error.
|
ManagedConnection |
getConnection()
Gets the connection.
|
int |
getFetchDirection()
Retrieves the direction for fetching rows from
database tables that is the default for result sets
generated from this
Statement object. |
int |
getFetchSize()
Retrieves the number of result set rows that is the default
fetch size for
ResultSet objects
generated from this Statement object. |
int |
getMaxRows()
Retrieves the maximum number of rows that a
ResultSet object produced by this
Statement object can contain. |
Db |
getSession()
Gets the currently attached session.
|
String |
getSql()
Gets the sql string.
|
Statement |
getStatement()
Gets the wrapped statement.
|
boolean |
isCancelled()
Returns whether this statement has been cancelled.
May be invoked from any thread. |
boolean |
isClosed()
Determines whether this statement is closed.
|
boolean |
isMarkedReady()
Returns whether this statement is marked ready.
Used to determine unused pending statements in servers when closing dead sessions. |
boolean |
isRunning()
Returns whether the statement is being executed.
May be invoked from any thread. |
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(org.tentackle.persist.StatementKey, boolean, int, int, java.util.function.Supplier<java.lang.String>)
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 genrated 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. |
protected void |
setRunning(boolean running)
Sets the running flag of this statements.
Updates the running statements in ManagedConnection. |
String |
toString() |
void |
unmarkReady()
Unmarks (consumes) this statement previously marked ready.
|
protected final ManagedConnection con
protected Statement stmt
protected String sql
protected boolean ready
protected boolean running
protected boolean cancelled
protected WeakReference<ResultSetWrapper> openResultSet
public StatementWrapper(ManagedConnection con, Statement stmt)
con - the connectionstmt - the sql statementpublic ManagedConnection getConnection()
public Statement getStatement()
public Db getSession()
public void markReady()
Db attached
to a ConnectionManager.Db.createStatement()
(for non-prepared statements) or after Db.getPreparedStatement(org.tentackle.persist.StatementKey, boolean, int, int, java.util.function.Supplier<java.lang.String>)
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 statment'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 resultset and you wouldn't have any clue which resultset forced the closing.
public void unmarkReady()
public boolean isMarkedReady()
public void consume()
public String getSql()
protected void detachSession()
protected int executeUpdateImpl(String sql) throws SQLException
sql - the sql stringSQLException - if update failespublic int executeUpdate(String sql)
sql - an sql-statementprotected ResultSet executeQueryImpl(String sql) throws SQLException
sql - the sql stringSQLException - if query failedpublic ResultSetWrapper executeQuery(String sql, boolean withinTx)
sql - is the query sql stringwithinTx - is true if start a transaction for this query.public ResultSetWrapper executeQuery(String sql)
sql - is the query sql stringprotected void setRunning(boolean running)
ManagedConnection.running - true if running, false if finishedpublic boolean isRunning()
public void cancel()
public boolean isCancelled()
public void close()
close in interface AutoCloseablepublic boolean isClosed()
public void setFetchSize(int rows)
ResultSet objects genrated by this Statement.
If the value specified is zero, then the hint is ignored.
The default value is zero.rows - the number of rows to fetchgetFetchSize()public int getFetchSize()
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.Statement objectsetFetchSize(int)public void setMaxRows(int max)
ResultSet object generated by this Statement
object can contain to the given number.
If the limit is exceeded, the excess
rows are silently dropped.max - the new max rows limit; zero means there is no limitgetMaxRows()public int getMaxRows()
ResultSet object produced by this
Statement object can contain. If this limit is exceeded,
the excess rows are silently dropped.ResultSet
object produced by this Statement object;
zero means there is no limitsetMaxRows(int)public void setFetchDirection(int direction)
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.
direction - the initial direction for processing rowsgetFetchDirection()public int getFetchDirection()
Statement object.
If this Statement object has not set
a fetch direction by calling the method setFetchDirection,
the return value is implementation-specific.Statement objectsetFetchDirection(int)protected void assertOpen()
protected void assertNotReadOnly()
protected void forceDetached()
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.
Copyright © 2016 Krake Softwaretechnik. All rights reserved.