Some threading fixes for database connections:

If a connection is opened on a 'non-main' thread,
we can't close it from the SQLiteWrapper.