ensure protecting accesses to the dabatase in a critical section

and set SQLITE_THREADSAFE mode to 1