C3P0 connection pooling with hibenrate doesnt work with cloud foundry

2,186 views
Skip to first unread message

roseville

unread,
Oct 28, 2013, 4:01:28 PM10/28/13
to vcap...@cloudfoundry.org
Not sure If I can discuss issues related to usage of cloud foundry. If not then please guide me to the right forum.

I am trying to push a simple hibernate+MYSQL app developed using STS. Hibernate uses c3P0 for connection pooling. When I push the app and start accessing it I see exceptions related to c3P0.I tested it locally and it works well.

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A
ResourcePool could not acquire a resource from its primary factory or source. at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319) at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557) at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525
Enter code here...

This is the cloud profile I am using. I referred cloud foundry docs but still not able to make it work.

<beans profile="cloud"> <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://${cloud.services.mysql.connection.host}:${cloud.services.mysql.connection.port}/${cloud.services.mysql.connection.name}" /> <property name="user" value="${cloud.services.mysql.connection.username}" /> <property name="password" value="${cloud.services.mysql.connection.password}" /> <property name="minPoolSize" value="5" /> <property name="maxPoolSize" value="20" /> <property name="maxIdleTime" value="20" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="devnexus-db" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop> </props> </property> <property name="packagesToScan" value="com.iml.policytype.model" /> </bean> <tx:annotation-driven /> <context:component-scan base-package="com.iml.policytype.bo" /> <context:component-scan base-package="com.iml.policytype.dao" /> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" /> </beans>

Can anyone help?

Daniel Mikusa

unread,
Oct 28, 2013, 4:33:10 PM10/28/13
to vcap...@cloudfoundry.org
On Oct 28, 2013, at 4:01 PM, roseville <mmo...@gmail.com> wrote:

> Not sure If I can discuss issues related to usage of cloud foundry. If not then please guide me to the right forum.

Are you targeting run.pivotal.io? If so, you should post on the forums https://support.cloudfoundry.com or email sup...@cloudfoundry.com.

If you're running your own CloudFoundry instance then this is the place.

> I am trying to push a simple hibernate+MYSQL app developed using STS. Hibernate uses c3P0 for connection pooling. When I push the app and start accessing it I see exceptions related to c3P0.I tested it locally and it works well.
>
> Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A
> ResourcePool could not acquire a resource from its primary factory or source. at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319) at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557) at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525
> Enter code here...
>
> This is the cloud profile I am using. I referred cloud foundry docs but still not able to make it work.
>
> <beans profile="cloud">

This looks good, the "cloud" profile should be activated when run on CF.

>
> <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
>
> <property name="driverClass" value="com.mysql.jdbc.Driver" />
>
> <property name="jdbcUrl" value="jdbc:mysql://${cloud.services.mysql.connection.host}:${cloud.services.mysql.connection.port}/${cloud.services.mysql.connection.name}" />
> <property name="user" value="${cloud.services.mysql.connection.username}" />
> <property name="password" value="${cloud.services.mysql.connection.password}" />

Have you created a service and bound it to your application? What is the service's name? Based on the property place holder that you're using your service will need to be named "mysql". Is that correct?

Also, do you see this information in the environment variable VCAP_SERVICES?

Please note if you post your VCAP_SERVICES environment variable here, not really recommended, that it contains the credentials for your service. Don't forget to remove that info before posting.

Dan

> <property name="minPoolSize" value="5" />
>
>
> <property name="maxPoolSize" value="20" />
>
>
> <property name="maxIdleTime" value="20" />
>
>
> </bean>
>
>
> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
>
>
> <property name="dataSource" ref="devnexus-db" />
>
>
> <property name="hibernateProperties">
>
>
> <props>
>
>
> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
>
>
> <prop key="hibernate.show_sql">true</prop>
>
>
> <prop key="hibernate.hbm2ddl.auto">create</prop>
>
>
> <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
>
>
> </props>
>
>
> </property>
>
>
> <property name="packagesToScan" value="com.iml.policytype.model" />
>
>
> </bean>
>
>
> <tx:annotation-driven />
>
>
> <context:component-scan base-package="com.iml.policytype.bo" />
>
>
> <context:component-scan base-package="com.iml.policytype.dao" />
>
>
> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
>
>
> p:sessionFactory-ref="sessionFactory" />
> </beans>
>
> Can anyone help?
>
> To unsubscribe from this group and stop receiving emails from it, send an email to vcap-dev+u...@cloudfoundry.org.

