Global Database Access
Added by Louis Hoefler almost 9 years ago
Hello Everyone,
I have a external application which is started as thread within the main call of my WT Application.
The external application does also need DBO access, are there examples how to create a "Global" DBO object which can be used throught different threads?
Thank you, Louis
Replies (2)
RE: Global Database Access - Added by Koen Deforche almost 9 years ago
Hey, not sure what you hint at: Dbo is not thread-safe. So a 'global' Dbo object should either be protected by a lock, or could be a thread local "global" object?
RE: Global Database Access - Added by Saif Rehman over 8 years ago
If you mean sharing Dbos between threads, I have some experience achieving that. Unfortunately Wt::Dbo::ptr is not thread safe, so to make global Dbos, you'd have to use some other smart pointer. Additionally, you won't be able to use Dbos which have Wt::Dbo::ptr, Wt::Dbo::weak_ptr and Wt::Dbo::collection members in them as they are dependent upon Wt::Dbo::Session which, also, is not thread safe.
I abstracted Dbos as following
Base -> containing all members except for Wt::Dbo::ptr, Wt::Dbo::weak_ptr and Wt::Dbo::collection
Dbo : public Base -> containing the Wt::Dbo::ptr, Wt::Dbo::weak_ptr and Wt::Dbo::collection
Ddo : public Base -> containing members to hold the Wt::Dbo::ptr::id() values and a constructor that takes Wt::Dbo::ptr as an argument
I made functions that returned std::shared_ptr<const Ddo> while locking a mutex
You can take a look at:
https://github.com/SaiFi0102/WebWidgets/blob/master/src/WebWidgets/DboDatabase/ModuleDatabase.h
https://github.com/SaiFi0102/WebWidgets/blob/master/src/WebWidgets/Dbo/Module.h