public class SQLController extends Object
Separates statements into 2 categories.
An "update" statement can be batched. When getting the PreparedStatement the calling method passes in an argument to signify if it can be batched (whether it will use the statement before any other statement can execute), and if so will be cached against the Connection in use. When the statement is to be processed by the calling method it would pass in the "processNow" argument as false. This will leave the statement in the cache as "ready-to-be-processed" in case anything else comes in in the meantime. If the next statement to be requested has the same text and can also be batched then it will be returned the current statement so that it can populate it with its values. When the statement is ready for processing since it is in the cache the new values are added to the batch. Again the statement could be executed at that point or could continue batching.
Typical (unbatched) invocation would be as follows :-
SQLController sql = rdbmsMgr.getSQLController(); PreparedStatement ps = sql.getStatementForUpdate(conn, myStmt, false); // No batching sql.executeStatementUpdate(myStmt, ps, true); // Execute now, dont wait sql.closeStatement(ps);
Typical (batched) invocation would be as follows :-
SQLController sql = rdbmsMgr.getSQLController(); PreparedStatement ps = sql.getStatementForUpdate(conn, myStmt, true); // Batching sql.executeStatementUpdate(myStmt, ps, false); // Execute later sql.closeStatement(ps); ... PreparedStatement ps = sql.getStatementForUpdate(conn, myStmt, true); // Batching (same statement as first) sql.executeStatementUpdate(myStmt, ps, false); // Execute later sql.closeStatement(ps);
Things to note :-
Modifier and Type | Field and Description |
---|---|
protected boolean |
jdbcStatements |
protected int |
maxBatchSize
Maximum batch size (-1 implies no limit).
|
protected boolean |
paramValuesInBrackets |
protected int |
queryTimeout
Timeout to apply to queries (where required) in milliseconds.
|
protected boolean |
supportsBatching
Whether batching is supported for this controller (datastore).
|
Constructor and Description |
---|
SQLController(boolean supportsBatching,
int maxBatchSize,
int queryTimeout,
String stmtLogging)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
abortStatementForConnection(org.datanucleus.store.connection.ManagedConnection conn,
PreparedStatement ps)
Method to call to remove the current batched statement for this connection and close it
due to an error preventing continuation.
|
void |
closeStatement(org.datanucleus.store.connection.ManagedConnection conn,
PreparedStatement ps)
Convenience method to close a PreparedStatement.
|
boolean |
executeStatement(org.datanucleus.ExecutionContext ec,
org.datanucleus.store.connection.ManagedConnection conn,
String stmt,
PreparedStatement ps)
Method to execute a PreparedStatement (using PreparedStatement.execute()).
|
ResultSet |
executeStatementQuery(org.datanucleus.ExecutionContext ec,
org.datanucleus.store.connection.ManagedConnection conn,
String stmt,
PreparedStatement ps)
Method to execute a PreparedStatement query, and return the ResultSet.
|
int[] |
executeStatementUpdate(org.datanucleus.ExecutionContext ec,
org.datanucleus.store.connection.ManagedConnection conn,
String stmt,
PreparedStatement ps,
boolean processNow)
Method to execute a PreparedStatement update.
|
protected org.datanucleus.store.rdbms.SQLController.ConnectionStatementState |
getConnectionStatementState(org.datanucleus.store.connection.ManagedConnection conn)
Convenience method to get the state for this connection.
|
PreparedStatement |
getStatementForQuery(org.datanucleus.store.connection.ManagedConnection conn,
String stmtText)
Convenience method to create a new PreparedStatement for a query.
|
PreparedStatement |
getStatementForQuery(org.datanucleus.store.connection.ManagedConnection conn,
String stmtText,
String resultSetType,
String resultSetConcurrency)
Convenience method to create a new PreparedStatement for a query.
|
PreparedStatement |
getStatementForUpdate(org.datanucleus.store.connection.ManagedConnection conn,
String stmtText,
boolean batchable)
Convenience method to create a new PreparedStatement for an update.
|
PreparedStatement |
getStatementForUpdate(org.datanucleus.store.connection.ManagedConnection conn,
String stmtText,
boolean batchable,
boolean getGeneratedKeysFlag)
Convenience method to create a new PreparedStatement for an update.
|
protected int[] |
processConnectionStatement(org.datanucleus.store.connection.ManagedConnection conn)
Convenience method to process the currently waiting statement for the passed Connection.
|
void |
processStatementsForConnection(org.datanucleus.store.connection.ManagedConnection conn)
Convenience method to process any batched statement for the specified connection.
|
protected void |
removeConnectionStatementState(org.datanucleus.store.connection.ManagedConnection conn)
Convenience method to remove the state for this connection.
|
protected void |
setConnectionStatementState(org.datanucleus.store.connection.ManagedConnection conn,
org.datanucleus.store.rdbms.SQLController.ConnectionStatementState state)
Convenience method to set the state for this connection.
|
protected boolean supportsBatching
protected int maxBatchSize
protected int queryTimeout
protected boolean jdbcStatements
protected boolean paramValuesInBrackets
public SQLController(boolean supportsBatching, int maxBatchSize, int queryTimeout, String stmtLogging)
supportsBatching
- Whether batching is to be supported.maxBatchSize
- The maximum batch sizequeryTimeout
- Timeout for queries (ms)stmtLogging
- Setting for statement loggingpublic PreparedStatement getStatementForUpdate(org.datanucleus.store.connection.ManagedConnection conn, String stmtText, boolean batchable) throws SQLException
conn
- The Connection to use for the statementstmtText
- Statement textbatchable
- Whether this statement is batchable. Whether we will process the statement before any other statementSQLException
- thrown if an error occurs creating the statementpublic PreparedStatement getStatementForUpdate(org.datanucleus.store.connection.ManagedConnection conn, String stmtText, boolean batchable, boolean getGeneratedKeysFlag) throws SQLException
conn
- The Connection to use for the statementstmtText
- Statement textbatchable
- Whether this statement is batchable. Whether we will process the statement before any other statementgetGeneratedKeysFlag
- whether to request getGeneratedKeys for this statementSQLException
- thrown if an error occurs creating the statementpublic PreparedStatement getStatementForQuery(org.datanucleus.store.connection.ManagedConnection conn, String stmtText) throws SQLException
conn
- The Connection to use for the statementstmtText
- Statement textSQLException
- thrown if an error occurs creating the statementpublic PreparedStatement getStatementForQuery(org.datanucleus.store.connection.ManagedConnection conn, String stmtText, String resultSetType, String resultSetConcurrency) throws SQLException
conn
- The Connection to use for the statementstmtText
- Statement textresultSetType
- Type of result setresultSetConcurrency
- Concurrency for the result setSQLException
- thrown if an error occurs creating the statementpublic int[] executeStatementUpdate(org.datanucleus.ExecutionContext ec, org.datanucleus.store.connection.ManagedConnection conn, String stmt, PreparedStatement ps, boolean processNow) throws SQLException
ec
- ExecutionContextconn
- The connection (required since the one on PreparedStatement is not always the same so we cant use it)stmt
- The statement textps
- The Prepared StatementprocessNow
- Whether to process this statement now (only applies if is batched)SQLException
- Thrown if an error occurspublic boolean executeStatement(org.datanucleus.ExecutionContext ec, org.datanucleus.store.connection.ManagedConnection conn, String stmt, PreparedStatement ps) throws SQLException
ec
- Execution Contextconn
- The connection (required since the one on PreparedStatement is not always the same so we can't use it)stmt
- The statement textps
- The Prepared StatementSQLException
- Thrown if an error occurspublic ResultSet executeStatementQuery(org.datanucleus.ExecutionContext ec, org.datanucleus.store.connection.ManagedConnection conn, String stmt, PreparedStatement ps) throws SQLException
ec
- ExecutionContextconn
- The connection (required since the one on PreparedStatement is not always the same so we can't use it)stmt
- The statement textps
- The Prepared StatementSQLException
- Thrown if an error occurspublic void abortStatementForConnection(org.datanucleus.store.connection.ManagedConnection conn, PreparedStatement ps)
conn
- The connectionps
- The statementpublic void closeStatement(org.datanucleus.store.connection.ManagedConnection conn, PreparedStatement ps) throws SQLException
conn
- The Connectionps
- The PreparedStatementSQLException
- if an error occurs closing the statementpublic void processStatementsForConnection(org.datanucleus.store.connection.ManagedConnection conn) throws SQLException
conn
- The connectionSQLException
- Thrown if an error occurs on processing of the batchprotected int[] processConnectionStatement(org.datanucleus.store.connection.ManagedConnection conn) throws SQLException
conn
- The connectionSQLException
- if an error occurs processing the batchprotected void removeConnectionStatementState(org.datanucleus.store.connection.ManagedConnection conn)
conn
- The Connectionprotected org.datanucleus.store.rdbms.SQLController.ConnectionStatementState getConnectionStatementState(org.datanucleus.store.connection.ManagedConnection conn)
conn
- The Connectionprotected void setConnectionStatementState(org.datanucleus.store.connection.ManagedConnection conn, org.datanucleus.store.rdbms.SQLController.ConnectionStatementState state)
conn
- The Connectionstate
- The stateCopyright © 2015. All rights reserved.