0
votes

deploying spring application to cloud foundry. Cloud foundry has postgres service, trying for jdbc connection using java-cfenv library application.properties:

database.url = #{cfJdbcEnv.findJdbcService().getJdbcUrl()}
database.username = #{ cfJdbcEnv.findJdbcService().getUsername() }
database.password = #{ cfJdbcEnv.findJdbcService().getPassword() }
database.driverClassName = #{cfJdbcEnv.findJdbcService().getDriverClassName()}

applicationContext.xml

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="localOverride" value="false" />
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>classpath:application-${spring.profiles.active}.properties</value>
        </list>
    </property>
</bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
  </bean>

pom.xml

<dependency>
  <groupId>io.pivotal.cfenv</groupId>
  <artifactId>java-cfenv-boot</artifactId>
  <version>2.2.2.RELEASE</version>
</dependency>

<dependency>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
          <version>42.2.9</version>
        </dependency>

Getting error:

Caused by: java.sql.SQLException: No suitable driver found for postgres://postgres:[email protected]:5432/postgres
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153) ~[spring-jdbc-4.0.3.RELEASE.jar:4.0.3.RELEASE]
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144) ~[spring-jdbc-4.0.3.RELEASE.jar:4.0.3.RELEASE]
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120) ~[spring-jdbc-4.0.3.RELEASE.jar:4.0.3.RELEASE]
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
   2021-03-03T17:35:15.01-0500 [APP/PROC/WEB/0] OUT     at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
   2021-03-03T17:35:16.00-0500 [APP/PROC/WEB/0] OUT [CONTAINER] org.hibernate.engine.jdbc.spi.SqlExceptionHelper   ERROR   No suitable driver found for postgres://postgres:7cpdezVdlt@apa-svc-cd1365a3-c6d0-436b-811d-23fc0ec66f0c.apps.apa.comcast.net:5432/postgres
   2021-03-03T17:35:16.00-0500 [APP/PROC/WEB/0] OUT [CONTAINER] org.hibernate.engine.jdbc.spi.SqlExceptionHelper   WARN    SQL Error: 0, SQLState: 08001

DataSource object getting instantiated with below code:

@Bean(name="dataSource")
    public DataSource getDataSource() { 
        
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(driverClassName); 
          String url =
          cfEnv.findCredentialsByName("portal-db").getHost(); 
          String username = cfEnv.findCredentialsByName("portal-db").getUsername(); String
          password = cfEnv.findCredentialsByName("portal-db").getPassword();
         
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl("jdbc:postgresql://"+url+":5432/postgres");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
}

I am not sure why when reading database.url from application.properties file, I am getting error related to wrong url: postgres://postgres:xxxx:5432/postgres

1

1 Answers

0
votes

From the error message, it seems to be getting the information. It just does not recognize the JDBC URL that's being provided by your service broker.

I would suggest the following:

  1. Run cf env (or look at the env variables in some other way[1]) and look at VCAP_SERVICES. Look for the property that you are trying to target and confirm that the service broker has a JDBC URL set that starts with jdbc: all JDBC URLs should start with jdbc:.

    As a side note, I think the format that's present use to work with the Spring Cloud Connectors, the predecessor library java-cfenv. I believe it would automatically insert the jdbc: part. Your broker may be specifying a JDBC URL that's not strictly a JDBC URL & depending on SCC to correct that.

  2. If you are able to confirm that the URL is just missing the leading jdbc: then you can a.) contact the maker of your service broker (who makes the integration with CF) and ask them to fix this and b.) adjust the way that you're setting the database.url in application.properties to see if you can reformat it in the correct way.

    Perhaps something like this (although I didn't test it):

    database.url = jdbc:#{cfJdbcEnv.findJdbcService().getJdbcUrl()}
    
  3. I believe there's a known issue for this, see https://github.com/pivotal-cf/java-cfenv/issues/127 and there is a code commit linked. So it looks like this should eventually start working, once a new release is cut & you upgrade to that release. You can monitor the Github issue for more details. UPDATE this should be fixed in release 2.3.0+.

[1] - Other ways you could look at the env variables: cf ssh into the container & cat /proc/<pid>/environ or dump the env variables through the spring boot actuator /env endpoint.