Class YTransaction
- All Implemented Interfaces:
AutoCloseable
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 TypeMethodDescriptionvoidclose()Closes the transaction, committing all batched operations.voidcommit()Commits the transaction explicitly.protected voidfinalize()Ensures proper cleanup of native resources if close() was not called.booleanisClosed()Checks if this transaction has been closed.
-
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 closedRuntimeException- 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:
closein interfaceAutoCloseable
-
isClosed
public boolean isClosed()Checks if this transaction has been closed.- Returns:
- true if this transaction has been closed, false otherwise
-
finalize
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.
-