fix race condition with database access