add retry / error handling
handle database.close()
Merge pull request #133 from codemix/connection-lifecycle

Connection lifecycle fixes