Skip to contents

Begins a transaction, calls fun with it, and ends it — whatever happens. mdbx_with_write() commits if fun returns normally and aborts if it throws; mdbx_with_read() always aborts, which for a read transaction simply releases the snapshot. Both use on.exit(), so the transaction is also ended if fun is interrupted.

Usage

mdbx_with_write(env, fun)

mdbx_with_read(env, fun)

Arguments

env

An mdbx_env object, from mdbx_env_open().

fun

A function of one argument, called with the mdbx_txn.

Value

The value of fun.

Details

This is the recommended way to use transactions: it makes the "abandoned a write transaction and kept the writer lock" mistake unreachable.

See also

Examples

path <- tempfile(fileext = ".mdbx")
env <- mdbx_env_open(path)

# Committed on normal return.
mdbx_with_write(env, function(txn) mdbx_txn_state(txn))
#> [1] "active"

mdbx_with_read(env, function(txn) mdbx_txn_state(txn))
#> [1] "active"

mdbx_env_close(env)
unlink(c(path, paste0(path, "-lck")))