Functions for transaction management


Документация находится в процессе перевода и может отставать от английской версии.

Functions for transaction management

Общие сведения

For general information and examples, see section Transaction control.

Observe the following rules when working with transactions:

Rule #1

The requests in a transaction must be sent to a server as a single block. It is not enough to enclose them between begin and commit or rollback. To ensure they are sent as a single block: put them in a function, or put them all on one line, or use a delimiter so that multi-line requests are handled together.

Rule #2

All database operations in a transaction should use the same storage engine. It is not safe to access tuple sets that are defined with {engine='vinyl'} and also access tuple sets that are defined with {engine='memtx'}, in the same transaction.

Rule #3

Requests which cause changes to the data definition – create, alter, drop, truncate – must not be used.


Below is a list of all functions for transaction management.

Name Use
box.begin() Begin the transaction
box.commit() End the transaction and save all changes
box.rollback() End the transaction and discard all changes
box.savepoint() Get a savepoint descriptor
box.rollback_to_savepoint() Do not end the transaction and discard all changes made after a savepoint
box.atomic() Execute a function, treating it as a transaction

Begin the transaction. Disable implicit yields until the transaction ends. Signal that writes to the write-ahead log will be deferred until the transaction ends. In effect the fiber which executes box.begin() is starting an «active multi-request transaction», blocking all other fibers.


End the transaction, and make all its data-change operations permanent.


End the transaction, but cancel all its data-change operations. An explicit call to functions outside box.space that always yield, such as fiber.sleep() or fiber.yield(), will have the same effect.


Return a descriptor of a savepoint (type = table), which can be used later by box.rollback_to_savepoint(savepoint). Savepoints can only be created while a transaction is active, and they are destroyed when a transaction ends.


Do not end the transaction, but cancel all its data-change and box.savepoint() operations that were done after the specified savepoint.


function f()
  box.begin()           -- start transaction
  box.space.t:insert{1} -- this will not be rolled back
  local s = box.savepoint()
  box.space.t:insert{2} -- this will be rolled back
  box.commit()          -- end transaction
box.atomic(function-name[, function-arguments])

Execute a function, acting as if the function starts with an implicit box.begin() and ends with an implicit box.commit() if successful, or ends with an implicit box.rollback() if there is an error.