connect to db at the right time