diff options
author | deva <deva> | 2009-11-12 10:02:54 +0000 |
---|---|---|
committer | deva <deva> | 2009-11-12 10:02:54 +0000 |
commit | 93500a9656dffde57ab32410642c2dd74098b9f8 (patch) | |
tree | 92cc783992ec34b495f0f08354b435fa786e4454 /server/src/database.h | |
parent | 484d5c2b4dcf59f88093614903c6da5bec54ebda (diff) |
Make database connection shared among threads and make is thread safe.
Diffstat (limited to 'server/src/database.h')
-rw-r--r-- | server/src/database.h | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/server/src/database.h b/server/src/database.h index 396dda4..f253ba5 100644 --- a/server/src/database.h +++ b/server/src/database.h @@ -32,7 +32,7 @@ #include "pracrodao.h" #include "transaction.h" #include "template.h" - +#include "mutex.h" #include "debug.h" class Database { @@ -42,22 +42,31 @@ public: // Make a commit to the db void commitTransaction(std::string user, std::string patientid, Macro ¯o, Fields &fields, time_t now = time(NULL)) { + if(!dao) return; + mutex.lock(); PRACRO_DEBUG(db, "%s, %s, %s,...\n", user.c_str(), patientid.c_str(), macro.attributes["name"].c_str()); - if(dao) dao->commitTransaction(user, patientid, macro, fields, now); + dao->commitTransaction(user, patientid, macro, fields, now); + mutex.unlock(); } // Get a list of values from the db Values getValues(std::string patientid, Fieldnames &fieldnames, time_t oldest = 0) { + if(!dao) return Values(); + mutex.lock(); PRACRO_DEBUG(db, "%s, <%u fieldnames>, %ld\n", patientid.c_str(), fieldnames.size(), oldest); - if(dao) return dao->getLatestValues(patientid, NULL, fieldnames, oldest); - else return Values(); + Values values = dao->getLatestValues(patientid, NULL, fieldnames, oldest); + mutex.unlock(); + return values; } // Check if a macro has been committed. bool checkMacro(std::string patientid, std::string macro, time_t oldest = 0) { PRACRO_DEBUG(db, "%s, %s, %ld\n", patientid.c_str(), macro.c_str(), oldest); if(!dao) return false; - return dao->nrOfCommits(patientid, macro, oldest) > 0; + mutex.lock(); + bool res = dao->nrOfCommits(patientid, macro, oldest) > 0; + mutex.unlock(); + return res; } // Get latest resume of a given macro @@ -66,7 +75,9 @@ public: if(!dao) return ""; Fieldnames fn; fn.push_back("journal.resume"); + mutex.lock(); Values v = dao->getLatestValues(patientid, ¯o, fn, oldest); + mutex.unlock(); Values::iterator i = v.find("journal.resume"); if(i != v.end()) return i->second.value; else return ""; @@ -75,13 +86,17 @@ public: void addFieldname(std::string name, std::string description) { if(!dao) return; + mutex.lock(); dao->addFieldname(name, description); + mutex.unlock(); } void delFieldname(std::string name) { if(!dao) return; + mutex.lock(); dao->delFieldname(name); + mutex.unlock(); } std::vector<Fieldname> getFieldnames() @@ -90,11 +105,15 @@ public: std::vector<Fieldname> fieldnames; return fieldnames; } - return dao->getFieldnames(); + mutex.lock(); + std::vector<Fieldname> fieldnames = dao->getFieldnames(); + mutex.unlock(); + return fieldnames; } private: PracroDAO *dao; + Mutex mutex; }; #endif/*__PRACRO_DATABASE_H__*/ |