From 661d2b43f2b9fc681b1406cc6153dead531929f2 Mon Sep 17 00:00:00 2001
From: Marco Biasini <marco.biasini@unibas.ch>
Date: Wed, 6 Oct 2010 14:45:36 +0200
Subject: [PATCH] open compound lib in read-only mode, unless explicitly stated

avoids dead-locking when several processes try to open the
compound lib at the same time.
---
 modules/conop/pymod/export_compound.cc | 2 +-
 modules/conop/src/compound_lib.cc      | 5 +++--
 modules/conop/src/compound_lib.hh      | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/modules/conop/pymod/export_compound.cc b/modules/conop/pymod/export_compound.cc
index b0c6e11d5..379f20598 100644
--- a/modules/conop/pymod/export_compound.cc
+++ b/modules/conop/pymod/export_compound.cc
@@ -51,7 +51,7 @@ void export_Compound() {
   register_ptr_to_python<CompoundPtr>();  
   
   class_<CompoundLib>("CompoundLib", no_init)
-    .def("Load", &CompoundLib::Load).staticmethod("Load")
+    .def("Load", &CompoundLib::Load, arg("readonly")=true).staticmethod("Load")
     .def("FindCompound", &CompoundLib::FindCompound)
     .def("ClearCache", &CompoundLib::ClearCache)
   ;
diff --git a/modules/conop/src/compound_lib.cc b/modules/conop/src/compound_lib.cc
index 10e46b16b..b1878a973 100644
--- a/modules/conop/src/compound_lib.cc
+++ b/modules/conop/src/compound_lib.cc
@@ -237,10 +237,11 @@ CompoundLibPtr CompoundLib::Create(const String& database)
 }
 
 
-CompoundLibPtr CompoundLib::Load(const String& database) 
+CompoundLibPtr CompoundLib::Load(const String& database, bool readonly) 
 {
+  int flags=readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
   CompoundLibPtr lib(new CompoundLib);
-  int retval=sqlite3_open(database.c_str(), &lib->conn_);
+  int retval=sqlite3_open_v2(database.c_str(), &lib->conn_, flags, NULL);
   if (SQLITE_OK==retval) {
     return lib;
   }
diff --git a/modules/conop/src/compound_lib.hh b/modules/conop/src/compound_lib.hh
index 8948e3479..3d642d512 100644
--- a/modules/conop/src/compound_lib.hh
+++ b/modules/conop/src/compound_lib.hh
@@ -37,7 +37,7 @@ typedef std::map<String, CompoundPtr> CompoundMap;
 
 class DLLEXPORT_OST_CONOP CompoundLib {
 public:
-  static CompoundLibPtr Load(const String& database);
+  static CompoundLibPtr Load(const String& database, bool readonly=true);
   static CompoundLibPtr Create(const String& database);
   ~CompoundLib();
   
-- 
GitLab