Add synchronization logic to Connection.scala and ConnectionManager.scala to avoid race condition