David Laing

unread,
Oct 28, 2013, 4:52:03 PM10/28/13
to vcap-dev

I have a vague memory of the MySQL connection being limited to a really low number of concurrent connections - something like 4 - in CF v1 / on free plans.

Could this be a similar case?  Can you set c3po to a really low number of concurrent connections - like 2 - to test?

To unsubscribe from this group and stop receiving emails from it, send an email to vcap-dev+u...@cloudfoundry.org.

roseville

unread,
Oct 28, 2013, 4:55:09 PM10/28/13
to vcap...@cloudfoundry.org
Great thanks for the quick reply.

Since you asked more info, I will continue with the discussion here. But you can stop me if this is taking time:)

-I am running it on run.pivotal.io
-MYSQL service name is "mysql"
-Yes I have created the service and it my app is using this service. Please see the cf services output

E:\>cf services
name    service   provider   version   plan    bound apps
mysql   cleardb   cleardb    n/a       spark   cloudspring

this is the env.log file.
TMPDIR=/home/vcap/tmp
VCAP_APP_PORT=63613
USER=vcap
VCAP_APPLICATION={"instance_id":"e80c0ee59cb54f0f69e8da3272c251af","instance_index":0,"host":"0.0.0.0","port":63613,"started_at":"2013-10-28 20:06:04 +0000","started_at_timestamp":1382990764,"start":"2013-10-28 20:06:04 +0000","state_timestamp":1382990764,"limits":{"mem":512,"disk":1024,"fds":16384},"application_version":"ff07ee16-5e50-4ed5-a989-eb0d628c4a22","application_name":"cloudspring","application_uris":["cloudspring.cfapps.io"],"version":"ff07ee16-5e50-4ed5-a989-eb0d628c4a22","name":"cloudspring","uris":["cloudspring.cfapps.io"],"users":null}
PATH=/bin:/usr/bin
PWD=/home/vcap
VCAP_SERVICES={"cleardb-n/a":[{"name":"mysql","label":"cleardb-n/a","tags":["mysql","relational"],"plan":"spark","credentials":{"jdbcUrl":"jdbc:mysql://b6fc76b462d3bc:cbb5...@us-cdbr-east-04.cleardb.com:3306/ad_60082a44adc9bf9","uri":"mysql://b6fc76b462d3bc:cbb5...@us-cdbr-east-04.cleardb.com:3306/ad_60082a44adc9bf9?reconnect=true","name":"ad_60082a44adc9bf9","hostname":"us-cdbr-east-04.cleardb.com","port":"3306","username":"yyyyyyyyy","password":"xxxxxxxx"}}]}
SHLVL=1
HOME=/home/vcap/app
PORT=63613
VCAP_APP_HOST=0.0.0.0
DATABASE_URL=mysql2://b6fc76b462d3bc:cbb5...@us-cdbr-east-04.cleardb.com:3306/ad_60082a44adc9bf9?reconnect=true
MEMORY_LIMIT=512m
_=/usr/bin/env

Daniel Mikusa

unread,
Oct 28, 2013, 5:11:44 PM10/28/13
to vcap...@cloudfoundry.org
On Oct 28, 2013, at 4:55 PM, roseville <mmo...@gmail.com> wrote:

> Great thanks for the quick reply.
>
> Since you asked more info, I will continue with the discussion here. But you can stop me if this is taking time:)

Not a problem, just mentioned it for future reference.

> -I am running it on run.pivotal.io
> -MYSQL service name is "mysql"
> -Yes I have created the service and it my app is using this service. Please see the cf services output
>
> E:\>cf services
> name service provider version plan bound apps
> mysql cleardb cleardb n/a spark cloudspring

Good.

