comparison PostgreSQL/Plugins/PostgreSQLStorageArea.cpp @ 136:3266785d5627

cleaning up PostgreSQL locks with constants
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 09 May 2019 09:52:11 +0200
parents cc3dc759c989
children 4cd7e45b671e
comparison
equal deleted inserted replaced
135:e26690365c25 136:3266785d5627
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 21
22 #include "PostgreSQLStorageArea.h" 22 #include "PostgreSQLStorageArea.h"
23 #include "PostgreSQLDefinitions.h"
23 24
24 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h" 25 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h"
25 26
26 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h> 27 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h>
27 #include <Core/Logging.h> 28 #include <Core/Logging.h>
35 36
36 db->Open(); 37 db->Open();
37 38
38 if (parameters_.HasLock()) 39 if (parameters_.HasLock())
39 { 40 {
40 db->AdvisoryLock(43 /* some arbitrary constant */); 41 db->AdvisoryLock(POSTGRESQL_LOCK_STORAGE);
41 }
42
43 /**
44 * Try and acquire a transient advisory lock to protect the setup
45 * of the database, because concurrent statements like "CREATE
46 * TABLE" are not protected by transactions.
47 * https://groups.google.com/d/msg/orthanc-users/yV3LSTh_TjI/h3PRApJFBAAJ
48 **/
49 PostgreSQLDatabase::TransientAdvisoryLock lock(*db, 44 /* some arbitrary constant */);
50
51 if (clearAll_)
52 {
53 db->ClearAll();
54 } 42 }
55 43
56 { 44 {
57 PostgreSQLTransaction t(*db); 45 PostgreSQLDatabase::TransientAdvisoryLock lock(*db, POSTGRESQL_LOCK_DATABASE_SETUP);
58 46
59 if (!db->DoesTableExist("StorageArea")) 47 if (clearAll_)
60 { 48 {
61 db->Execute("CREATE TABLE IF NOT EXISTS StorageArea(" 49 db->ClearAll();
62 "uuid VARCHAR NOT NULL PRIMARY KEY,"
63 "content OID NOT NULL,"
64 "type INTEGER NOT NULL)");
65
66 // Automatically remove the large objects associated with the table
67 db->Execute("CREATE OR REPLACE RULE StorageAreaDelete AS ON DELETE "
68 "TO StorageArea DO SELECT lo_unlink(old.content);");
69 } 50 }
70 51
71 t.Commit(); 52 {
53 PostgreSQLTransaction t(*db);
54
55 if (!db->DoesTableExist("StorageArea"))
56 {
57 db->Execute("CREATE TABLE IF NOT EXISTS StorageArea("
58 "uuid VARCHAR NOT NULL PRIMARY KEY,"
59 "content OID NOT NULL,"
60 "type INTEGER NOT NULL)");
61
62 // Automatically remove the large objects associated with the table
63 db->Execute("CREATE OR REPLACE RULE StorageAreaDelete AS ON DELETE "
64 "TO StorageArea DO SELECT lo_unlink(old.content);");
65 }
66
67 t.Commit();
68 }
72 } 69 }
73 70
74 return db.release(); 71 return db.release();
75 } 72 }
76 73