Added support for onFailure hooks

Useful to cleanup database connections after a fatal error.