Add onFailure hooks

Useful to cleanup database connections after a fatal error.