pub struct Transaction { /* private fields */ }Expand description
A transaction handle.
Transaction handles are used to protect database operations. A single Transaction may be used for operations on multiple databases within the same environment.
Transaction handles are free-threaded; they may be used concurrently by multiple threads. Once committed or aborted, the handle must not be used for any further operations.
§Example
use noxu_db::{Environment, EnvironmentConfig};
use std::path::PathBuf;
let config = EnvironmentConfig::new(PathBuf::from("/tmp/mydb"))
.with_allow_create(true)
.with_transactional(true);
let env = Environment::open(config).unwrap();
let txn = env.begin_transaction(None).unwrap();
// ... do operations ...
txn.commit().unwrap();Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn new(id: u64, config: TransactionConfig) -> Self
👎Deprecated since 2.4.1: use Environment::begin_transaction() to obtain a fully wired transaction handle
pub fn new(id: u64, config: TransactionConfig) -> Self
use Environment::begin_transaction() to obtain a fully wired transaction handle
Create a new unconnected transaction handle.
Deprecated — this constructor creates a transaction that is not
wired to a WAL, lock manager, or environment. Commits and aborts
on such a transaction are no-ops. Use
Environment::begin_transaction
to obtain a fully operational handle.
§Arguments
id- Unique transaction IDconfig- Transaction configuration
Sourcepub fn with_log_manager(
id: u64,
config: TransactionConfig,
log_manager: Arc<LogManager>,
) -> Self
pub fn with_log_manager( id: u64, config: TransactionConfig, log_manager: Arc<LogManager>, ) -> Self
Create a new transaction backed by a real WAL.
Called by Environment::begin_transaction() to wire the transaction to
the environment’s log manager so that commit/abort write WAL entries.
Internal — this method is pub for cross-crate wiring within the
Noxu DB engine but is not part of the v3.0 stable surface.
LogManager is not re-exported by noxu-db; downstream callers
cannot use this method without adding an internal crate dependency.
Sourcepub fn with_env_impl(self, env_impl: Arc<SyncMutex<EnvironmentImpl>>) -> Self
pub fn with_env_impl(self, env_impl: Arc<SyncMutex<EnvironmentImpl>>) -> Self
Wires the EnvironmentImpl so that abort() can apply undo records.
Called by Environment::begin_transaction() after constructing the
Transaction.
Internal — EnvironmentImpl is not re-exported by noxu-db.
Sourcepub fn with_inner_txn(self, txn: Arc<Mutex<Txn>>) -> Self
pub fn with_inner_txn(self, txn: Arc<Mutex<Txn>>) -> Self
Sets the inner Txn for lock management and write-set tracking.
Called by Environment::begin_transaction() to wire the transaction to
the environment’s TxnManager / LockManager.
Internal — noxu_txn::Txn is not re-exported by noxu-db.
Sourcepub fn get_inner_txn(&self) -> Option<Arc<Mutex<Txn>>>
pub fn get_inner_txn(&self) -> Option<Arc<Mutex<Txn>>>
Returns a clone of the Arc<Mutex<Txn>> inner transaction, if any.
Used by Database::make_cursor_for_txn() to wire the cursor to the
same Txn so that write operations lock via the transaction.
Internal — noxu_txn::Txn is not re-exported by noxu-db.
Sourcepub fn register_abort_callback<F>(&self, f: F)
pub fn register_abort_callback<F>(&self, f: F)
Register a callback to run when this transaction aborts.
Used by Environment::open_database() to roll back a transactional
database creation if the owning transaction is aborted (C-4 / JE 1-I).
The callback is invoked from within abort(), after WAL writes but
before the outer state is marked Aborted.
Sourcepub fn register_commit_callback<F>(&self, f: F)
pub fn register_commit_callback<F>(&self, f: F)
Register a callback to run when this transaction commits.
Used by Environment::open_database() to finalise a transactional
database creation when the owning transaction commits (C-4 / JE 1-I).
The callback is invoked from within commit_with_durability(), after
the WAL entry is written and locks are released.
Sourcepub fn commit(&self) -> Result<()>
pub fn commit(&self) -> Result<()>
Commit the transaction.
All operations performed under this transaction are made durable and visible to other transactions.
§Errors
Returns an error if:
- The transaction is not in
Openstate. - Writing the
TxnCommitWAL entry fails (EnvironmentFailurewith reasonLogWrite, propagated fromwrite_txn_end). - The inner-
Txncommit fails after the WAL entry has been fsynced (e.g. open cursors held against this transaction, or inner-state inconsistency surfaced bycheck_state). When this happens the transaction is still durably committed; the error is propagated so the caller can react to the leak.
Sourcepub fn commit_with_durability(&self, durability: Durability) -> Result<()>
pub fn commit_with_durability(&self, durability: Durability) -> Result<()>
Commit the transaction with specific durability.
§Arguments
durability- Durability settings for this commit
§Errors
Returns an error if:
- The transaction is not in
Openstate. - Writing the
TxnCommitWAL entry fails (EnvironmentFailurewith reasonLogWrite, propagated fromwrite_txn_end). - The inner-
Txncommit fails after the WAL entry has been fsynced (e.g. open cursors held against this transaction, or inner-state inconsistency surfaced bycheck_state). When this happens the transaction is still durably committed; the error is propagated so the caller can react to the leak.
Sourcepub fn abort(&self) -> Result<()>
pub fn abort(&self) -> Result<()>
Abort the transaction.
All operations performed under this transaction are rolled back.
§Errors
Returns an error if:
- The transaction is already committed or aborted.
- Writing the
TxnAbortWAL entry fails (EnvironmentFailurewith reasonLogWrite, propagated fromwrite_txn_end). This path is taken only when the transaction is not read-only and aLogManageris configured on the environment.
Sourcepub fn prepare(
&self,
xid_format_id: i32,
xid_gtrid: &[u8],
xid_bqual: &[u8],
) -> Result<()>
pub fn prepare( &self, xid_format_id: i32, xid_gtrid: &[u8], xid_bqual: &[u8], ) -> Result<()>
Prepares the transaction for the second phase of XA two-phase commit.
Implements the crash-durable contract introduced in wave 3-2:
- Writes a
TxnPrepareWAL frame containing the txn id, the first / last LSN logged by this transaction, and the supplied XID components (format_id, gtrid, bqual). The frame is fsynced before this method returns, so a crash immediately afterwards still allows recovery to resurrect the prepared state. - Marks the inner
Txnas PREPARED — directcommit()andabort()calls now returnOperationNotAllowed; onlyresolved_commit_after_prepareandresolved_abort_after_preparemay finalise the transaction. - Locks are RETAINED — prepared transactions hold every lock until xa_commit / xa_rollback so concurrent readers cannot observe in-flight state.
- The persistent prepared-log entry (the
noxu-xa::PreparedLogXID -> timestamp record) is the responsibility of the XA layer and is written after this method returns. The WALTxnPrepareframe is the source of truth for crash durability; the prepared-log database is a convenience for operators inspecting in-doubt XIDs without scanning the WAL.
Returns Ok(()) on success. Read-only transactions (no LN
frames written) still take a code path here so the inner Txn
flips to PREPARED, but no TxnPrepare frame is emitted — the
XA layer should take its PrepareResult::ReadOnly shortcut
rather than calling this method on read-only branches.
§Errors
OperationNotAllowedif the transaction is not Open.EnvironmentFailure { reason: LogWrite }if the WAL write or fsync fails.
Sourcepub fn resolved_commit_after_prepare(&self) -> Result<()>
pub fn resolved_commit_after_prepare(&self) -> Result<()>
Resolves a prepared transaction with a commit.
Used by the XA xa_commit path. Bypasses the
TransactionState::Prepared guard in commit_with_durability
because the prepare already established the commit decision.
Steps:
- Verifies the txn is Prepared.
- Writes a
TxnCommitWAL frame (mirrorscommit()). - Releases the inner Txn’s locks via
resolved_commit_after_prepare. - Transitions state to Committed and prunes from the active-txns registry.
Sourcepub fn resolved_abort_after_prepare(&self) -> Result<()>
pub fn resolved_abort_after_prepare(&self) -> Result<()>
Resolves a prepared transaction with an abort.
Sourcepub fn set_name<S: Into<String>>(&self, name: S)
pub fn set_name<S: Into<String>>(&self, name: S)
Set the human-readable name of this transaction.
Mirrors Transaction.setName(String). The name is purely
diagnostic — it appears in Debug output, structured log
records, and lock-conflict reports.
(transaction-env F22).
Sourcepub fn get_name(&self) -> Option<String>
pub fn get_name(&self) -> Option<String>
Returns the caller-supplied transaction name, if any.
Mirrors Transaction.getName().
Sourcepub fn lock_count(&self) -> usize
pub fn lock_count(&self) -> usize
Returns the number of locks currently held by this transaction.
Mirrors Transaction.getLockStat() / Transaction.getNumWriteLocks() + getNumReadLocks() (the JE API exposes both counts; we return
the sum because the lock manager partitions reads / writes per
LSN rather than per record). Returns 0 for transactions that
have not acquired any locks (or for read-only transactions
running with read-uncommitted isolation, which skip lock
acquisition entirely).
(transaction-env F23 “lock-stat reporting missing”).
Sourcepub fn lock_counts(&self) -> (usize, usize)
pub fn lock_counts(&self) -> (usize, usize)
Returns (read_lock_count, write_lock_count) for this
transaction’s lock set.
Mirrors JE’s Transaction.getNumReadLocks() /
getNumWriteLocks() accessors. Returns (0, 0) for a
transaction that has not acquired any locks.
Sourcepub fn get_state(&self) -> TransactionState
pub fn get_state(&self) -> TransactionState
Get the current transaction state.
Sourcepub fn set_lock_timeout(&self, timeout_ms: u64)
pub fn set_lock_timeout(&self, timeout_ms: u64)
Set the lock timeout for this transaction.
§Arguments
timeout_ms- Lock timeout in milliseconds (0 = use environment default)
Sourcepub fn get_lock_timeout(&self) -> u64
pub fn get_lock_timeout(&self) -> u64
Get the lock timeout for this transaction.
Sourcepub fn set_txn_timeout(&self, timeout_ms: u64)
pub fn set_txn_timeout(&self, timeout_ms: u64)
Set the transaction timeout.
§Arguments
timeout_ms- Transaction timeout in milliseconds (0 = use environment default)
Sourcepub fn get_txn_timeout(&self) -> u64
pub fn get_txn_timeout(&self) -> u64
Get the transaction timeout for this transaction.
Sourcepub fn get_durability(&self) -> Option<Durability>
pub fn get_durability(&self) -> Option<Durability>
Get the durability setting for this transaction.
Sourcepub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
Check if this is a read-only transaction.