connect to db only when its necessery.