[SPARK-3106] Fix the race condition issue about Connection and ConnectionManager

You can still merge the conditions. Your code is racy with the connection being closed regardless of whether you have one or two ifs here.

You can get rid of the weird check by doing:

    if ((connection == null || !connection.isClosed) && !key.isValid) { throw ... }

But this kinda feeds back into the "why avoid exceptions when they're handled properly" discussion.