comparison Framework/PostgreSQL/PostgreSQLDatabase.cpp @ 12:41543239072d

transactions for storage area
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Jul 2018 18:34:56 +0200
parents 7cea966b6829
children b2ff1cd2907a
comparison
equal deleted inserted replaced
11:0217486720b3 12:41543239072d
94 } 94 }
95 95
96 LOG(ERROR) << "PostgreSQL error: " << message; 96 LOG(ERROR) << "PostgreSQL error: " << message;
97 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable); 97 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable);
98 } 98 }
99 }
99 100
100 if (parameters_.HasLock()) 101
102 void PostgreSQLDatabase::AdvisoryLock(int32_t lock)
103 {
104 PostgreSQLTransaction transaction(*this);
105
106 PostgreSQLStatement s(*this, "select pg_try_advisory_lock(" +
107 boost::lexical_cast<std::string>(lock) + ");");
108
109 PostgreSQLResult result(s);
110 if (result.IsDone() ||
111 !result.GetBoolean(0))
101 { 112 {
102 PostgreSQLTransaction transaction(*this); 113 LOG(ERROR) << "The PostgreSQL database is locked by another instance of Orthanc";
114 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
115 }
103 116
104 int32_t lock = 42; // Some arbitrary constant 117 transaction.Commit();
105
106 PostgreSQLStatement s(*this, "select pg_try_advisory_lock(" +
107 boost::lexical_cast<std::string>(lock) + ");");
108
109 PostgreSQLResult result(s);
110 if (result.IsDone() ||
111 !result.GetBoolean(0))
112 {
113 LOG(ERROR) << "The PostgreSQL database is locked by another instance of Orthanc";
114 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
115 }
116
117 transaction.Commit();
118 }
119 } 118 }
120 119
121 120
122 void PostgreSQLDatabase::Execute(const std::string& sql) 121 void PostgreSQLDatabase::Execute(const std::string& sql)
123 { 122 {