adding a check on is_open before closing connection