void PostgreSQLDatabase::Open() { if (pg_ != NULL) { switch (PQstatus(reinterpret_cast(pg_))) { case CONNECTION_OK: // Already connected and CONNECTION_OK return; default: // connection not ok PQreset(pg_); if(PQstatus(reinterpret_cast(pg_)) == CONNECTION_OK) { LOG(INFO) << "Reconnected to the database"; return; } else { LOG(WARNING) << "Reconnection did not immediatelly return an OK connection"; PQfinish(reinterpret_cast(pg_)); } break; } } std::string s; parameters_.Format(s); if (pg_) PQfinish(reinterpret_cast(pg_)); pg_ = PQconnectdb(s.c_str()); if (pg_ == NULL || PQstatus(reinterpret_cast(pg_)) != CONNECTION_OK) { std::string message; if (pg_) { message = PQerrorMessage(reinterpret_cast(pg_)); PQfinish(reinterpret_cast(pg_)); pg_ = NULL; } LOG(ERROR) << "PostgreSQL error: " << message; throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable); } }