Stack Exchange
Unable to connect Mongo Db from Spring Application
I am trying to connect mongo db from spring application and i m getting the following error
Unable to connect to any servers; nested exception is com.mongodb.MongoServerSelectionException: Unable to connect to any servers
DAO Code:  
 productList = mongoTemplate.findAll(PSAProduct.class, GSM_STATS_PSA3);

Mongo DB Template:
public class MongoDBTemplate 
{
    static String MONGO_SERVER_IP;
    private MongoTemplate mongoTemplate;
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ProductController.class);



    public static @Bean MongoTemplate getTemplate(String databaseName) 
    { 
        MongoDBTemplate mongoDBTemplate=new MongoDBTemplate();
        try
        {
            mongoDBTemplate.mongoTemplate = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient("172.16.19.261:27017"), databaseName));
        }catch(Exception ee)
        {
            ee.printStackTrace();
        }
        return mongoDBTemplate.mongoTemplate; 
    } 
}

Stacktrace :
31804 [http-8080-1] ERROR com.teledna.pms.dao.psa.PSADao - Exception Occured while fetching entity count from DAO  Unable to connect to any servers; nested exception is com.mongodb.MongoServerSelectionException: Unable to connect to any servers
org.springframework.dao.InvalidDataAccessResourceUsageException: Unable to connect to any servers; nested exception is com.mongodb.MongoServerSelectionException: Unable to connect to any servers
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:64)
    at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1665)
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1548)
    at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1062)
    at com.teledna.pms.dao.psa.PSADao.getPSA2Product(PSADao.java:44)
    at com.teledna.pms.service.psa.PSAService.getPSA2Product(PSAService.java:39)
    at com.teledna.pms.controller.PSAController.getPSA2Product(PSAController.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:623)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:713)
Caused by: com.mongodb.MongoServerSelectionException: Unable to connect to any servers
    at com.mongodb.BaseCluster.getDescription(BaseCluster.java:112)
    at com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:401)
    at com.mongodb.DBTCPConnector.getType(DBTCPConnector.java:574)
    at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:375)
    at com.mongodb.Mongo.isMongosConnection(Mongo.java:645)
    at com.mongodb.DBCursor._check(DBCursor.java:454)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1534)
    ... 33 more


can you connect to your mongod server from mongo command ?

Why ae you first creating a MongoTemplate without any settings and afterwards replace it. Why is your method static it shouldn't be.

Ah now i get the contraption it is because it is static... Remove the static and just return new MongoTemplate(....) from the method. Also you probably want the beans needed to be spring beans as well to allow them to be properly started and destroyed.

@Harsh Patel Yah..I was able to connect ...but through the application I m not able to connect

@M. Deinum  I tried with non static.. still same error i am getting...

try to create 
@Bean
public MongoDbFactory mongoDbFactory() throws MongoException, UnknownHostException {
    MongoDbFactory factory = new SimpleMongoDbFactory(new MongoURI(env.getProperty("mongo_uri.dev")));
    return factory;
}

and the mongo mongo_uri is in the format:
mongo_uri.heroku=mongodb://user:pass@ds037097.mongolab.com:37097/heroku_app13338532
create your MongoTemplate in that way:
mongoDBTemplate.mongoTemplate = new MongoTemplate(mongoDbFactory());

