Thread-safety improvements for DB writing