Add synchronization to the connection switching in on_db