From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id D92E32A8DD for ; Thu, 6 Sep 2018 21:00:24 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id gzJ3TyGah2Jx for ; Thu, 6 Sep 2018 21:00:24 -0400 (EDT) Received: from forward104o.mail.yandex.net (forward104o.mail.yandex.net [37.140.190.179]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 3DE2F2A8DB for ; Thu, 6 Sep 2018 21:00:24 -0400 (EDT) From: Roman Khabibov Subject: [tarantool-patches] [PATCH] box: add a method to check if transaction is open Date: Fri, 7 Sep 2018 04:00:19 +0300 Message-Id: <1536282019-17978-1-git-send-email-roman.habibov1@yandex.ru> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: v.shpilevoy@tarantool.org Add method box.is_in_txn() Closes: #3518. --- Branch: https://github.com/tarantool/tarantool/tree/romankhabibov/gh-3518-check-if-txn Issue: https://github.com/tarantool/tarantool/issues/3518 extra/exports | 1 + src/box/lua/schema.lua | 6 ++++++ src/box/txn.c | 6 ++++++ src/box/txn.h | 8 ++++++++ test/box/misc.result | 1 + test/box/transaction.result | 18 ++++++++++++++++++ test/box/transaction.test.lua | 8 ++++++++ test/engine/savepoint.result | 12 ++++++------ 8 files changed, 54 insertions(+), 6 deletions(-) diff --git a/extra/exports b/extra/exports index 319d2c4..2f408d9 100644 --- a/extra/exports +++ b/extra/exports @@ -136,6 +136,7 @@ luaT_state luaT_tolstring box_txn box_txn_begin +box_is_in_txn box_txn_commit box_txn_savepoint box_txn_rollback diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua index 74b7064..e1526ac 100644 --- a/src/box/lua/schema.lua +++ b/src/box/lua/schema.lua @@ -68,6 +68,8 @@ ffi.cdef[[ box_txn_id(); int box_txn_begin(); + bool + box_is_in_txn(); /** \endcond public */ typedef struct txn_savepoint box_txn_savepoint_t; @@ -321,6 +323,10 @@ box.begin = function() end end +box.is_in_txn = function() + return { builtin.box_is_in_txn() } +end + box.savepoint = function() local csavepoint = builtin.box_txn_savepoint() if csavepoint == nil then diff --git a/src/box/txn.c b/src/box/txn.c index 617ceb8..51b50bf 100644 --- a/src/box/txn.c +++ b/src/box/txn.c @@ -447,6 +447,12 @@ box_txn_begin() return 0; } +bool +box_is_in_txn() +{ + return box_txn(); +} + int box_txn_commit() { diff --git a/src/box/txn.h b/src/box/txn.h index e74c14d..e587129 100644 --- a/src/box/txn.h +++ b/src/box/txn.h @@ -382,6 +382,14 @@ API_EXPORT int box_txn_commit(void); /** + * Check if transaction opened in the current fiber. + * @retval true Transaction is opened. + * @retval false Transaction is closed. + */ +API_EXPORT bool +box_is_in_txn(void); + +/** * Rollback the current transaction. * May fail if called from a nested * statement. diff --git a/test/box/misc.result b/test/box/misc.result index d9f8d4e..325af3b 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -67,6 +67,7 @@ t - index - info - internal + - is_in_txn - on_commit - on_rollback - once diff --git a/test/box/transaction.result b/test/box/transaction.result index e024084..fb7ebbd 100644 --- a/test/box/transaction.result +++ b/test/box/transaction.result @@ -12,6 +12,24 @@ test_run:cmd("setopt delimiter ';'") box.begin() box.commit(); --- ... +-- gh-3518 no active transaction - false +box.is_in_txn(); +--- +- - false +... +-- start transaction +box.begin(); +--- +... +-- active transaction - true +box.is_in_txn(); +--- +- - true +... +-- close transaction +box.commit(); +--- +... -- double begin box.begin() box.begin(); --- diff --git a/test/box/transaction.test.lua b/test/box/transaction.test.lua index e1d258e..79a2c0b 100644 --- a/test/box/transaction.test.lua +++ b/test/box/transaction.test.lua @@ -3,6 +3,14 @@ test_run = env.new() test_run:cmd("setopt delimiter ';'") -- empty transaction - ok box.begin() box.commit(); +-- gh-3518 no active transaction - false +box.is_in_txn(); +-- start transaction +box.begin(); +-- active transaction - true +box.is_in_txn(); +-- close transaction +box.commit(); -- double begin box.begin() box.begin(); -- no active transaction since exception rolled it back diff --git a/test/engine/savepoint.result b/test/engine/savepoint.result index a62a2e1..00d281b 100644 --- a/test/engine/savepoint.result +++ b/test/engine/savepoint.result @@ -14,7 +14,7 @@ s1 = box.savepoint() ... box.rollback_to_savepoint(s1) --- -- error: 'builtin/box/schema.lua:340: Usage: box.rollback_to_savepoint(savepoint)' +- error: 'builtin/box/schema.lua:346: Usage: box.rollback_to_savepoint(savepoint)' ... box.begin() s1 = box.savepoint() --- @@ -323,27 +323,27 @@ test_run:cmd("setopt delimiter ''"); ok1, errmsg1 --- - false -- 'builtin/box/schema.lua:340: Usage: box.rollback_to_savepoint(savepoint)' +- 'builtin/box/schema.lua:346: Usage: box.rollback_to_savepoint(savepoint)' ... ok2, errmsg2 --- - false -- 'builtin/box/schema.lua:340: Usage: box.rollback_to_savepoint(savepoint)' +- 'builtin/box/schema.lua:346: Usage: box.rollback_to_savepoint(savepoint)' ... ok3, errmsg3 --- - false -- 'builtin/box/schema.lua:340: Usage: box.rollback_to_savepoint(savepoint)' +- 'builtin/box/schema.lua:346: Usage: box.rollback_to_savepoint(savepoint)' ... ok4, errmsg4 --- - false -- 'builtin/box/schema.lua:340: Usage: box.rollback_to_savepoint(savepoint)' +- 'builtin/box/schema.lua:346: Usage: box.rollback_to_savepoint(savepoint)' ... ok5, errmsg5 --- - false -- 'builtin/box/schema.lua:340: Usage: box.rollback_to_savepoint(savepoint)' +- 'builtin/box/schema.lua:346: Usage: box.rollback_to_savepoint(savepoint)' ... s:select{} --- -- 2.7.4