> VCAP_SERVICES={"cleardb-n/a":[{"name":"mysql","label":"cleardb-n/a","tags":["mysql","relational"],"plan":"spark","credentials":{"jdbcUrl":"jdbc:mysql://b6fc76b462d3bc:cbb5...@us-cdbr-east-04.cleardb.com:3306/ad_60082a44adc9bf9","uri":"mysql://b6fc76b462d3bc:cbb5...@us-cdbr-east-04.cleardb.com:3306/ad_60082a44adc9bf9?reconnect=true","name":"ad_60082a44adc9bf9","hostname":"us-cdbr-east-04.cleardb.com","port":"3306","username":"yyyyyyyyy","password":"xxxxxxxx"}}]}

Couple things here…

1.) This is the most important. You've missed a couple spots where the l/p is listed (see the jdbcURL and uri). You're going to want to contact ClearDb to reset the l/p for this service. See this KB.

http://support.cloudfoundry.com/entries/24488351-What-Should-I-Do-If-My-Service-Credentials-Are-Compromised-

2.) This is probably the cause of your problem. Your using property place holders for "cloud.services.mysql.connection.host", "cloud.services.mysql.connection.port" and "cloud.services.mysql.connection.name", however I don't think these will all exist. Instead of "cloud.services.mysql.connection.host" I think you'd want "cloud.services.mysql.connection.hostname". Alternatively, you could just do this…

> <property name="jdbcUrl" value="${cloud.services.mysql.connection.jdbcUrl}" />


Which uses the pre-formatted JDBC URL provided by the service.

Hope that helps!

Dan

David Laing

unread,
Oct 28, 2013, 5:08:27 PM10/28/13
to vcap-dev

Oh, and as Dan mentioned, you just posted your mysql username and password, so you'll probably want to change those soonish.

:)

To unsubscribe from this group and stop receiving emails from it, send an email to vcap-dev+u...@cloudfoundry.org.

David Laing

unread,
Oct 28, 2013, 5:07:01 PM10/28/13
to vcap-dev

I'm pretty sure that the free cleardb plans have a max connection limit of 10.

That said, you should post this question over at support.cloudfoundry.com since they'd be able to give you the specific numbers for run.pivotal.io.

On 28 Oct 2013 20:55, "roseville" <mmo...@gmail.com> wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to vcap-dev+u...@cloudfoundry.org.

roseville

unread,
Oct 28, 2013, 5:37:08 PM10/28/13
to vcap...@cloudfoundry.org
I did the following changes


            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="${cloud.services.mysql.connection.jdbcUrl}" />

            <property name="user" value="${cloud.services.mysql.connection.username}" />
            <property name="password" value="${cloud.services.mysql.connection.password}" />
            <property name="minPoolSize" value="1" />
            <property name="maxPoolSize" value="4" />
But No luck...:(

I am still getting the exception

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:514)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:417)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:255)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy58.getPolicyType(Unknown Source)
    at com.symantec.netbackup.iml.webservice.PolicyTypeResource.getMsg(PolicyTypeResource.java:30)
    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:606)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:680)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
    at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:550)
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:428)
    ... 47 more
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
    at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
    ... 50 more

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
    ... 54 more

Daniel Mikusa

unread,
Oct 29, 2013, 8:23:29 AM10/29/13
to vcap...@cloudfoundry.org
On Oct 28, 2013, at 5:37 PM, roseville <mmo...@gmail.com> wrote:

> I did the following changes
>
> <property name="driverClass" value="com.mysql.jdbc.Driver" />
> <property name="jdbcUrl" value="${cloud.services.mysql.connection.jdbcUrl}" />
> <property name="user" value="${cloud.services.mysql.connection.username}" />
> <property name="password" value="${cloud.services.mysql.connection.password}" />
> <property name="minPoolSize" value="1" />
> <property name="maxPoolSize" value="4" />
> But No luck…:(

Ok, at this point I would suggest a few things:

1.) Create a dummy bean in your application. Give it getters / setters for jdbcUrl, user and password. When a setter is called, log the value that is passed into it. Define the bean in your spring cloud profile and inject the "cloud.services.xxxx" property place holders.

Ex:

<bean class="com.my.DummyClass">
<property name="jdbcUrl" value="${cloud.services.mysql.connection.jdbcUrl}" />
<property name="user" value="${cloud.services.mysql.connection.username}" />
<property name="password" value="${cloud.services.mysql.connection.password}" />
</bean>

