12.3. OpenShift Configure

This section explains the changes required in application configuration and code to run Fins on OpenShift.

JDO config for MySQL

In JDO configuration file META-INF/jdoconfig.xml, we have to add a new persistence-manager-factory for MySQL.

Configuration requires connection URL, User Name and Password and we obtain them through

$ rhc app ssh demo
> env | grep -i mysql
OPENSHIFT_MYSQL_DB_PORT=60311
OPENSHIFT_MYSQL_DB_HOST=51fb####-finsdemo.rhcloud.com
OPENSHIFT_MYSQL_DB_PASSWORD=w1pd####
OPENSHIFT_MYSQL_DB_USERNAME=admin####

Use the values for configuration in jdoconfig.xml.

META-INF/jdoconfig.xml

        <persistence-manager-factory name="datastore">
                <property name="javax.jdo.PersistenceManagerFactoryClass"
                        value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" />
                <property name="javax.jdo.option.ConnectionDriverName"
                          value="com.mysql.jdbc.Driver" />
                <property name="javax.jdo.option.ConnectionURL" 
                      value="jdbc:mysql://51fb####-finsdemo.rhcloud.com:60311/demo" />
                <property name="javax.jdo.option.ConnectionUserName" value="admin####" />
                <property name="javax.jdo.option.ConnectionPassword" value="w1pd####" />
           ....

This hard coded configuration works, but problem with this approach is that, OpenShift may change the Host and Port in future and then application fails to connect to MySQL. To overcome these nasty surprises, OpenShift suggests to use environment variables as shown in following snippet.

META-INF/jdoconfig.xml

   <persistence-manager-factory name="datastore">
      ....
       <property name="javax.jdo.option.ConnectionURL"
          value="jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/demo" />
 
      ....

But, when JDO tries create the connection it doesn’t patch the env variables with their actual values and throws exception. OpenShift forum suggests to use ${env.OPENSHIFT_MYSQL_DB_HOST} but that too fails.

Another option is to add a JNDI datasource to Tomcat context file - META-INF/context.xml with these env variables. But, even Tomcat fails to patch the variable with actual values.

 
 

Fortunately OpenShift JBoss EWS comes with a pre-configured JNDI datasource for MySQL and PostgreSQL, which is in .openshift/config/context.xml in cloned application folder . It defines two JNDI resources; jdbc/MysqlDS and jdbc/PostgreSQLDS using OpenShift environment variables and we may use this resource for our JDO config.

The new persistence-manager-factory configuration for MySQL is added to jdoconfig.xml.

META-INF/jdoconfig.xml

       <persistence-manager-factory name="datastore">
                <property name="javax.jdo.PersistenceManagerFactoryClass"
                        value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" />
                <property name="javax.jdo.option.ConnectionFactoryName"
                          value="java:comp/env/jdbc/MysqlDS" />

                <property name="javax.jdo.option.NontransactionalRead" value="true" />
                <property name="javax.jdo.option.NontransactionalWrite"
                        value="true" />
                <property name="javax.jdo.option.RetainValues" value="true" />
                <property name="datanucleus.appengine.autoCreateDatastoreTxns"
                        value="true" />
                <property name="datanucleus.cache.level2.type" value="soft" />
                <property name="datanucleus.metadata.validate" value="false" />
                <property name="datanucleus.autoCreateSchema" value="true" />
        </persistence-manager-factory>

To use JNDI resource, we require a resource-ref definition in web.xml.

war/WEB-INF/web.xml

        <resource-ref>
                <description>DataSource</description>
                <res-ref-name>jdbc/MysqlDS</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
        </resource-ref>

Value generator uuid-string throws duplicate key exception for MySQL, instead it requires auid as value generator. In src/in/fins/shared/package.jdo value-strategy attribute of Data and DataGroup is set to auid.

 
 
Form URL

For FormPanel, we had given form action as /upload, but this fails to work in OpenShift. For OpenShift, we have to remove the leading / from /upload in in/fins/client/widget/UploadPanel.ui.xml and in/fins/client/widget/CachePanel.ui.xml.

With these changes, Fins is ready for deployment and the next section explains how it done.