Added shutdown hooks.

Clean shutdown of database connection.