This should allow you to confirm if sane values are being given to the DataSource bean. In other words, is "cloud.services.mysql.connection.jdbcUrl" actually resolving to the JDBC URL or is it just blank.

2.) Attach the "stdout.log" and "stderr.log" content when you run "cf logs".

3.) As David has mentioned, the free service provided by ClearDb has a very small connection limit. I don't know what it is off the top of my head, but I would suggest setting your pool sizes (initial, min, max, etc…) to 1. That should allow us to rule out connection limits as a possibility.

4.) Take the connection information presented in the VCAP_SERVICES environment variable and try to connect to it with a standard MYSQL or database client. You should be able to connect to the service and submit queries. This will allow us to rule out a possible problem with the database service.

If after all that, you are no closer to a solution. Post your code somewhere (github, bitbucket, etc..) and I'll take a look.

Dan

Ramnivas Laddad

unread,
Oct 29, 2013, 1:28:22 PM10/29/13
to vcap...@cloudfoundry.org
Can you try replacing your data source bean with the following:

<cloud:data-source id="c3p0DataSource" service-name="<name of the service>"/>

(You will have to declare the cloud namespace as described here: http://docs.cloudfoundry.com/docs/using/services/spring-service-bindings.html)

-Ramnivas

roseville

unread,
Oct 30, 2013, 6:34:17 PM10/30/13
to vcap...@cloudfoundry.org
Hi Daniel,

This is what I tried today with the spring project

1. Get hold of C3P0 bean and print out the values
I            ComboPooledDataSource finder = (ComboPooledDataSource)appContext.getBean("c3p0DataSource");
           
            System.out.println("\n\n  URL = "+finder.getJdbcUrl());
            System.out.println("\n\n  name = "+finder.getUser());
            System.out.println("\n\n  password = "+finder.getPassword());

I found that in the cloud foundry cloud profile(<beans profile="cloud">) is ignored and it tries to use the default profile(<beans profile="default">). It prints out the local JDBCurl.

2. I removed default profile from the xml and found that it was not able to detect any bean..which means it didnt find and bean tag. So having cloud profile not working.

3. After that I removed profile tag from the spring xml. Now it tries to connect but I am getting the same c3p0 exception.This is expected I guess because spring auto config not replacing the values with

4. With just one profile (but not with profile tag) I hardcoded jdbc url in the spring xml from mysql service running in the cloud expecting atleast this would work but it seems this also failed. Now I am clueless.

 I am now questioning whether DB service is properly configured or not. How do I find out whether mysql service is properly configured?

Since I am new to both spring and cloud foundry not sure if I am doing the right thing. just wondering if you have any sample application that interacts with
 the database using JPA and also using xml approach of spring not the annotation one(I see that annotation approach is there on github). I didnt find this example anywhere.

Daniel Mikusa

unread,
Oct 31, 2013, 7:35:21 AM10/31/13
to vcap...@cloudfoundry.org
On Oct 30, 2013, at 6:34 PM, roseville <mmo...@gmail.com> wrote:

> Hi Daniel,
>
> This is what I tried today with the spring project
>
> 1. Get hold of C3P0 bean and print out the values
> I ComboPooledDataSource finder = (ComboPooledDataSource)appContext.getBean("c3p0DataSource");
>
> System.out.println("\n\n URL = "+finder.getJdbcUrl());
> System.out.println("\n\n name = "+finder.getUser());
> System.out.println("\n\n password = "+finder.getPassword());
>
> I found that in the cloud foundry cloud profile(<beans profile="cloud">) is ignored and it tries to use the default profile(<beans profile="default">). It prints out the local JDBCurl.
>
> 2. I removed default profile from the xml and found that it was not able to detect any bean..which means it didnt find and bean tag. So having cloud profile not working.
>
> 3. After that I removed profile tag from the spring xml. Now it tries to connect but I am getting the same c3p0 exception.This is expected I guess because spring auto config not replacing the values with
>
> 4. With just one profile (but not with profile tag) I hardcoded jdbc url in the spring xml from mysql service running in the cloud expecting atleast this would work but it seems this also failed. Now I am clueless.

It's hard for me to say what's going on here cause I can't see your code. If you want to post the code somewhere, I'd be happy to take a look. Otherwise, I'd suggest you start with one of the sample applications and go from there. That way I know what you're working with.

>
> I am now questioning whether DB service is properly configured or not. How do I find out whether mysql service is properly configured?

This is easy to test. On your local machine. Fire up a MySQL client. You can use the command line one or a fancy GUI app like SQuirreLSQL. Enter the connection information that you see in VCAP_SERVICES. You should be able to connect to the service from your local machine. If that works, then the service is OK.

>
> Since I am new to both spring

Start here and familiarize yourself with Spring.

http://spring.io/guides

> and cloud foundry not sure if I am doing the right thing. just wondering if you have any sample application that interacts with
> the database using JPA and also using xml approach of spring not the annotation one(I see that annotation approach is there on github). I didnt find this example anywhere.

There are a few samples here.

https://github.com/cloudfoundry-samples

Samples of interest, in order of complexity...

https://github.com/cloudfoundry-samples/spring-hello-env
https://github.com/cloudfoundry-samples/hello-spring
https://github.com/cloudfoundry-samples/spring-travel
https://github.com/cloudfoundry-samples/springmvc-hibernate-template
https://github.com/cloudfoundry-samples/spring-music

I don't know that there is one that will cover your exact use-case, spring-travel is probably the closest though.

Dan

Milind Moghe

unread,
Oct 31, 2013, 2:33:42 PM10/31/13
to vcap...@cloudfoundry.org
Hi Daniel,

I tried to connect to mysql using following command
mysql -D [DATABASE_NAME] -h [HOST_NAME] -P [PORT] -u [USER] -p

but its returning
ERROR 2003 (HY000): Can't connect to MySQL server on 'us-cdbr-east-04.cleardb.com' (10061)

What should I do next? How do I know whether service is configured properly?

Daniel Mikusa

unread,
Oct 31, 2013, 3:38:00 PM10/31/13
to vcap...@cloudfoundry.org
On Oct 31, 2013, at 2:33 PM, Milind Moghe <mmo...@gmail.com> wrote:

> Hi Daniel,
>
> I tried to connect to mysql using following command
> mysql -D [DATABASE_NAME] -h [HOST_NAME] -P [PORT] -u [USER] -p
>
> but its returning
> ERROR 2003 (HY000): Can't connect to MySQL server on 'us-cdbr-east-04.cleardb.com' (10061)
>
> What should I do next? How do I know whether service is configured properly?

Assuming that you've entered the correct information for host, port user and database name and that you're not behind a network proxy or firewall which might be blocking the traffic then I would say that you have a problem with the service. If this database has important data in it, you'll want to contact the service provider and troubleshoot with them.

You can reach ClearDb here.

http://www.cleardb.com/developers/help/support

You may also need to provide them with your space GUID, instructions for finding that are here.

http://docs.cloudfoundry.com/docs/dotcom/marketplace/contacting-service-providers-for-support.html

If it's just a testing database then I would suggest that you delete the service and start over with a new one.

Dan

Milind Moghe

unread,
Oct 31, 2013, 3:49:38 PM10/31/13
to vcap...@cloudfoundry.org
It is a test database and yes I can delete it. I will check by deleting the service and creating the new one.

Daniel Mikusa

unread,
Oct 31, 2013, 4:18:20 PM10/31/13
to vcap...@cloudfoundry.org
On Oct 31, 2013, at 3:49 PM, Milind Moghe <mmo...@gmail.com> wrote:

> It is a test database and yes I can delete it. I will check by deleting the service and creating the new one.

Delete the old service then do this and create a new service.

1.) Clone this repo: https://github.com/cloudfoundry-samples/hello-spring

