Class BatchOperation

java.lang.Object
com.invirgance.convirgance.dbms.BatchOperation
All Implemented Interfaces:
AtomicOperation

public class BatchOperation extends Object implements 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.
Author:
jbanes
  • Constructor Details

    • BatchOperation

      public BatchOperation()
      Default constructor for BatchOperation. Initializes an empty BatchOperation without a predefined query or records.
    • BatchOperation

      public BatchOperation(Query query)
      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

      public 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.
      Parameters:
      query - The query to be executed in batch operations.
      records - The records to be processed in the batch.
  • Method Details

    • getQuery

      public Query 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

      public void setQuery(Query query)
      Sets the batch SQL query that will be used by the operation
      Parameters:
      query - The query.
    • getRecords

      public Iterable<JSONObject> 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

      public void setRecords(Iterable<JSONObject> records)
      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

      public void execute(Connection connection) throws SQLException
      DO NOT CALL DIRECTLY. This is called by DBMS 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 interface AtomicOperation
      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 initialized
      NullPointerException - if the batch SQL query has not been set
      See Also: