Added lock to prevent the database from being opened multiple times.