]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#888,!573] use thread_local instead of ThreadResource 888-create-pgsql-connection-thread-handle
authorRazvan Becheriu <razvan@isc.org>
Wed, 6 Nov 2019 16:30:36 +0000 (18:30 +0200)
committerRazvan Becheriu <razvan@isc.org>
Wed, 6 Nov 2019 16:30:36 +0000 (18:30 +0200)
src/lib/pgsql/pgsql_connection.cc
src/lib/pgsql/pgsql_connection.h

index 38318843f98019f2d990789a35b27c0e8e2320e9..a2d7e6c56ed7c51cc736378981f1399446b1cf1c 100644 (file)
@@ -188,6 +188,18 @@ PgSqlTransaction::commit() {
     committed_ = true;
 }
 
+PgSqlHolder&
+PgSqlConnection::handle() const {
+    thread_local std::shared_ptr<PgSqlHolder> result(std::make_shared<PgSqlHolder>());
+    if (connected_) {
+        result->openDatabase(*(const_cast<PgSqlConnection*>(this)));
+    }
+    if (prepared_) {
+        result->prepareStatements(*(const_cast<PgSqlConnection*>(this)));
+    }
+    return *result;
+}
+
 PgSqlConnection::~PgSqlConnection() {
     statements_.clear();
     handle().clear();
index 54dd23cac118afcc79372c4aed211cf138c45738..c344f71d59a5810010290624b934edfc16764bb0 100644 (file)
@@ -426,23 +426,11 @@ public:
     ///
     /// This field is public, because it is used heavily from PgSqlLeaseMgr
     /// and from PgSqlHostDataSource.
-    PgSqlHolder& handle() const {
-        auto result = handles_.resource();
-        if (connected_) {
-            result->openDatabase(*(const_cast<PgSqlConnection*>(this)));
-        }
-        if (prepared_) {
-            result->prepareStatements(*(const_cast<PgSqlConnection*>(this)));
-        }
-        return *result;
-    }
-
+    PgSqlHolder& handle() const;
 private:
     bool connected_;     ///< Flag to indicate openDatabase has been called
 
     bool prepared_;      ///< Flag to indicate prepareStatements has been called
-
-    mutable isc::dhcp::ThreadResource<PgSqlHolder> handles_;
 };
 
 }  // namespace db