PDA

View Full Version : CloudFoundry: MongoDB : Connection Refused vs NoClassDefFoundError



skram
Aug 29th, 2011, 10:26 PM
Case closed. The issue has been resolved by using the latest Cloud Foundry dependency:



<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:



<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:



<?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.MongoTemplat e">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="mydb"/>
</bean>
</beans>


When I deploy this directly to CloudFoundry, I get the following exception:


[ERROR] [main 02:56:48] (ContextLoader.java:initWebApplicationContext:238) Context initialization failed
org.springframework.beans.factory.BeanCreationExce ption: 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.DataAccessResourceFailureE xception: can't call something; nested exception is com.mongodb.MongoException$Network: can't call something
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 95)
at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:292 )
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:900)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:455)
at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:215)
at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 47)
at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:4205)
at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4704)
at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDirec tory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirec tories(HostConfig.java:1002)
at org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostC onfig.java:1315)
at org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1061)
at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:840)
at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1053)
at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:463)
at org.apache.catalina.core.StandardService.start(Sta ndardService.java:525)
at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalin a.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:414)
Caused by: org.springframework.dao.DataAccessResourceFailureE xception: can't call something; nested exception is com.mongodb.MongoException$Network: can't call something
at org.springframework.data.mongodb.core.MongoExcepti onTranslator.translateExceptionIfPossible(MongoExc eptionTranslator.java:56)
at org.springframework.data.mongodb.core.MongoTemplat e.potentiallyConvertRuntimeException(MongoTemplate .java:1153)
at org.springframework.data.mongodb.core.MongoTemplat e.execute(MongoTemplate.java:292)
at org.springframework.data.mongodb.core.MongoTemplat e.dropCollection(MongoTemplate.java:350)
at org.krams.tutorial.service.InitMongoService.init(I nitMongoService.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeCustomInitMethod( AbstractAutowireCapableBeanFactory.java:1546)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1487)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1417)
... 36 more
Caused by: com.mongodb.MongoException$Network: can't call something
at com.mongodb.DBTCPConnector.call(DBTCPConnector.jav a:211)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLa yer.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:68 2)
at com.mongodb.DBApiLayer$MyCollection.drop(DBApiLaye r.java:212)
at org.springframework.data.mongodb.core.MongoTemplat e$6.doInCollection(MongoTemplate.java:352)
at org.springframework.data.mongodb.core.MongoTemplat e$6.doInCollection(MongoTemplate.java:350)
at org.springframework.data.mongodb.core.MongoTemplat e.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.jav a:202)
... 53 more

I suspect maybe because I didn't used the cloud namespace.

skram
Aug 29th, 2011, 10:26 PM
So I modified and redeployed my app.

Here's my new MongoDB configuration:



<?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.MongoTemplat e">
<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:


[ERROR] [main 03:12:37] (ContextLoader.java:initWebApplicationContext:238) Context initialization failed
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'mongoDbFactory': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.cloudfoundry.runtime.service.document.CloudMon goDbFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/document/mongodb/MongoDbFactory
at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:288)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:1003)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:907)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.getSingletonFactoryBean ForTypeCheck(AbstractAutowireCapableBeanFactory.ja va:735)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.getTypeForFactoryBean(A bstractAutowireCapableBeanFactory.java:666)
at org.springframework.beans.factory.support.Abstract BeanFactory.isTypeMatch(AbstractBeanFactory.java:5 11)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanNamesForType(DefaultList ableBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeansOfType(DefaultListableB eanFactory.java:394)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeansOfType(DefaultListableB eanFactory.java:388)
at org.cloudfoundry.reconfiguration.CloudAutoStagingB eanFactoryPostProcessor.postProcessBeanFactory(Clo udAutoStagingBeanFactoryPostProcessor.java:71)
at org.springframework.context.support.AbstractApplic ationContext.invokeBeanFactoryPostProcessors(Abstr actApplicationContext.java:668)
at org.springframework.context.support.AbstractApplic ationContext.invokeBeanFactoryPostProcessors(Abstr actApplicationContext.java:651)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:437)
at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:215)
at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 47)
at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:4205)
at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4704)
at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDirec tory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirec tories(HostConfig.java:1002)
at org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostC onfig.java:1315)
at org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1061)
at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:840)
at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1053)
at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:463)
at org.apache.catalina.core.StandardService.start(Sta ndardService.java:525)
at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalin a.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:414)
Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.cloudfoundry.runtime.service.document.CloudMon goDbFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/document/mongodb/MongoDbFactory
at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:108)
at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:280)
... 39 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/document/mongodb/MongoDbFactory
at org.cloudfoundry.runtime.service.document.CloudMon goDbFactoryBean.<init>(CloudMongoDbFactoryBean.java:16)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:147)
... 41 more
Caused by: java.lang.ClassNotFoundException: org.springframework.data.document.mongodb.MongoDbF actory
at org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadC lass(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/showthread.php?113598-Spring-Data-JPA-and-Spring-Data-MongoDB-Integration-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.

jrosoff
Sep 6th, 2011, 12:32 AM
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/cloudfoundry-samples/blob/master/hello-spring-mongodb/src/main/resources/root-context.xml for their sample config.

Could that be it?

Oliver Gierke
Sep 6th, 2011, 03:14 AM
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.

skram
Sep 6th, 2011, 08:24 PM
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.

Oliver Gierke
Sep 12th, 2011, 04:55 AM
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.

skram
Sep 16th, 2011, 10:16 AM
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/spring-mvc-integrating-mysql-mongodb_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/1045668/jsessionid-is-occured-in-all-urls-which-are-generated-by-jstl-curl-tag)

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