Lookup DBaaS connection with Spring Cloud Foundry Connector

Hello there :slight_smile:

I am trying to deploy an example CUBA app with postgresql to Pivotals Cloud Foundry Platform.
I’ve followed the blog post provided by Mario.
Generally speaking, it is working. However, I have to hard-code the DBaaS credentials.
What I am looking for is a way to differentiate between local development setup where the database may be linked locally and the database connection, which should be resolved dynamically by using the spring cloud foundry connector library.

Steps to reproduce:

  • enter credentials for local development database in the project properties via CUBA Studio
  • create deployment descriptor with the single war file option (custom war-context.xml with DBaaS credentials and single-web-war.xml files)
  • tried to remove the Resource tag from the war-context.xml, didn’t make a difference
  • use dependecy injection via spring xml configuration in spring.xml (see the blog post)

I’ve also tried to use the Java-based approach for configuring the data source, neither this nor the xml config succeeded.

I’m getting following error:

2018-01-25T09:02:28.926+01:00 [APP/PROC/WEB/0] [OUT] org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [cuba-spring.xml]: Cannot resolve reference to bean ‘cubaDataSource’ while setting bean property ‘dataSource’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘cubaDataSource’ defined in class path resource [cuba-spring.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/CubaDS] is not bound in this Context. Unable to find [jdbc].

As it seems, the app is trying to find the jndi datasource before my custom configuration code is invoked and is therefore failing.

Are there any steps I can take to fix the issue?
You can find an example project attached.cf-database-test.zip (73.4 KB)

Your help is much appreciated.

Thanks for the great framework!


Are you sure your CloudFoundryDataSourceFactory is actually invoked? I doubt because I don’t see such thing as profile="cloud" in Mario’s blog. Try to add some logging to the bean and see the output.

Alright, I removed the beans profile="cloud" tag and now it is working.
It looks like

[OUT] [CONTAINER] nfiguration.spring.CloudProfileApplicationListener INFO Adding ‘cloud’ to list of active profiles

[OUT] [CONTAINER] g.CloudPropertySourceApplicationContextInitializer INFO Adding ‘cloud’ PropertySource to ApplicationContext

is actually setup after the data source connection is created / queried.

But I wonder, just out of curiosity, why does this spring.xml-configuration not override the local development database connection when I start the development server through Cuba Studio?

EDIT: I did a workaround by introducing a prod profile and appending it to the Cloud Foundry manifest file as an extra JVM argument. Now the DBaaS connection is still picked up and I can safely assume that it will only happen when I deploy the app to PCF!

Thanks for your help and time! :slight_smile:

Good to hear it is working now.
Regarding why it didn’t affected your dev environment - I honestly have no idea, it should.