Class TransactionalBuffer

  • All Implemented Interfaces:
    AutoCloseable

    @NotThreadSafe
    public final class TransactionalBuffer
    extends Object
    implements AutoCloseable
    Buffer that stores transactions and related callbacks that will be executed when a transaction commits or discarded when a transaction has been rolled back.
    Author:
    Andrey Pustovetov
    • Constructor Detail

      • TransactionalBuffer

        TransactionalBuffer​(ErrorHandler errorHandler,
                            OracleStreamingChangeEventSourceMetrics streamingMetrics)
        Constructor to create a new instance.
        Parameters:
        errorHandler - the connector error handler
        streamingMetrics - the streaming metrics
    • Method Detail

      • getRolledBackTransactionIds

        Set<String> getRolledBackTransactionIds()
        Returns:
        rolled back transactions
      • registerCommitCallback

        void registerCommitCallback​(String transactionId,
                                    Scn scn,
                                    Instant changeTime,
                                    TransactionalBuffer.CommitCallback callback)
        Registers callback to execute when transaction commits.
        Parameters:
        transactionId - transaction identifier
        scn - SCN
        changeTime - time of DML parsing completion
        callback - callback to execute when transaction commits
      • commit

        boolean commit​(String transactionId,
                       Scn scn,
                       OracleOffsetContext offsetContext,
                       Timestamp timestamp,
                       ChangeEventSource.ChangeEventSourceContext context,
                       String debugMessage,
                       EventDispatcher dispatcher)
        Commits a transaction by looking up the transaction in the buffer and if exists, all registered callbacks will be executed in chronological order, emitting events for each followed by a transaction commit event.
        Parameters:
        transactionId - transaction identifier
        scn - SCN of the commit.
        offsetContext - Oracle offset
        timestamp - commit timestamp
        context - context to check that source is running
        debugMessage - message
        dispatcher - event dispatcher
        Returns:
        true if committed transaction is in the buffer, was not processed yet and processed now
      • rollback

        boolean rollback​(String transactionId,
                         String debugMessage)
        Clears registered callbacks for given transaction identifier.
        Parameters:
        transactionId - transaction id
        debugMessage - message
        Returns:
        true if the rollback is for a transaction in the buffer
      • abandonLongTransactions

        void abandonLongTransactions​(Scn thresholdScn,
                                     OracleOffsetContext offsetContext)
        If for some reason the connector got restarted, the offset will point to the beginning of the oldest captured transaction. If that transaction was lasted for a long time, let say > 4 hours, the offset might be not accessible after restart, Hence we have to address these cases manually.

        In case of an abandonment, all DMLs/Commits/Rollbacks for this transaction will be ignored

        Parameters:
        thresholdScn - the smallest SVN of any transaction to keep in the buffer. All others will be removed.
        offsetContext - the offset context
      • isTransactionRegistered

        boolean isTransactionRegistered​(String txId)
      • calculateSmallestScn

        private Scn calculateSmallestScn()
      • isEmpty

        boolean isEmpty()
        Returns true if buffer is empty, otherwise false.
        Returns:
        true if buffer is empty, otherwise false
      • setDatabaseTimeDifference

        void setDatabaseTimeDifference​(long difference)
        Set the database time difference.
        Parameters:
        difference - the time difference in milliseconds