Fix multithreading problems. Added multithreaded test for SQLite database.