Can I yield during an transaction in Tarantool?

182 Views Asked by At

I'm curious about yielding during a transaction. Will it be interrupted immediately? Or will other fibers be able to read changes that have not yet been committed?

I've overlooked the documentation but didn't see it.

1

There are 1 best solutions below

0
On BEST ANSWER

Currently, there is a way to yield in a transaction. This is because there is now a fully functional transaction manager for both memtx and vinyl engines.

Here's a working example:

local fiber = require('fiber')

-- If you just use box.cfg{}, this app will fail
box.cfg{memtx_use_mvcc_engine=true}
--box.cfg{}

box.schema.space.create('account', {if_not_exists=true})
box.space.account:format({ {name='id',type='unsigned'},
      {name='first_name',type='string'},
      {name='last_name',type='string'},
})

box.begin()
box.space.account:put({2, "John", "Doe"})

print("sleeping")
fiber.sleep(1)
print("woke up")

box.space.account:put({3, "Ivan", "Ivanov"})

box.commit()

os.exit(0)

Note the memtx_use_mvcc_engine option. It enables the transaction engine. Without this option, a yield inside the transaction will result in immediate rollback.

The new transaction manager will also make changes inside the transaction to be invisible to other fibers and transactions before it is committed. And in case of conflict (parallel changes to the same data), the transaction will be rolled back.