2.) Edit "src/main/resources/root-context.xml". Uncomment "<cloud:data-source id="dataSource"/>".

3.) Run "mvn package".

4.) Run "cf push --path target/hello-spring.war".

5.) Enter the following when prompted.
Name>hello-spring
Instances>1
Memory Limit>3
Subdomain>hello-spring-mmoghe
Domain>cfapps.io
Create services for application?>y
What kind?> 2
Name?>cleardb-xxxxx
Which plan?> 1
Create another service?> n
Bind other services to application?> n
Save configuration?> y

Now wait till this finishes and you should have a running application.

Go to the URL in your browser. You should see a screen that says "The following services are bound to this application:" and it should list a ClearDb datasource. If it doesn't, make sure you did #2 above and then run #3 and #4 again.

Now go to http://<your-url>/env. You should see the environment dumped, which should include all of the cloud properties.

Now on your local machine. Run mysql and use the host, port, db, user and password from this information to connect. You should be able to connect with that info.

When I walk through these steps it works OK for me. If you're seeing an issue, there's either a problem with your service or something on your network is blocking the outgoing mysql traffic. Any chance you could try from another network like at home?

roseville

unread,
Nov 1, 2013, 3:34:02 PM11/1/13
to vcap...@cloudfoundry.org
I was able to make it work with though I didn't try with profile stuff

