resource handling db

Improved the resource handling of db connections.