Luc4
2nd July 2011, 09:11
Hi! I need to create a library that should provide API to get content from a database. I would like to create this library thread-safe, because it is possible that the users will try to invoke the methods from within many different threads.
To do this, I created a singleton class that accesses the database. That class have methods get*, set* etc... to respectively execute select, update etc... queries. To guarantee thread-safety, in each method I create the connection to the database, I set the connection options, I open the database etc... and, at the end of the method I close the connection to the database. This because the connection can only be used in the same thread that created it.
My question is: is this the correct way or the best way of doing it?
Thanks!
To do this, I created a singleton class that accesses the database. That class have methods get*, set* etc... to respectively execute select, update etc... queries. To guarantee thread-safety, in each method I create the connection to the database, I set the connection options, I open the database etc... and, at the end of the method I close the connection to the database. This because the connection can only be used in the same thread that created it.
My question is: is this the correct way or the best way of doing it?
Thanks!