Added mutex for ensure_connection to avoid race conditions.