Announcement Announcement Module
Collapse
No announcement yet.
CloudFoundry: MongoDB : Connection Refused vs NoClassDefFoundError Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CloudFoundry: MongoDB : Connection Refused vs NoClassDefFoundError

    Case closed. The issue has been resolved by using the latest Cloud Foundry dependency:

    Code:
        <org.cloudfoundry-version>0.8.0</org.cloudfoundry-version>
    
        <dependency>
            <groupId>org.cloudfoundry</groupId>
            <artifactId>cloudfoundry-runtime</artifactId>
            <version>${org.cloudfoundry-version}</version>
        </dependency>


    My problem is basic. I have an application that uses MongoDB. It works locally, but when deploying to Cloud Foundry, I get two errors (depending on how I configure my Mongo).

    First, my Maven snippet:

    Code:
        <spring.data.mongo.version>1.0.0.BUILD-SNAPSHOT</spring.data.mongo.version>
        <org.cloudfoundry-version>0.7.1</org.cloudfoundry-version>
    
            <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>${spring.data.mongo.version}</version>
        </dependency>
    
            <dependency>
            <groupId>org.cloudfoundry</groupId>
            <artifactId>cloudfoundry-runtime</artifactId>
            <version>${org.cloudfoundry-version}</version>
        </dependency>
    Here's my initial MongoDB config that works on my localhost machine:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p" 
            xmlns:mongo="http://www.springframework.org/schema/data/mongo"
        xmlns:cloud="http://schema.cloudfoundry.org/spring"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
               http://www.springframework.org/schema/data/mongo
                http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
                http://schema.cloudfoundry.org/spring 
                http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.7.xsd">
        
          <mongo:repositories base-package="org.sample.repositories" />
      
        <mongo:mongo host="localhost" port="27017"/>
        
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
                  <constructor-arg ref="mongo"/>
                <constructor-arg name="databaseName" value="mydb"/>
          </bean>
    </beans>
    When I deploy this directly to CloudFoundry, I get the following exception:

    Code:
    [ERROR] [main 02:56:48] (ContextLoader.java:initWebApplicationContext:238) Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initMongoService' defined in ServletContext resource [/WEB-INF/spring-mongo.xml]: Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: can't call something; nested exception is com.mongodb.MongoException$Network: can't call something
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:294)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:215)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
        at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
        at org.apache.catalina.core.StandardService.start(StandardService.java:525)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: org.springframework.dao.DataAccessResourceFailureException: can't call something; nested exception is com.mongodb.MongoException$Network: can't call something
        at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:56)
        at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1153)
        at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:292)
        at org.springframework.data.mongodb.core.MongoTemplate.dropCollection(MongoTemplate.java:350)
        at org.krams.tutorial.service.InitMongoService.init(InitMongoService.java:31)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1546)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1487)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
        ... 36 more
    Caused by: com.mongodb.MongoException$Network: can't call something
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:211)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:303)
        at com.mongodb.DB.command(DB.java:159)
        at com.mongodb.DB.command(DB.java:144)
        at com.mongodb.DBCollection.drop(DBCollection.java:682)
        at com.mongodb.DBApiLayer$MyCollection.drop(DBApiLayer.java:212)
        at org.springframework.data.mongodb.core.MongoTemplate$6.doInCollection(MongoTemplate.java:352)
        at org.springframework.data.mongodb.core.MongoTemplate$6.doInCollection(MongoTemplate.java:350)
        at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:290)
        ... 45 more
    Caused by: java.io.IOException: couldn't connect to [7a844500-b368-429d-bc29-bfc3484e10d9/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused
        at com.mongodb.DBPort._open(DBPort.java:224)
        at com.mongodb.DBPort.go(DBPort.java:101)
        at com.mongodb.DBPort.go(DBPort.java:82)
        at com.mongodb.DBPort.call(DBPort.java:72)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:202)
        ... 53 more
    I suspect maybe because I didn't used the cloud namespace.
    Last edited by skram; Sep 16th, 2011, 10:19 AM.

  • #2
    So I modified and redeployed my app.

    Here's my new MongoDB configuration:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:p="http://www.springframework.org/schema/p" 
            xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    	xmlns:cloud="http://schema.cloudfoundry.org/spring"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	   	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       		http://www.springframework.org/schema/data/mongo
        		http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
        		http://schema.cloudfoundry.org/spring 
        		http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.7.xsd">
    	
      	<mongo:repositories base-package="org.sample.repositories" />
      
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    			<constructor-arg ref="mongoDbFactory"/>
      	</bean>
    	
    	<cloud:mongo-db-factory id="mongoDbFactory"/>
    </beans>
    However, I get the following exception as soon as I deploy it to CloudFoundry:

    Code:
    [ERROR] [main 03:12:37] (ContextLoader.java:initWebApplicationContext:238) Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDbFactory': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.cloudfoundry.runtime.service.document.CloudMongoDbFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/document/mongodb/MongoDbFactory
    	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:735)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:666)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:511)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:317)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:394)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:388)
    	at org.cloudfoundry.reconfiguration.CloudAutoStagingBeanFactoryPostProcessor.postProcessBeanFactory(CloudAutoStagingBeanFactoryPostProcessor.java:71)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:668)
    	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:651)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:437)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:294)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:215)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
    	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
    	at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
    	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
    	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.cloudfoundry.runtime.service.document.CloudMongoDbFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/document/mongodb/MongoDbFactory
    	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108)
    	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    	... 39 more
    Caused by: java.lang.NoClassDefFoundError: org/springframework/data/document/mongodb/MongoDbFactory
    	at org.cloudfoundry.runtime.service.document.CloudMongoDbFactoryBean.<init>(CloudMongoDbFactoryBean.java:16)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    	... 41 more
    Caused by: java.lang.ClassNotFoundException: org.springframework.data.document.mongodb.MongoDbFactory
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    	... 47 more
    Stopping Tomcat because the context stopped.
    I suspect it's because I'm using the latest Spring Data MongoDB snapshot (along with the latest spring-data-commons-core dependency). See Forum: http://forum.springsource.org/showth...tegration-Hell and JIRA: https://jira.springsource.org/browse/DATAJPA-90 for a brief background of these updates.

    So now I'm stucked in two worlds. It's a connection configuration issue and a class definition exception.

    Comment


    • #3
      So in your first post, it looks like problem is that the connection information is incorrect. In Cloud Foundry, your database host, port, username, password and database name are passed in as an environment variable. In your mongo config, you have it hardcoded to look for a mongodb on localhost:27017. That is why you were first getting the failure in initMongo.

      I believe that the cloud:mongo-db-factory can automatically parse the environment variable for you (i'm more of a mongodb guy, than a spring source guy, so forgive my ignorance here). The examples from springsource differ from your code. Check out https://github.com/SpringSource/clou...ot-context.xml for their sample config.

      Could that be it?

      Comment


      • #4
        It seems CloudFoundry hasn't updated to Spring Data MongoDB 1.0.0.M4 yet. I've pinged the CF team and will report back as soon as the issue is fixed.

        Comment


        • #5
          Thanks for the replies.

          I'm not really sure at first where to post this issue: here (Spring forums) or there (CloudFoundry forums). I'm glad action is being done.

          Comment


          • #6
            The update to SD MongoDB 1.0 M4 has been done with CF runtime version 0.8.0. So you shouldn't see the namespace related error anymore.

            Comment


            • #7
              Thanks Oliver.

              Everything is working now. Sorry if it took a while to reply back. I was busy writing and testing my app for a new guide I'm writing at: Spring MVC: Integrating MySQL, MongoDB, RabbitMQ, and AJAX

              You can check the whole guide at http://krams915.blogspot.com/2011/09...godb_8914.html which is actually intended for people who want to learn more about these technologies

              Also, I've deployed the app to Cloud Foundry which you can find at http://spring-mysql-mongo-rabbit.cloudfoundry.com/ You might need to refresh the app due to a bug with JSTL c:url (See http://stackoverflow.com/questions/1...-jstl-curl-tag)

              There's also a dedicated GitHub repo for this project at https://github.com/krams915/spring-m...it-integration

              Comment

              Working...
              X