Fixed DB module stability issues

Fixed stability issues in sqlite when querying from multiple threads.
Combined all DB access into single thread and created exclusive
connections for queries and inserts.