]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4441] Add tranactions to PostgreSQL lease ops
authorThomas Markwalder <tmark@isc.org>
Fri, 17 Apr 2026 14:03:02 +0000 (10:03 -0400)
committerThomas Markwalder <tmark@isc.org>
Fri, 17 Apr 2026 17:21:44 +0000 (17:21 +0000)
/src/hooks/dhcp/mysql/mysql_lease_mgr.cc
/src/lib/pgsql/pgsql_connection.h
    PgSqlLeaseMgr::addLeaseCommon()
    PgSqlLeaseMgr::updateLeaseCommon()
    PgSqlLeaseMgr::deleteLeaseCommon()
    - added scoped transactions

src/hooks/dhcp/pgsql/pgsql_lease_mgr.cc
src/lib/pgsql/pgsql_connection.h
src/share/database/scripts/pgsql/dhcpdb_drop.pgsql

index eb6cfb26c07182484a7a2b0c8e6ab6048bb43bf5..03c83145329a82d014fe7f70182406d9b0d6cb9c 100644 (file)
@@ -1886,6 +1886,11 @@ PgSqlLeaseMgr::addLeaseCommon(PgSqlLeaseContextPtr& ctx,
                               StatementIndex stindex,
                               PsqlBindArray& bind_array,
                               bool outputs_row_count /* = false */) {
+    ScopedPgSqlTransactionPtr trans;
+    if (outputs_row_count) {
+        trans.reset(new PgSqlTransaction(ctx->conn_));
+    }
+
     PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
                                  tagged_statements[stindex].nbparams,
                                  &bind_array.values_[0],
@@ -1907,6 +1912,7 @@ PgSqlLeaseMgr::addLeaseCommon(PgSqlLeaseContextPtr& ctx,
     /// @todo not sure Postresql cares - Consume the results even though we don't care.
     if (outputs_row_count) {
         getRowCount(r);
+        trans->commit();
     }
 
     return (true);
@@ -2758,12 +2764,17 @@ PgSqlLeaseMgr::updateLeaseCommon(PgSqlLeaseContextPtr& ctx,
                                  &bind_array.lengths_[0],
                                  &bind_array.formats_[0], 0));
 
-    ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
+    ScopedPgSqlTransactionPtr trans;
+    if (outputs_row_count) {
+        trans.reset(new PgSqlTransaction(ctx->conn_));
+    }
 
+    ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
 
     int affected_rows = 0;
     if (outputs_row_count) {
         affected_rows = getRowCount(r);
+        trans->commit();
     } else {
         affected_rows = boost::lexical_cast<int>(PQcmdTuples(r));
     }
@@ -2783,7 +2794,8 @@ PgSqlLeaseMgr::updateLeaseCommon(PgSqlLeaseContextPtr& ctx,
     // Should not happen - primary key constraint should only have selected
     // one row.
     isc_throw(DbOperationError, "apparently updated more than one lease "
-              "that had the address " << lease->addr_.toText());
+              "that had the address " << lease->addr_.toText()
+              << ", row count: " << affected_rows);
 }
 
 void
@@ -2905,11 +2917,17 @@ PgSqlLeaseMgr::deleteLeaseCommon(PgSqlLeaseContextPtr& ctx,
                                  &bind_array.lengths_[0],
                                  &bind_array.formats_[0], 0));
 
+    ScopedPgSqlTransactionPtr trans;
+    if (outputs_row_count) {
+        trans.reset(new PgSqlTransaction(ctx->conn_));
+    }
+
     ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
 
     int affected_rows = 0;
     if (outputs_row_count) {
         affected_rows = getRowCount(r);
+        trans->commit();
     } else {
         affected_rows = boost::lexical_cast<int>(PQcmdTuples(r));
     }
index 2cf8b3656e5bb9e62cbd464e4486f204c0a3f307..c3d7418a1f52a3ea1f08037c53ff94c8a77af0c8 100644 (file)
@@ -188,6 +188,9 @@ private:
     bool committed_;
 };
 
+/// @brief Defines a scoped pointer to a transaction.
+typedef boost::scoped_ptr<PgSqlTransaction> ScopedPgSqlTransactionPtr;
+
 /// @brief Common PgSql Connector Pool
 ///
 /// This class provides common operations for PgSql database connection
index 5922de6aec661a827b2a5628c7365d1e6df945e6..9e38d58f2fb467d785f15017c62d9faa6168384c 100644 (file)
@@ -264,7 +264,6 @@ DROP FUNCTION IF EXISTS sflqDeleteLease4;
 DROP FUNCTION IF EXISTS sflqInsertLease6;
 DROP FUNCTION IF EXISTS sflqUpdateLease6;
 DROP FUNCTION IF EXISTS sflqDeleteLease6;
-DROP FUNCTION IF EXISTS sflqFakeRowCount;
 DROP TABLE IF EXISTS free_lease4;
 DROP TABLE IF EXISTS flq_pool4;
 DROP TABLE IF EXISTS free_lease6;