无法从Spring Application连接Mongo Db

时间:2015-06-08 11:17:58

标签: spring mongodb spring-mvc

我正在尝试从spring应用程序连接mongo db,我收到以下错误

无法连接到任何服务器;嵌套异常是com.mongodb.MongoServerSelectionException:无法连接到任何服务器

DAO代码:

 productList = mongoTemplate.findAll(PSAProduct.class, GSM_STATS_PSA3);

Mongo DB模板:

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

1 个答案:

答案 0 :(得分:0)

尝试创建

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

并且mongo mongo_uri的格式为:

mongo_uri.heroku=mongodb://user:pass@ds037097.mongolab.com:37097/heroku_app13338532

以这种方式创建MongoTemplate:

mongoDBTemplate.mongoTemplate = new MongoTemplate(mongoDbFactory());