From a2372471324ac23ad8675cf14c0b1886080c6846 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Thu, 20 Jan 2011 14:16:23 -0500
Subject: [PATCH 1/2] Wrap cleanup task in a sysdb transaction

---
 src/providers/ldap/ldap_id_cleanup.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/src/providers/ldap/ldap_id_cleanup.c b/src/providers/ldap/ldap_id_cleanup.c
index 293681cf4b212eef6db6dd930abefec8c316f17c..ced46522cce9ddf07ca0a16927c87a0d2f2092ab 100644
--- a/src/providers/ldap/ldap_id_cleanup.c
+++ b/src/providers/ldap/ldap_id_cleanup.c
@@ -181,6 +181,7 @@ struct tevent_req *ldap_id_cleanup_send(TALLOC_CTX *memctx,
     struct global_cleanup_state *state;
     struct tevent_req *req;
     int ret;
+    bool in_transaction = false;
 
     req = tevent_req_create(memctx, &state, struct global_cleanup_state);
     if (!req) return NULL;
@@ -190,6 +191,11 @@ struct tevent_req *ldap_id_cleanup_send(TALLOC_CTX *memctx,
 
     ctx->last_purge = tevent_timeval_current();
 
+    ret = sysdb_transaction_start(state->ctx->be->sysdb);
+    if (ret != EOK) {
+        goto fail;
+    }
+
     ret = cleanup_users(state, state->ctx);
     if (ret && ret != ENOENT) {
         goto fail;
@@ -202,6 +208,11 @@ struct tevent_req *ldap_id_cleanup_send(TALLOC_CTX *memctx,
         goto fail;
     }
 
+    ret = sysdb_transaction_commit(state->ctx->be->sysdb);
+    if (ret != EOK) {
+        goto fail;
+    }
+
     tevent_req_done(req);
     tevent_req_post(req, ev);
     return req;
@@ -209,6 +220,15 @@ struct tevent_req *ldap_id_cleanup_send(TALLOC_CTX *memctx,
 fail:
     DEBUG(1, ("Failed to cleanup caches (%d [%s]), retrying later!\n",
               (int)ret, strerror(ret)));
+    if (in_transaction) {
+        ret = sysdb_transaction_cancel(state->ctx->be->sysdb);
+        if (ret != EOK) {
+            DEBUG(1, ("Could not cancel transaction\n"));
+            tevent_req_error(req, ret);
+            tevent_req_post(req, ev);
+            return req;
+        }
+    }
     tevent_req_done(req);
     tevent_req_post(req, ev);
     return req;
-- 
1.7.3.5

