db: Use mutex to make database thread-safe