move db connection and close to very start and end