Package groovy.sql
Class BatchingStatementWrapper
java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.sql.BatchingStatementWrapper
- All Implemented Interfaces:
GroovyObject,AutoCloseable
- Direct Known Subclasses:
BatchingPreparedStatementWrapper
Class which delegates to a Statement but keeps track of a batch count size.
If the batch count reaches the predefined number, this Statement does an executeBatch()
automatically. If batchSize is zero, then no batching is performed.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intNumber of commands added since the last delegate batch execution.protected intAutomatic execution threshold;0disables automatic partitioning.protected LoggerLogger used for batch diagnostics.Accumulated update counts across delegate batch executions. -
Constructor Summary
ConstructorsConstructorDescriptionBatchingStatementWrapper(Statement delegate, int batchSize, Logger log) Creates a batching wrapper for a statement. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a SQL command to the current batch.voidClears the current batch and resets this wrapper's batch bookkeeping.voidclose()Closes the wrapped statement.int[]Executes any pending batched commands and returns the aggregated update counts.protected voidIncrements batch count (after addBatch(..) has been called) and executedelegate.executeBatch()if batchSize has been reached.invokeMethod(String name, Object args) Delegates unknown method calls to the wrappedStatement.protected voidprocessResult(int[] lastResult) Incorporates one delegate batch execution result into this wrapper's state.protected voidreset()Resets the wrapper's in-memory batch bookkeeping.Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClassMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface groovy.lang.GroovyObject
getProperty, setProperty
-
Field Details
-
batchSize
protected int batchSizeAutomatic execution threshold;0disables automatic partitioning. -
batchCount
protected int batchCountNumber of commands added since the last delegate batch execution. -
log
Logger used for batch diagnostics. -
results
Accumulated update counts across delegate batch executions.
-
-
Constructor Details
-
BatchingStatementWrapper
Creates a batching wrapper for a statement.- Parameters:
delegate- the statement to wrapbatchSize- the automatic execution threshold;0disables automatic partitioninglog- the logger to use for batch diagnostics
-
-
Method Details
-
reset
protected void reset()Resets the wrapper's in-memory batch bookkeeping. -
invokeMethod
Delegates unknown method calls to the wrappedStatement.- Specified by:
invokeMethodin interfaceGroovyObject- Parameters:
name- the method nameargs- the method arguments- Returns:
- the delegated result
-
addBatch
Adds a SQL command to the current batch.- Parameters:
sql- the SQL command to add- Throws:
SQLException- if the command cannot be added
-
incrementBatchCount
Increments batch count (after addBatch(..) has been called) and executedelegate.executeBatch()if batchSize has been reached.- Throws:
SQLException
-
clearBatch
Clears the current batch and resets this wrapper's batch bookkeeping.- Throws:
SQLException- if the wrapped statement fails to clear its batch
-
executeBatch
Executes any pending batched commands and returns the aggregated update counts.- Returns:
- one update count per executed batch command
- Throws:
SQLException- if batch execution fails
-
processResult
protected void processResult(int[] lastResult) Incorporates one delegate batch execution result into this wrapper's state.- Parameters:
lastResult- the update counts returned by the wrapped statement
-
close
Closes the wrapped statement.- Specified by:
closein interfaceAutoCloseable- Throws:
SQLException- if the statement cannot be closed
-