Package com.invirgance.convirgance.dbms
Class BatchOperation
java.lang.Object
com.invirgance.convirgance.dbms.BatchOperation
- All Implemented Interfaces:
AtomicOperation
Bulk insert or update a stream of records in a single transaction. This uses the JDBC
batching APIs for maximum performance.
Note that transaction atomicity may be violated in the case of large loads. By default a commit is triggered every 1,000 records to prevent an overflow of the transaction buffer. A failure after 1,000 records have been inserted may leave the database in an inconsistent state and thus should be planned for.
Note that transaction atomicity may be violated in the case of large loads. By default a commit is triggered every 1,000 records to prevent an overflow of the transaction buffer. A failure after 1,000 records have been inserted may leave the database in an inconsistent state and thus should be planned for.
- Author:
- jbanes
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for BatchOperation.BatchOperation
(Query query) Constructs a BatchOperation with the specified query.BatchOperation
(Query query, Iterable<JSONObject> records) This constructor allows specifying both the query to be executed and the records to be processed with the query. -
Method Summary
Modifier and TypeMethodDescriptionvoid
execute
(Connection connection) DO NOT CALL DIRECTLY.int
Returns the current auto commit interval used when processing the transaction.getQuery()
Gets the query to be executed across the records.Returns the stream of records that will be used by this BatchOperation during executionvoid
setAutoCommit
(int commit) Sets the auto commit interval used when processing the transaction.void
Sets the batch SQL query that will be used by the operationvoid
setRecords
(Iterable<JSONObject> records) Sets the records that will be inserted or updated during execution.
-
Constructor Details
-
BatchOperation
public BatchOperation()Default constructor for BatchOperation. Initializes an empty BatchOperation without a predefined query or records. -
BatchOperation
Constructs a BatchOperation with the specified query. This query will be used to perform batch operations.- Parameters:
query
- The query to be executed in batch operations.
-
BatchOperation
This constructor allows specifying both the query to be executed and the records to be processed with the query.- Parameters:
query
- The query to be executed in batch operations.records
- The records to be processed in the batch.
-
-
Method Details
-
getQuery
Gets the query to be executed across the records.- Returns:
- The batch SQL query. Null if the query has not yet been set.
-
setQuery
Sets the batch SQL query that will be used by the operation- Parameters:
query
- The query.
-
getRecords
Returns the stream of records that will be used by this BatchOperation during execution- Returns:
- The stream used for the operation. Null if the stream has not yet been set.
-
setRecords
Sets the records that will be inserted or updated during execution.- Parameters:
records
- The JSONObjects to use.
-
getAutoCommit
public int getAutoCommit()Returns the current auto commit interval used when processing the transaction. A commit will be triggered after this number of inserts or updates. The default commit interval is 1,000.- Returns:
- The auto commit interval
-
setAutoCommit
public void setAutoCommit(int commit) Sets the auto commit interval used when processing the transaction. A commit will be triggered after this number of inserts or updates. Be carefuly about setting too high of a number or the database may fail on a full transaction log.- Parameters:
commit
- The auto commit interval
-
execute
DO NOT CALL DIRECTLY. This is called byDBMS
to executes the operation using the provided query for each record in the stream of data. If the stream is larger than the auto commit threshold, the transaction will be partially committed to prevent an overflow of the transaction log.- Specified by:
execute
in interfaceAtomicOperation
- Parameters:
connection
- an active JDBC connection- Throws:
SQLException
- When an issue occurs while preparing the statement for the given records. Or while executing the batch operation.NullPointerException
- if the records have not been initializedNullPointerException
- if the batch SQL query has not been set- See Also:
-