From 1da05729779a9453e2b18191bbc9568cbbcddf75 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Tue, 30 Aug 2022 20:58:49 -0400 Subject: [PATCH] DbLib: Retry connection if first attempt fails --- .../database/sch_database_plugin.cpp | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/eeschema/sch_plugins/database/sch_database_plugin.cpp b/eeschema/sch_plugins/database/sch_database_plugin.cpp index 5ca1c584b4..6eac594fef 100644 --- a/eeschema/sch_plugins/database/sch_database_plugin.cpp +++ b/eeschema/sch_plugins/database/sch_database_plugin.cpp @@ -162,26 +162,41 @@ bool SCH_DATABASE_PLUGIN::CheckHeader( const wxString& aFileName ) void SCH_DATABASE_PLUGIN::ensureSettings( const wxString& aSettingsPath ) { + auto tryLoad = + [&]() + { + if( !m_settings->LoadFromFile() ) + { + wxString msg = wxString::Format( + _( "Could not load database library: settings file %s missing or invalid" ), + aSettingsPath ); + + THROW_IO_ERROR( msg ); + } + }; + if( !m_settings && !aSettingsPath.IsEmpty() ) { std::string path( aSettingsPath.ToUTF8() ); m_settings = std::make_unique( path ); m_settings->SetReadOnly( true ); - if( !m_settings->LoadFromFile() ) - { - wxString msg = wxString::Format( - _( "Could not load database library: settings file %s missing or invalid" ), - aSettingsPath ); - - THROW_IO_ERROR( msg ); - } + tryLoad(); } - else if( !m_settings ) + else if( !m_conn && m_settings ) + { + // If we have valid settings but no connection yet; reload settings in case user is editing + tryLoad(); + } + else if( m_conn && m_settings && !aSettingsPath.IsEmpty() ) { wxASSERT_MSG( aSettingsPath == m_settings->GetFilename(), "Path changed for database library without re-initializing plugin!" ); } + else if( !m_settings ) + { + wxLogTrace( traceDatabase, wxT( "ensureSettings: no settings but no valid path!" ) ); + } } @@ -217,6 +232,8 @@ void SCH_DATABASE_PLUGIN::ensureConnection() m_settings->m_Source.dsn, m_conn->GetLastError() ); + m_conn.reset(); + THROW_IO_ERROR( msg ); } } @@ -333,4 +350,4 @@ LIB_SYMBOL* SCH_DATABASE_PLUGIN::loadSymbolFromRow( const wxString& aSymbolName, } return symbol; -} \ No newline at end of file +}