comparison Framework/Common/DatabaseManager.cpp @ 226:a4918d57435c

DatabaseManager doesn't IDatabaseFactory anymore
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 02 Apr 2021 19:23:36 +0200
parents 90eb271f85b2
children 0a9b48d19643
comparison
equal deleted inserted replaced
225:94c9908e6aca 226:a4918d57435c
29 29
30 #include <boost/thread.hpp> 30 #include <boost/thread.hpp>
31 31
32 namespace OrthancDatabases 32 namespace OrthancDatabases
33 { 33 {
34 IDatabase& DatabaseManager::GetDatabase()
35 {
36 unsigned int maxConnectionRetries = 10;
37 unsigned int connectionRetryInterval = 5;
38 unsigned int count = 0;
39
40 factory_->GetConnectionRetriesParameters(maxConnectionRetries, connectionRetryInterval);
41
42 while (database_.get() == NULL)
43 {
44 transaction_.reset(NULL);
45
46 try
47 {
48 database_.reset(factory_->Open());
49 }
50 catch (Orthanc::OrthancException& e)
51 {
52 if (e.GetErrorCode() == Orthanc::ErrorCode_DatabaseUnavailable)
53 {
54 count ++;
55
56 if (count <= maxConnectionRetries)
57 {
58 LOG(WARNING) << "Database is currently unavailable, retrying...";
59 boost::this_thread::sleep(boost::posix_time::seconds(connectionRetryInterval));
60 continue;
61 }
62 else
63 {
64 LOG(ERROR) << "Timeout when connecting to the database, giving up";
65 }
66 }
67
68 throw;
69 }
70 }
71
72 if (database_.get() == NULL ||
73 database_->GetDialect() != dialect_)
74 {
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
76 }
77 else
78 {
79 return *database_;
80 }
81 }
82
83
84 void DatabaseManager::Close() 34 void DatabaseManager::Close()
85 { 35 {
86 LOG(TRACE) << "Closing the connection to the database"; 36 LOG(TRACE) << "Closing the connection to the database";
87 37
88 // Rollback active transaction, if any 38 // Rollback active transaction, if any
198 } 148 }
199 } 149 }
200 } 150 }
201 151
202 152
203 DatabaseManager::DatabaseManager(IDatabaseFactory* factory) : // Takes ownership 153 DatabaseManager::DatabaseManager(IDatabase* database) :
204 factory_(factory) 154 database_(database)
205 { 155 {
206 if (factory == NULL) 156 if (database == NULL)
207 { 157 {
208 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 158 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
209 } 159 }
210 160
211 dialect_ = factory->GetDialect(); 161 dialect_ = database->GetDialect();
212 } 162 }
213 163
214 164
215 void DatabaseManager::StartTransaction(TransactionType type) 165 void DatabaseManager::StartTransaction(TransactionType type)
216 { 166 {