DB Use counting and reference locking

Fixes upgrade/reader crashing issue.