Before hibernate session is closed, it is checked if it is open.

Normally, this shouldn't be necessary but prevents yet another exception in case of previous error.