Question

I am new to openshift. I created a sample application with spring security and tried to deploy in it. My spring-database.xml fragment looks like below.

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://${env.OPENSHIFT_MYSQL_DB_HOST}:${env.OPENSHIFT_MYSQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}" />
    <property name="username" value="${env.OPENSHIFT_MYSQL_DB_USERNAME}" />
    <property name="password" value="${env.OPENSHIFT_MYSQL_DB_PASSWORD}" />
</bean>

and I am getting following exception in the cloud environment.

Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "${env.OPENSHIFT_MYSQL_DB_PORT}"'

The following are my catrige/gear configuration

  • Mysql 5.1
  • Tomcat 7 (JBoss EWS 2.0)

any solutions how to overcome this issue?

Was it helpful?

Solution

The problem is the usage of expression like ${env.OPENSHIFT_MYSQL_DB_HOST}.

Those are operating system environment variables and as you can see in the exception that instead of trying to read the values of those system environment variables Spring is trying to turn it into a number.

Try something like #{systemEnvironment[OPENSHIFT_MYSQL_DB_HOST]} to read the OS environment variables. That's an expression to read system environment variable called OPENSHIFT_MYSQL_DB_HOST.

OTHER TIPS

For whom like me that the accpeted asnwer did not work for them I used this answer

I was just putting

<bean id="propertyPlaceholderChttps://stackoverflow.com/a/3965727/1460591onfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></bean>

into application context with adding varibales like this

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}" />
    <property name="username" value="${OPENSHIFT_MYSQL_DB_USERNAME}" />
    <property name="password" value="${OPENSHIFT_MYSQL_DB_PASSWORD}" />
</bean>

And It all worked just fine

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top