If you're accessing the database driver from multiple threads, then the problem is likely in the driver, and there are two solutions:
Protect all accesses to the driver by a mutex. Ideally, create a wrapper class for database access that enforces this.
Create multiple instances of the driver, one per each thread.
A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
This means that you cannot use cloneDatabase
in one thread, and use the cloned connection in another thread. You must create and then open a fresh, independent connection, by code running in the thread where you wish to use that connection.
You must be also very careful that whatever implicitly shared classes you pass to the threads are in fact separate instances. It is a bug if they are references, const references, or pointers. See this answer for details.