Refine db connection handling