Parcourir la source

[4544] Prevent unexpected closing of connection to the host database.

Marcin Siodelski il y a 8 ans
Parent
commit
b7b75e4b00

+ 2 - 1
src/lib/dhcpsrv/cfg_db_access.cc

@@ -7,6 +7,7 @@
 #include <config.h>
 #include <config.h>
 #include <dhcpsrv/cfg_db_access.h>
 #include <dhcpsrv/cfg_db_access.h>
 #include <dhcpsrv/host_data_source_factory.h>
 #include <dhcpsrv/host_data_source_factory.h>
+#include <dhcpsrv/host_mgr.h>
 #include <dhcpsrv/lease_mgr_factory.h>
 #include <dhcpsrv/lease_mgr_factory.h>
 #include <sstream>
 #include <sstream>
 
 
@@ -39,7 +40,7 @@ CfgDbAccess::createManagers() const {
     // Recreate host data source.
     // Recreate host data source.
     HostDataSourceFactory::destroy();
     HostDataSourceFactory::destroy();
     if (!host_db_access_.empty()) {
     if (!host_db_access_.empty()) {
-        HostDataSourceFactory::create(getHostDbAccessString());
+        HostMgr::create(getHostDbAccessString());
     }
     }
 }
 }
 
 

+ 15 - 0
src/lib/dhcpsrv/tests/cfg_db_access_unittest.cc

@@ -7,6 +7,7 @@
 #include <config.h>
 #include <config.h>
 #include <dhcpsrv/cfg_db_access.h>
 #include <dhcpsrv/cfg_db_access.h>
 #include <dhcpsrv/host_data_source_factory.h>
 #include <dhcpsrv/host_data_source_factory.h>
+#include <dhcpsrv/host_mgr.h>
 #include <dhcpsrv/lease_mgr.h>
 #include <dhcpsrv/lease_mgr.h>
 #include <dhcpsrv/lease_mgr_factory.h>
 #include <dhcpsrv/lease_mgr_factory.h>
 #include <dhcpsrv/testutils/mysql_schema.h>
 #include <dhcpsrv/testutils/mysql_schema.h>
@@ -108,6 +109,20 @@ TEST_F(CfgMySQLDbAccessTest, createManagers) {
     ASSERT_NO_THROW({
     ASSERT_NO_THROW({
         HostDataSourcePtr& host_data_source =
         HostDataSourcePtr& host_data_source =
             HostDataSourceFactory::getHostDataSourcePtr();
             HostDataSourceFactory::getHostDataSourcePtr();
+        ASSERT_TRUE(host_data_source);
+        EXPECT_EQ("mysql", host_data_source->getType());
+    });
+
+    // Because of the lazy initialization of the HostMgr instance, it is
+    // possible that the first call to the instance() function tosses
+    // existing connection to the database created by the call to
+    // createManagers(). Let's make sure that this doesn't happen.
+    ASSERT_NO_THROW(HostMgr::instance());
+
+    ASSERT_NO_THROW({
+        HostDataSourcePtr& host_data_source =
+            HostDataSourceFactory::getHostDataSourcePtr();
+        ASSERT_TRUE(host_data_source);
         EXPECT_EQ("mysql", host_data_source->getType());
         EXPECT_EQ("mysql", host_data_source->getType());
     });
     });
 }
 }