Fix race condition in data base