Fix bug for connection is null closing failure

When a connection fails to open, check for null on the returned
connection before attempting to close it.

Adding a test for connection failure handling