iBatis and ColdFusion

One of my colleagues has been writing Java web apps using Spring, Struts and the iBatis Data Mapper.

A lot of his code would be very useful to use in my ColdFusion apps, so I wanted to find out if there was a way to integrate ColdFusion and iBatis.

Back in July 2006, Charlie Arehart blogged about this possibility however as far as I am aware no-one provided a working solution.

However with the help of Mark Mandel’s Javaloader, and a blog post on integrating ColdFusion and Spring from Kevan Stannard I have been able to put together a demo of iBatis and ColdFusion working together.

My sample app code can be downloaded from http://www.abm.id.au/downloads/cfibatisdemo.zip

To get this to work you need to download a few dependencies:

Once you’ve downloaded these extract them to an appropriate place in your file system (I put them all at c:javadev). Then update the build.xml in my sample app to reference where you put these libraries. You should also ensure ant is on your path.

You will also need to set up a sample database and connection to run this demo. A mysql script is included in the db/ directory of the sample code.

Setting up the database connection is a bit more complicated. My solution was to set up a datasource using JNDI. I have ColdFusion 8 installed in a multiserver config so this had to be set up in the JRun Management Console.

The following screenshot shows the settings I used to create this datasource. Note that I had to change the MySQL Driver Class Name from the default to get it to work with MySQL 5.x. Also note that you will need to update web/beans.xml to use the correct name you give this datasource.

JNDI Datasource settings

If you are using another server, you will need to check your app server documentation to find out how to set up a JNDI datasource in your environment.

A couple of gotcha’s I found with the JNDI datasource are that if the database is not running when you start the server, it doesn’t seen to initialise the datasource correctly. Similarly if the database is stopped and restarted, I found you need to restart the JRun server to get things working again. Obviously this would be a problem in a production environment, and needs further investigation.

Finally to get things up and running you need to install Javaloader into your ColdFusion root directory or create an appropriate mapping.

Then build the sample app by issuing the following command in the directory where you extracted the sample code:

ant dist

You can then copy the contents of the /dist dir from the sample app over to your ColdFusion server, and run the sample.

Please feel free to leave comments on this post if you have any problems or suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *