diff --git a/swh/core/db/__init__.py b/swh/core/db/__init__.py
index 122b36397c0b2eebcad5afc8545380e3ec3c6e58..4383454e3af42c21786c88794439b61667ed5560 100644
--- a/swh/core/db/__init__.py
+++ b/swh/core/db/__init__.py
@@ -184,14 +184,9 @@ class BaseDb:
             a psycopg cursor
 
         """
-        with self.conn.cursor() as cur:
-            try:
+        with self.conn.transaction():
+            with self.conn.cursor() as cur:
                 yield cur
-                self.conn.commit()
-            except Exception:
-                if not self.conn.closed:
-                    self.conn.rollback()
-                raise
 
     def copy_to(
         self,