Fixes bug 929197 - Make sure connection closing can happen before doing it.

@peterbe r? 

Notably, some files do this:

```python
connection = self.database.connection()
try:
    # things
finally:
    connection.close() 
```

Is it better than this:

```python
try:
    connection = self.database.connection()
    # things
finally:
    if connection:
        connection.close()
```

?