Fix potential unclosed connection if commit fails