How to Configure JNDI DataSource in the Tomcat Cartridge - Archived

This short blog post will tell you how to configure a JNDI datasource with the OpenShift Tomcat cartridge (Jboss EWS cartridge). After configuring the datasource, 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.This short blog post will tell you how to configure a JNDI datasource with the OpenShift Tomcat cartridge (Jboss EWS cartridge). After configuring the datasource, 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.

rhc app create tomcatjndidemo 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 postgresql-8.4 -a tomcatjndidemo

Step 3 : Defining Datasource in Tomcat Configuration Files

OpenShift gives you the flexibility to edit the tomcat configuration files. These files are location in the .openshift/config folder inside the 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>

The next change you have to make is in context.xml. You have to define a 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 the 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. We are using catalina.home system property to get the OPENSHIFT_DATA_DIR path.

common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.home}/../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 application is a simple Spring MVC application which does CRUD operations on a Member entity. Please note that the code also contains the tomcat configuration changes in .openshift/config folder. Please update the catalina.properties with your $OPENSHIFT_DATA_DIR value. The reason you need to pull the code is to see the tomcat configuration in action.

git remote add jndi git://github.com/shekhargulati/tomcatjndidemo.git
git pull -s recursive -X theirs jndi master

Next do a 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/

Go to the application url http://tomcatjndidemo-cix.rhcloud.com/ and register a new member. I registered a new member with name “Shekhar Gulati”.When you register a new member you can view the member json by hitting the url http://tomcatjndidemo-cix.rhcloud.com/rest/members/1. Replace 1 with your user id.
You can also ssh into the application instance and run the psql client to see that member got created in member table as shown below.

What’s Next?

Categories
OpenShift Online, PostgreSQL, Tomcat
Tags
, , ,
Comments are closed.