Guides

Transactions and durability

Group writes so they land together, choose when writes reach the disk, and understand how XOD shares a database between threads and processes.

Writes outside a transaction

An Insert, Update, Delete or Drop on its own is written as part of the call (with the default Immediate durability, it is in the database’s files when the call returns). It isn’t journalled.

A write that fails leaves nothing behind in any durability mode or transaction: an Insert, InsertRange, InsertOrUpdate, Update, Delete, DeleteWhere, Drop or DropAll that throws (a handler throws, a key is already used, another program holds a file) puts back what it changed and leaves the database and earlier buffered or transaction writes intact, so a retry behaves like a first try.

One case isn’t covered outside a transaction: a process crash in the middle of a write. When writes must land together or not at all across a process crash too, use a transaction.

Transactions

To make several writes land together, open a transaction:

using (var tx = db.BeginTransaction())
{
    db.Insert(new Order { Status = "paid" });
    db.Insert(new Order { Status = "paid" });
    tx.Commit();
}

BeginTransaction() writes out anything previously buffered before starting the transaction, so a rollback discards only the transaction’s writes. A transaction touches no files on disk before Commit(), deletes included.

Rollback() touches no files and discards everything written or deleted since BeginTransaction(). Disposing a transaction without Commit() rolls it back, so an exception inside the using block leaves the database as it was.

using (var tx = db.BeginTransaction())
{
    db.Insert(new Order { Status = "draft" });
    tx.Rollback(); // the draft is gone
}

Commit() writes an undo journal (transaction.xwal) containing the before-images of the files it changes, applies the writes and deletes, and removes the journal. If the process dies during a commit, the next open undoes the commit from the journal before anything reads or writes. If a commit fails, it is undone at once and its original exception is thrown; if the undo fails too, Commit() throws TransactionException and the context refuses further writes until reopened.

Numbers handed out by an autonumber inside a rolled-back transaction aren’t handed out again, and a table dropped inside a rolled-back transaction keeps its numbering.

Transaction ownership and concurrency

One transaction can be open on a context at a time, and it belongs to the async flow that began it:

  • An await, a Task.Run or an *Async method inside the transaction is part of it.
  • Other threads wait for it. While a transaction is open, reads and writes from other threads wait for it to end, up to DatabaseOptions.TransactionTimeoutMs (30 seconds by default), then proceed or throw TransactionException with nothing done.
  • BeginTransaction() waits for other threads’ reads, writes and transactions, throwing TransactionException on timeout. Flush, ClearCache, GetStatistics, Drop and password changes wait for another thread’s transaction too.
  • Keep transactions short and begin them where their work is done. A transaction begun inside an async method belongs to that method, so its caller’s reads and writes wait for it; a transaction never disposed blocks other threads until the context closes.
  • Inside a BeforeAction or AfterAction handler, BeginTransaction(), Commit() and Rollback() throw TransactionException.

Durability: when writes reach the disk

DatabaseOptions.Durability chooses the trade-off.

Mode What happens Use it for
Immediate (default) Each write is in the database’s files when the call returns. Most applications.
Buffered Writes (including InsertRange, Delete and DeleteWhere) stay in memory until Flush() or context disposal. Bulk loads and high write rates.
var options = new DatabaseOptions { Durability = DurabilityMode.Buffered };

using (var db = new XodContext("data/app.xod", options: options))
{
    db.InsertRange(rows);
    db.Flush(); // now in the files
}

With Buffered, a process crash loses writes made since the last Flush(); call Flush() wherever losing work would matter. Flush() writes pages, tables and indexes (whether the database has a password or not).

Neither mode calls fsync to flush past the operating system to storage hardware. A write handed to the OS (in Buffered mode, at Flush() or disposal) survives a process crash or kill, while a power cut or OS crash can lose writes the OS hadn’t yet persisted. A transaction’s journal protects a commit against a process crash, not a power cut.

Threads

One XodContext is safe to use from many threads. Any number of reads run concurrently. Writes are applied one at a time across the database: a write waits for reads under way, and reads wait for it. A transaction holds up other threads until it ends (see Transactions).

Disposing a context waits for in-flight reads and writes on other threads to finish, flushes buffered data, and releases the files. Keep one XodContext per database for the life of your application rather than opening one per request.

Processes

A database belongs to one XodContext at a time. XOD is an embedded library, not a shared server: if several processes must write the same data concurrently, use a server database.

  • Exclusive folder and database locks: every context holds both xod.lock and app.xod.lock exclusively from open until disposal. Both lock files stay in the folder after close; leave them in place.
  • A second open is refused: opening the same database from another context or process (including the xod CLI) waits up to LockRetryTimeoutMs, then throws DatabaseLockedException.
  • Dispose every context: a context left to the garbage collector holds the database locked until finalized. A failed open (such as a wrong password) releases its locks immediately.
  • Crash recovery runs at open: because every open holds the database exclusively, it finishes or undoes any crash leftovers before anything else reads or writes.
  • Read-only access and lock files: a context with read-only access to existing lock files still holds the database lock until closed. Where the folder can’t be written and has no lock files, the context opens for reading only without holding a lock (see Read-only folders).
  • Network file systems: where the file system provides no exclusive file lock (such as some NFS clients), opening is refused rather than running unprotected. On Linux or macOS, setting DOTNET_SYSTEM_IO_DISABLEFILELOCKING=1 bypasses the lock check when you guarantee single-context access.

Source on GitHub · Report a problem with this page