From: Thomas Markwalder Date: Fri, 17 Apr 2026 14:03:02 +0000 (-0400) Subject: [#4441] Add tranactions to PostgreSQL lease ops X-Git-Tag: Kea-3.1.8~52 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=e137bd494f30bfe28066fa098698498b5366f2cf;p=thirdparty%2Fkea.git [#4441] Add tranactions to PostgreSQL lease ops /src/hooks/dhcp/mysql/mysql_lease_mgr.cc /src/lib/pgsql/pgsql_connection.h PgSqlLeaseMgr::addLeaseCommon() PgSqlLeaseMgr::updateLeaseCommon() PgSqlLeaseMgr::deleteLeaseCommon() - added scoped transactions --- diff --git a/src/hooks/dhcp/pgsql/pgsql_lease_mgr.cc b/src/hooks/dhcp/pgsql/pgsql_lease_mgr.cc index eb6cfb26c0..03c8314532 100644 --- a/src/hooks/dhcp/pgsql/pgsql_lease_mgr.cc +++ b/src/hooks/dhcp/pgsql/pgsql_lease_mgr.cc @@ -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(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(PQcmdTuples(r)); } diff --git a/src/lib/pgsql/pgsql_connection.h b/src/lib/pgsql/pgsql_connection.h index 2cf8b3656e..c3d7418a1f 100644 --- a/src/lib/pgsql/pgsql_connection.h +++ b/src/lib/pgsql/pgsql_connection.h @@ -188,6 +188,9 @@ private: bool committed_; }; +/// @brief Defines a scoped pointer to a transaction. +typedef boost::scoped_ptr ScopedPgSqlTransactionPtr; + /// @brief Common PgSql Connector Pool /// /// This class provides common operations for PgSql database connection diff --git a/src/share/database/scripts/pgsql/dhcpdb_drop.pgsql b/src/share/database/scripts/pgsql/dhcpdb_drop.pgsql index 5922de6aec..9e38d58f2f 100644 --- a/src/share/database/scripts/pgsql/dhcpdb_drop.pgsql +++ b/src/share/database/scripts/pgsql/dhcpdb_drop.pgsql @@ -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;