Added lock to make sure cocurrent accesses do not make the DB
inconsistent.