Configuring JNDI DataSource with OpenShift Tomcat Cartridge

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"
	driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
	maxActive="120" maxIdle="5" validationQuery="select 1"

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.


Next update the 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.


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 with your $OPENSHIFT_DATA_DIR value.

git remote add jndi 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

14 thoughts on “Configuring JNDI DataSource with OpenShift Tomcat Cartridge”

  1. Hi, I modified the server.xml and when I start tomcat I get the error:

    WARNING: Unexpected exception resolving reference
    org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Illegal connection port value ‘${env.OPENSHIFT_MYSQL_DB_PORT}’)

    1. To me it looks like you have not added mysql cartridge to the application. You need to add mysql cartridge to the application using rhc cartridge add -a appname -c mysql-5.1

  2. Excellent items from you, man. I’ve be aware your stuff previous to and you are just extremely great. I really like what you have acquired right here, certainly like what you are saying and the best way in which you say it. You are making it entertaining and you still take care of to stay it smart. I can’t wait to learn much
    more from you. This is actually a terrific site.

  3. Wow that was strange. I just wrote an extremely long comment
    but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing
    all that over again. Anyhow, just wanted to say wonderful blog!

  4. Hello! I know this is somewhat off topic but I was wondering which blog platform are you using for this site?

    I’m getting fed up of WordPress because I’ve had problems with hackers
    and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.

  5. I tend not to comment, but after reading a ton of
    comments on Configuring JNDI DataSource with OpenShift Tomcat Cartridge Shekhar Gulati : OpenShift Evangelist, Software Developer, Writer, and Speaker.
    I actually do have some questions for you if it’s allright. Could it be simply me or do some of the responses appear like they are coming from brain dead people? 😛 And, if you are writing at additional sites, I’d
    like to follow everything new you have to post. Could you list
    of all of your social networking sites like your
    Facebook page, twitter feed, or linkedin profile?

  6. Excellent beat ! I wish to apprentice while you amend your site, how cann i
    subscribe for a blog site? The account aided me a acceptable deal.

    I had bwen tiny bit acquainted of this our broadcast proovided bright clear idea

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: