use thread locking to serialize all db access

also reenable check_same_thread so we get exceptions instead of crashes,
although that shouldn't be possible anymore with this locking in place

Signed-off-by: Thomas Stilwell <stilwellt@openlabs.co>
Merge pull request #937 from benemorius/db-locking

use thread locking to serialize all db access