This short blog post will tell you how to configure JNDI datasource with OpenShift Tomcat cartridge. After configuring, we will also make it work with a sample Spring MVC application. Let’s get started.
Step 1 : Create Tomcat Application
After you have signed up for OpenShift and setup your account. Execute the rhc app create command as shown below to create tomcat application as shown below.
rhc app create -a tomcatjndidemo -t jbossews-1.0
Step 2 : Adding PostgreSQL Cartridge
Next we will add postgresql cartridge to our application by executing the command as shown below.
rhc cartridge add -a tomcatjndidemo -c postgresql-8.4
Step 3 : Defining Datasource in Tomcat Configuration Files
OpenShift gives you the flexibility to edit the tomcat configuration files. These files are location in .openshift/config folders inside tomcatjndidemo folder. If you look into this directory you will find that there are 5 files. For adding datasource we have to make changes in two files — context.xml and server.xml
In server.xml you have to define a resource under GlobalNamingResource as shown below.
<Resource name="jdbc/postgresqldb" auth="Container" type="javax.sql.DataSource"
username="${env.OPENSHIFT_POSTGRESQL_DB_USERNAME}" password="${env.OPENSHIFT_POSTGRESQL_DB_PASSWORD}"
url="jdbc:postgresql://${env.OPENSHIFT_POSTGRESQL_DB_HOST}:${env.OPENSHIFT_POSTGRESQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}"
driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
maxActive="120" maxIdle="5" validationQuery="select 1"
poolPreparedStatements="true">
</Resource>
Next change that you have to make is in context.xml. You have to define resource link to the datasource as shown below.
<ResourceLink name="jdbc/postgresqldb" global="jdbc/postgresqldb" type="javax.sql.DataSource"/>
Step 4 : Updating Tomcat Classpath with PostgreSQL JDBC Driver
Tomcat will require PostgreSQL JDBC driver jar to make connection with PostrgreSQL. You can’t copy the jars to tomcat lib directory as it is not writable. To do that, first ssh into the application instance and then download the jar in $OPENSHIFT_DATA_DIR as shown below.
cd $OPENSHIFT_DATA_DIR
wget http://repo1.maven.org/maven2/postgresql/postgresql/8.4-702.jdbc4/postgresql-8.4-702.jdbc4.jar
Next update the catalina.properties in your application .openshift/config folder to scan the OPENSHIFT_DATA_DIR as shown below. Please replace the openshift path with value of you OPENSHIFT_DATA_DIR environment variable.
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,/var/lib/openshift/2e110c16da18478aa607f742d34b70fb/app-root/data/*.jar
Step 5 : Pushing Changes to OpenShift
Now you can push the changes to OpenShift as shown below.
git commit -am "made changes for tomcat jndi datasource"
git push
Step 6 : Testing the DataSource
The last step in this blog is to test the datasource configuration we added in step 3 and step 4. To do that pull the code from my github repository. The code is a simple Spring MVC application which do CRUD operations on Member entity. Please note that the code also contains the tomcat configuration changes. So, please update the catalina.properties with your $OPENSHIFT_DATA_DIR value.
git remote add jndi git://github.com/shekhargulati/tomcatjndidemo.git
git pull -s recursive -X theirs jndi master
Next do git push which will build the war file and deploy the application to tomcat. The application should be accessible at http://tomcatjndidemo-cix.rhcloud.com/