fix database deadlock bugs