comparison Framework/Common/DatabaseManager.cpp @ 141:0b3e9ee53c46

Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
author Alain Mazy <alain@mazy.be>
date Mon, 03 Feb 2020 22:29:51 +0100
parents 4cd7e45b671e
children 063aa53b5917
comparison
equal deleted inserted replaced
140:4cd7e45b671e 141:0b3e9ee53c46
30 30
31 namespace OrthancDatabases 31 namespace OrthancDatabases
32 { 32 {
33 IDatabase& DatabaseManager::GetDatabase() 33 IDatabase& DatabaseManager::GetDatabase()
34 { 34 {
35 static const unsigned int MAX_CONNECTION_ATTEMPTS = 10; // TODO: Parameter 35 unsigned int maxConnectionRetries = 10;
36 36 unsigned int connectionRetryInterval = 5;
37 unsigned int count = 0; 37 unsigned int count = 0;
38
39 factory_->GetConnectionRetriesParameters(maxConnectionRetries, connectionRetryInterval);
38 40
39 while (database_.get() == NULL) 41 while (database_.get() == NULL)
40 { 42 {
41 transaction_.reset(NULL); 43 transaction_.reset(NULL);
42 44
48 { 50 {
49 if (e.GetErrorCode() == Orthanc::ErrorCode_DatabaseUnavailable) 51 if (e.GetErrorCode() == Orthanc::ErrorCode_DatabaseUnavailable)
50 { 52 {
51 count ++; 53 count ++;
52 54
53 if (count <= MAX_CONNECTION_ATTEMPTS) 55 if (count <= maxConnectionRetries)
54 { 56 {
55 LOG(WARNING) << "Database is currently unavailable, retrying..."; 57 LOG(WARNING) << "Database is currently unavailable, retrying...";
56 boost::this_thread::sleep(boost::posix_time::seconds(1)); 58 boost::this_thread::sleep(boost::posix_time::seconds(connectionRetryInterval));
57 continue; 59 continue;
58 } 60 }
59 else 61 else
60 { 62 {
61 LOG(ERROR) << "Timeout when connecting to the database, giving up"; 63 LOG(ERROR) << "Timeout when connecting to the database, giving up";