Exception handling and db connection closing

Fixed a bug where DB connections which are implicitly opened are not
implicitly closed. This lead to the connection pool becoming exhausted
and leading to connection exceptions. Also added other exception
handling for non existent resources and a catch-all exception handler
for all other exceptions.