tdb: Add new function tdb_transaction_active()
authorAndrew Bartlett <abartlet@samba.org>
Wed, 26 Apr 2017 20:34:56 +0000 (08:34 +1200)
committerStefan Metzmacher <metze@samba.org>
Sun, 2 Jul 2017 15:35:19 +0000 (17:35 +0200)
This will allow callers to avoid their own reference counting of transactions.

Additionally, this will always line up with the acutal transaction state, even
in the error cases where tdb can cancel the transaction

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
lib/tdb/common/transaction.c
lib/tdb/include/tdb.h
lib/tdb/test/run-traverse-in-transaction.c

index 420e754d9b6e36c1aabcc6bb09ac296cd6f61829..9b975eaa0476ed783caf2b201efcc195a4e9cfe7 100644 (file)
@@ -412,6 +412,14 @@ static const struct tdb_methods transaction_methods = {
        transaction_expand_file,
 };
 
+/*
+ * Is a transaction currently active on this context?
+ *
+ */
+_PUBLIC_ bool tdb_transaction_active(struct tdb_context *tdb)
+{
+       return (tdb->transaction != NULL);
+}
 
 /*
   start a tdb transaction. No token is returned, as only a single
index 4bfa1a4dd85148836f543164f9af303caa489375..535f07ac00adc09784ef57dd0a7a24d76f280593 100644 (file)
@@ -650,6 +650,24 @@ tdb_log_func tdb_log_fn(struct tdb_context *tdb);
  */
 void *tdb_get_logging_private(struct tdb_context *tdb);
 
+/**
+ * @brief Is a transaction active?
+ *
+ * It is helpful for the application to know if a transaction is
+ * active, rather than needing to maintain an application-level reference
+ * count.
+ *
+ * @param[in]  tdb      The database to start the transaction.
+ *
+ * @return              true if there is a transaction active, false otherwise
+ *
+ * @see tdb_transaction_start()
+ * @see tdb_transaction_prepare_commit()
+ * @see tdb_transaction_commit()
+ * @see tdb_transaction_cancel()
+ */
+bool tdb_transaction_active(struct tdb_context *tdb);
+
 /**
  * @brief Start a transaction.
  *
index 17d641296422ece600b29b1c979d1e67b8f9dbce..d187b9b58b8d1f50104f965c5b466e1163563301 100644 (file)
@@ -63,7 +63,9 @@ int main(int argc, char *argv[])
 
        ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
 
+       ok1(tdb_transaction_active(tdb) == 0);
        ok1(tdb_transaction_start(tdb) == 0);
+       ok1(tdb_transaction_active(tdb) == 1);
        ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
            == WOULD_HAVE_BLOCKED);
        tdb_traverse(tdb, traverse, NULL);
@@ -77,6 +79,7 @@ int main(int argc, char *argv[])
        ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
            == WOULD_HAVE_BLOCKED);
        ok1(tdb_transaction_commit(tdb) == 0);
+       ok1(tdb_transaction_active(tdb) == 0);
        /* Now we should be fine. */
        ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
            == SUCCESS);