Skip to content

Commit e0db7bf

Browse files
box.atomic tnx_isolation (#3593)
1 parent 6f85ec6 commit e0db7bf

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

doc/code_snippets/test/transactions/box_atomic_test.lua

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ g.before_each(function(cg)
1515
{ name = 'band_name', type = 'string' },
1616
{ name = 'year', type = 'unsigned' }
1717
})
18-
box.space.bands:create_index('primary', { parts = { 'id' } })
1918
end)
2019
end)
2120

@@ -26,18 +25,28 @@ end)
2625

2726
g.test_space_is_updated = function(cg)
2827
cg.server:exec(function()
28+
-- Create an index with the specified sequence --
29+
box.schema.sequence.create('id_sequence', { min = 1 })
30+
box.space.bands:create_index('primary', { parts = { 'id' }, sequence = 'id_sequence' })
31+
2932
-- Insert test data --
3033
box.space.bands:insert { 1, 'Roxette', 1986 }
3134
box.space.bands:insert { 2, 'Scorpions', 1965 }
3235
box.space.bands:insert { 3, 'Ace of Base', 1987 }
3336

34-
-- Commit the transaction implicitly --
35-
box.atomic(function()
36-
box.space.bands:insert { 4, 'The Beatles', 1960 }
37-
box.space.bands:replace { 1, 'Pink Floyd', 1965 }
38-
end)
37+
-- Define a function --
38+
local function insert_band(band_name, year)
39+
box.space.bands:insert { nil, band_name, year }
40+
end
41+
42+
-- Begin and commit the transaction implicitly --
43+
box.atomic(insert_band, 'The Beatles', 1960)
44+
45+
-- Begin the transaction with the specified isolation level --
46+
box.atomic({ txn_isolation = 'read-committed' },
47+
insert_band, 'The Rolling Stones', 1962)
3948

40-
t.assert_equals(box.space.bands:count(), 4)
41-
t.assert_equals(box.space.bands:select { 1 }[1], { 1, 'Pink Floyd', 1965 })
49+
t.assert_equals(box.space.bands:count(), 5)
50+
t.assert_equals(box.space.bands:select { 5 }[1], { 5, 'The Rolling Stones', 1962 })
4251
end)
4352
end

doc/code_snippets/test/transactions/box_commit_test.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ g.test_space_is_updated = function(cg)
3131
box.space.bands:insert { 2, 'Scorpions', 1965 }
3232
box.space.bands:insert { 3, 'Ace of Base', 1987 }
3333

34-
-- Commit the transaction --
34+
-- Begin and commit the transaction explicitly --
3535
box.begin()
3636
box.space.bands:insert { 4, 'The Beatles', 1960 }
3737
box.space.bands:replace { 1, 'Pink Floyd', 1965 }
3838
box.commit()
3939

40-
t.assert_equals(box.space.bands:count(), 4)
41-
t.assert_equals(box.space.bands:select { 1 }[1], { 1, 'Pink Floyd', 1965 })
40+
-- Begin the transaction with the specified isolation level --
41+
box.begin({ txn_isolation = 'read-committed' })
42+
box.space.bands:insert { 5, 'The Rolling Stones', 1962 }
43+
box.space.bands:replace { 1, 'The Doors', 1965 }
44+
box.commit()
45+
46+
t.assert_equals(box.space.bands:count(), 5)
47+
t.assert_equals(box.space.bands:select { 1 }[1], { 1, 'The Doors', 1965 })
4248
end)
4349
end

doc/reference/reference_lua/box_txn_management/atomic.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,35 @@
44
box.atomic()
55
================================================================================
66

7-
.. function:: box.atomic(tx-function [, function-arguments])
7+
.. function:: box.atomic([opts,] tx-function [, function-arguments])
88

99
Execute a function, acting as if the function starts with an implicit
1010
:doc:`/reference/reference_lua/box_txn_management/begin` and ends with an implicit
1111
:doc:`/reference/reference_lua/box_txn_management/commit` if successful, or ends with an implicit
1212
:doc:`/reference/reference_lua/box_txn_management/rollback` if there is an error.
1313

14-
:return: the result of the function passed to ``atomic()`` as an argument.
14+
:param table opts: (optional) transaction options:
15+
16+
* ``txn_isolation`` -- the :ref:`transaction isolation level <txn_mode_mvcc-options>`
17+
* ``timeout`` -- a timeout (in seconds), after which the transaction is rolled back
18+
19+
:param string tx-function: the function name
20+
21+
:param function-arguments: (optional) arguments passed to the function
22+
23+
:return: the result of the function passed to ``atomic()`` as an argument
1524

1625
**Possible errors:**
1726

1827
* error and abort the transaction in case of a conflict.
28+
* error and abort the transaction if the timeout is exceeded.
1929
* error if the operation fails to write to disk.
2030
* error if for some reason memory cannot be allocated.
2131

32+
2233
**Example**
2334

2435
.. literalinclude:: /code_snippets/test/transactions/box_atomic_test.lua
2536
:language: lua
26-
:lines: 29-38
37+
:lines: 28-47
2738
:dedent:

doc/reference/reference_lua/box_txn_management/begin.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
box.begin()
44
===========
55

6-
.. function:: box.begin([txn_isolation])
6+
.. function:: box.begin([opts])
77

88
Begin the transaction. Disable :ref:`implicit yields <app-implicit-yields>`
99
until the transaction ends.
@@ -12,16 +12,20 @@ box.begin()
1212
In effect the fiber which executes ``box.begin()`` is starting an "active
1313
multi-request transaction", blocking all other fibers.
1414

15-
:param txn_isolation: :ref:`transaction isolation level <txn_mode_mvcc-options>`
15+
:param table opts: (optional) transaction options:
16+
17+
* ``txn_isolation`` -- the :ref:`transaction isolation level <txn_mode_mvcc-options>`
18+
* ``timeout`` -- a timeout (in seconds), after which the transaction is rolled back
1619

1720
**Possible errors:**
1821

1922
* error if this operation is not permitted because there is already an active transaction.
2023
* error if for some reason memory cannot be allocated.
24+
* error and abort the transaction if the timeout is exceeded.
2125

2226
**Example**
2327

2428
.. literalinclude:: /code_snippets/test/transactions/box_commit_test.lua
2529
:language: lua
26-
:lines: 29-38
30+
:lines: 29-44
2731
:dedent:

doc/reference/reference_lua/box_txn_management/commit.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ box.commit()
1818

1919
.. literalinclude:: /code_snippets/test/transactions/box_commit_test.lua
2020
:language: lua
21-
:lines: 29-38
21+
:lines: 29-44
2222
:dedent:

0 commit comments

Comments
 (0)