box.atomic() | Tarantool

box.atomic()

box.atomic([opts, ]tx-function[, 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.

Parameters:
  • opts (table) –

    (optional) transaction options:

  • tx-function (string) – the function name
  • function-arguments – (optional) arguments passed to the function
Return:

the result of the function passed to atomic() as an argument

Possible errors:

  • error and abort the transaction in case of a conflict.
  • error and abort the transaction if the timeout is exceeded.
  • error if the operation fails to write to disk.
  • error if for some reason memory cannot be allocated.

Example

-- Create an index with the specified sequence --
box.schema.sequence.create('id_sequence', { min = 1 })
box.space.bands:create_index('primary', { parts = { 'id' }, sequence = 'id_sequence' })

-- Insert test data --
box.space.bands:insert { 1, 'Roxette', 1986 }
box.space.bands:insert { 2, 'Scorpions', 1965 }
box.space.bands:insert { 3, 'Ace of Base', 1987 }

-- Define a function --
local function insert_band(band_name, year)
    box.space.bands:insert { nil, band_name, year }
end

-- Begin and commit the transaction implicitly --
box.atomic(insert_band, 'The Beatles', 1960)

-- Begin the transaction with the specified isolation level --
box.atomic({ txn_isolation = 'read-committed' },
        insert_band, 'The Rolling Stones', 1962)
Found what you were looking for?
Feedback