close connection before opening again on error