Do not rely on locking database files if you're relying on an external library to manipulate them.
It used to be very common advice to lock database files - it was even in the Cookbook as a recipe. The ultimate source of this advice seems to have been Berkeley DB documentation circa version 1.65.
However since then at least some dbm applications (Berkely DB among them) in multi-user situations will sometimes close and then re-open the database behind the scenes. Because of how flock works, any close to the file, even if you have another handle to that file open (such as the one holding a lock), will lose your flock. Then you're in the worst of all possible worlds. You don't actually have a lock, but you think that you do.