Added locks around database accesses