improve logic for connect db