Class YTransaction

java.lang.Object
net.carcdr.ycrdt.YTransaction
All Implemented Interfaces:
AutoCloseable

public class YTransaction extends Object implements AutoCloseable
Transaction handle for batching multiple CRDT operations.

Transactions allow multiple operations to be combined into a single atomic unit, resulting in:

  • Better performance (fewer JNI calls, single commit)
  • Single observer notification with combined changes
  • More efficient update encoding for synchronization

Usage with try-with-resources (recommended):


 try (YTransaction txn = doc.beginTransaction()) {
     text.insert(txn, 0, "Hello");
     text.insert(txn, 5, " World");
 } // Auto-commits here
 

Thread Safety: YTransaction instances are not thread-safe and must only be used from the thread that created them.

Memory Management: YTransaction implements Closeable and must be closed when no longer needed to commit changes and free native resources. Use try-with-resources to ensure proper cleanup.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the transaction, committing all batched operations.
    void
    Commits the transaction explicitly.
    protected void
    Ensures proper cleanup of native resources if close() was not called.
    boolean
    Checks if this transaction has been closed.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • commit

      public void commit()
      Commits the transaction explicitly.

      After commit, all batched operations become visible to observers and are encoded as a single update for synchronization.

      This method is called automatically by close(), so explicit calls are typically not necessary when using try-with-resources.

      This method is idempotent - calling it multiple times is safe.

      Throws:
      IllegalStateException - if transaction already closed
      RuntimeException - if commit fails
    • close

      public void close()
      Closes the transaction, committing all batched operations.

      This method is called automatically when using try-with-resources.

      This method is idempotent - calling it multiple times is safe.

      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Checks if this transaction has been closed.
      Returns:
      true if this transaction has been closed, false otherwise
    • finalize

      protected void finalize() throws Throwable
      Ensures proper cleanup of native resources if close() was not called.

      This is a safety net - you should always call close() explicitly or use try-with-resources.

      Overrides:
      finalize in class Object
      Throws:
      Throwable