Improve performance of referencing resources.
Fix crashing when database is locked.