Following is the xml that should be used if you want to use c3P0.

        <cloud:data-source id="c3p0DataSource" service-name="mysql"/>
         <cloud:properties id="cloudProperties"/>
         <context:property-placeholder properties-ref="cloudProperties"/>

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:mysql://${cloud.services.mysql.connection.hostname}:${cloud.services.mysql.connection.port}/${cloud.services.mysql.connection.name}" />

            <property name="user" value="${cloud.services.mysql.connection.username}" />
            <property name="password" value="${cloud.services.mysql.connection.password}" />
            <property name="minPoolSize" value="2" />
            <property name="maxPoolSize" value="3" />
            <property name="maxIdleTime" value="319" />

        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="c3p0DataSource" />

            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                   
                   
                </props>
            </property>
            <property name="packagesToScan" value="com.iml.policytype.model" />
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
            p:sessionFactory-ref="sessionFactory" />
I didnt use variable {cloud.services.mysql.connection.jdbcUrl} which was causing the DB connection to fail. If you observe the env.log file the value for this entry also contains username and password instead of just hostname, portname and db name.

Also the property cloud.services.mysql.connection.hostname I have seen mentined as (cloud.services.mysql.connection.name) that was also causing the issue. I guess thats not a big deal as it depends on which data source you use. In some cases it could be name and in some cases it could be hostname.

Also the highlighted code was missing entries from my xml. I would be trying now with profile and would update the thread based on what I see.

After going through this I felt that spring and or or cloud foundry should provide one simple tutorial instead of giving bits an pieces of these things.
For those who are new to both would face difficulties to make it work. It should be very simple end to end tutorial.






Milind Moghe

unread,
Oct 31, 2013, 5:43:06 PM10/31/13
to vcap...@cloudfoundry.org
Ok now I have something to talk about. In the last email I talked about hard coding the jdbcurl in spring xml from env.log. It worked only whne I realized that you can not be directly use it. It contains username, password also which I guess is not allowing DB connection. I gave the valid URL and it worked. This has atleast proved that DB setup is done properly and if you have the right credentials then your application connects properly.

Now why spring auto configuration is not replacing the variables with actual values. I printed the values and was expecting the actual jdbcurl but it was showing me variables as values
  URL = ${cloud.services.mysql.connection.jdbcUrl}

This is now failing.So how this spring auto configuration works. I have not used profile here. Can you help me?

current xml is like this

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="${cloud.services.mysql.connection.jdbcUrl}" />
            <property name="user" value="${cloud.services.mysql.connection.username}" />
            <property name="password" value="${cloud.services.mysql.connection.password}" />

            <property name="minPoolSize" value="2" />
            <property name="maxPoolSize" value="3" />
            <property name="maxIdleTime" value="319" />

        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="c3p0DataSource" />

            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                   
                   
                </props>
            </property>
            <property name="packagesToScan" value="com.symantec.netbackup.iml.policytype.model" />
        </bean>


        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
            p:sessionFactory-ref="sessionFactory" />


Can you help me how to do this effectively?

Also how do we generally create the schema on cloud database? Is there any support from the spring cloud etc?







Reply all
Reply to author
Forward
0 new messages