DB Use counting and reference locking

Fixes upgrade/reader crashing issue.
Merge pull request #726 from nevercast/bug-reader

DB Use counting and reference locking