changeset 417:15bfd9a76f8d pg-transactions

merge
author Alain Mazy <am@osimis.io>
date Fri, 23 Jun 2023 14:26:58 +0200
parents d2b5d9c92214 (diff) 6b16914b6655 (current diff)
children a7f0f27fe33c
files Framework/PostgreSQL/PostgreSQLDatabase.h Framework/PostgreSQL/PostgreSQLParameters.cpp Framework/PostgreSQL/PostgreSQLParameters.h Framework/PostgreSQL/PostgreSQLTransaction.cpp PostgreSQL/NEWS
diffstat 5 files changed, 69 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/PostgreSQL/PostgreSQLDatabase.h	Thu Jun 22 21:15:50 2023 +0200
+++ b/Framework/PostgreSQL/PostgreSQLDatabase.h	Fri Jun 23 14:26:58 2023 +0200
@@ -36,6 +36,7 @@
   private:
     friend class PostgreSQLStatement;
     friend class PostgreSQLLargeObject;
+    friend class PostgreSQLTransaction;
 
     class Factory;
 
@@ -99,5 +100,17 @@
     static IDatabaseFactory* CreateDatabaseFactory(const PostgreSQLParameters& parameters);
 
     static PostgreSQLDatabase* CreateDatabaseConnection(const PostgreSQLParameters& parameters);
+
+  protected:
+    const std::string& GetReadWriteTransactionStatement() const
+    {
+      return parameters_.GetReadWriteTransactionStatement();
+    }
+
+    const std::string& GetReadOnlyTransactionStatement() const
+    {
+      return parameters_.GetReadOnlyTransactionStatement();
+    }
+
   };
 }
--- a/Framework/PostgreSQL/PostgreSQLParameters.cpp	Thu Jun 22 21:15:50 2023 +0200
+++ b/Framework/PostgreSQL/PostgreSQLParameters.cpp	Fri Jun 23 14:26:58 2023 +0200
@@ -42,6 +42,8 @@
     lock_ = true;
     maxConnectionRetries_ = 10;
     connectionRetryInterval_ = 5;
+    readWriteTransactionStatement_ = "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE";
+    readOnlyTransactionStatement_ = "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY";
   }
 
 
@@ -96,6 +98,17 @@
 
     maxConnectionRetries_ = configuration.GetUnsignedIntegerValue("MaximumConnectionRetries", 10);
     connectionRetryInterval_ = configuration.GetUnsignedIntegerValue("ConnectionRetryInterval", 5);
+
+    if (configuration.LookupStringValue(s, "ReadWriteTransactionStatement"))
+    {
+      SetReadWriteTransactionStatement(s);
+    }
+
+    if (configuration.LookupStringValue(s, "ReadOnlyTransactionStatement"))
+    {
+      SetReadOnlyTransactionStatement(s);
+    }
+
   }
 
 
--- a/Framework/PostgreSQL/PostgreSQLParameters.h	Thu Jun 22 21:15:50 2023 +0200
+++ b/Framework/PostgreSQL/PostgreSQLParameters.h	Fri Jun 23 14:26:58 2023 +0200
@@ -43,6 +43,8 @@
     bool         lock_;
     unsigned int maxConnectionRetries_;
     unsigned int connectionRetryInterval_;
+    std::string  readWriteTransactionStatement_;
+    std::string  readOnlyTransactionStatement_;
 
     void Reset();
 
@@ -125,6 +127,26 @@
       return connectionRetryInterval_;
     }
 
+    void SetReadWriteTransactionStatement(const std::string& statement)
+    {
+      readWriteTransactionStatement_ = statement;
+    }
+
+    void SetReadOnlyTransactionStatement(const std::string& statement)
+    {
+      readOnlyTransactionStatement_ = statement;
+    }
+
+    const std::string& GetReadWriteTransactionStatement() const
+    {
+      return readWriteTransactionStatement_;
+    }
+
+    const std::string& GetReadOnlyTransactionStatement() const
+    {
+      return readOnlyTransactionStatement_;
+    }
+
     void Format(std::string& target) const;
   };
 }
--- a/Framework/PostgreSQL/PostgreSQLTransaction.cpp	Thu Jun 22 21:15:50 2023 +0200
+++ b/Framework/PostgreSQL/PostgreSQLTransaction.cpp	Fri Jun 23 14:26:58 2023 +0200
@@ -70,12 +70,22 @@
     switch (type)
     {
       case TransactionType_ReadWrite:
-        database_.ExecuteMultiLines("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE");
-        break;
+      {
+        const std::string& statement = database_.GetReadWriteTransactionStatement();
+        if (!statement.empty()) // if not defined, will use the default DB transaction isolation level
+        {
+          database_.ExecuteMultiLines(statement);
+        }
+       }; break;
 
       case TransactionType_ReadOnly:
-        database_.ExecuteMultiLines("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY");
-        break;
+      {
+        const std::string& statement = database_.GetReadOnlyTransactionStatement();
+        if (!statement.empty()) // if not defined, will use the default DB transaction isolation level
+        {
+          database_.ExecuteMultiLines(statement);
+        }
+       }; break;
 
       default:
         throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
--- a/PostgreSQL/NEWS	Thu Jun 22 21:15:50 2023 +0200
+++ b/PostgreSQL/NEWS	Fri Jun 23 14:26:58 2023 +0200
@@ -13,6 +13,13 @@
 * Upgraded dependencies for static builds (notably on Windows and LSB):
   - openssl 3.1.0
 
+* Experimental debug feature:
+  Introduced 2 new configurations with these default values:
+    "ReadOnlyTransactionStatement": "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY"
+    "ReadWriteTransactionStatement": "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE"
+  You can now customize the transaction isolation level.
+  If setting these values to "", no statement is run and therefore, the default DB or server
+  isolation level is used.
 
 Release 4.0 (2021-04-22)
 ========================