summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/tdb/common/transaction.c8
-rw-r--r--lib/tdb/include/tdb.h18
-rw-r--r--lib/tdb/test/run-traverse-in-transaction.c3
3 files changed, 29 insertions, 0 deletions
diff --git a/lib/tdb/common/transaction.c b/lib/tdb/common/transaction.c
index 420e754d9b6..9b975eaa047 100644
--- a/lib/tdb/common/transaction.c
+++ b/lib/tdb/common/transaction.c
@@ -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
diff --git a/lib/tdb/include/tdb.h b/lib/tdb/include/tdb.h
index 4bfa1a4dd85..535f07ac00a 100644
--- a/lib/tdb/include/tdb.h
+++ b/lib/tdb/include/tdb.h
@@ -651,6 +651,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.
*
* All operations after the transaction start can either be committed with
diff --git a/lib/tdb/test/run-traverse-in-transaction.c b/lib/tdb/test/run-traverse-in-transaction.c
index 17d64129642..d187b9b58b8 100644
--- a/lib/tdb/test/run-traverse-in-transaction.c
+++ b/lib/tdb/test/run-traverse-in-transaction.c
@@ -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);