Connection URI for k8s statefulSet headless service or list of pods?


I’m little bit unsure what would the correct connection URI for my applications for the Mongodb statefulset. I have three replicas running in my cluster, each one in separate node.
Should I configure the pods OR the headless service (load balancer for the pods)?

The documentation is directing to use pods, like this (Running MongoDB on Kubernetes with StatefulSets | Kubernetes):

But I have got it working with the service as well:

But, I don’t know what is the correct URI? The problem I’m having is that when some of the replicas goes down, for some reason, the application crashes as the database connection is lost. I think this is where the headless-service comes in picture, but no, the documentation says to configure the pods. And if I scale the replicas I need to reconfigure the URI. This does not sound so dynamic.

I’m also facing some issues with the headless service, as if it is in different namespace I cannot get the connection work with namespace defined, like:

Have I missed something?
Thank you in any advance!