Wednesday, 27 May 2009

ColdFusion (JRun) and JNDI mapping for JDBC and Mail Sessions

We have a situation where we're working on a ColdFusion project that will be deployed on glassfish (in production). Glassfish will use JNDI to manage all the connections to external resources (namely the databases and the mail server).

By default, ColdFusion ships with its own J2EE container (JRun) - so as the project began to take shape, we wanted to be able to create similar JNDI bindings within JRun - so we could closely mimic how the application would behave when deployed on glassfish.

I think I'm being fair saying the documentation for ColdFusion's JRun config files isn't exactly what you'd call the most comprehensive, so here's a rough list of the steps we took to set up JNDI mappings in ColdFusion for a JDBC[JNDI] datasource and for a mail server and what was required to get it all working:

Configuring ColdFusion to work with a JDBC datasource

1. Get the relevant Java JDBC driver for the database you're trying to use (for example, MS SQL Server would use sqljdbc4.jar) and put that jar into your Coldfusion lib folder (e.g. ColdFusion8/lib)

2. In the jrun-web.xml (/Applications/ColdFusion8/wwwroot/WEB-INF/jrun-web.xml), you need to declare the JNDI bindings before you can configure them, so the last few lines of your xml document should look like this:



N.B. make sure this is just above the closing tag (</jrun-web-app>)

3. In your jrun-resources.xml (/Applications/ColdFusion8/runtime/servers/coldfusion/SERVER-INF/jrun-resources.xml)



N.B. This should be just before the closing tag (</jrun-resources>)

An important note here should be that there should be a match between the names in the two files on:
<jndi-name>your-jndi-db-ref-name</jndi-name>
and
<jndi-name>your-jndi-db-ref-name</jndi-name>
respectively.

This should complete the binding for Coldfusion/JRun - restart ColdFusion and you should be good to go.

Configuring ColdFusion to work with a MailServer via JNDI

1. In the jrun-web.xml:



2. In your jrun-resources.xml:



As before, restart ColdFusion to test whether these changes worked.

Useful References:
http://www.adobe.com/support/jrun/working_jrun/jrun4_jndi_and_j2ee_enc/jrun4_jndi_and_j2ee_enc03.html
http://livedocs.adobe.com/jrun/4/descriptordocs/jrun-resources/jrun-resources.htm

2 comments:

jg said...

In regards to your first paragraph:

Isn't the reason you'd want to do this type of thing more to do with running Java components that use JNDI within a ColdFusion App?

JNDI is one of the ways of decoupling objects from each other in Java (kind of like dependency injection in a way).

So, if you have a set of Java code that uses JNDI, and you want to use that Java code within ColdFusion , you'd want to setup datasources and mail sessions in the ColdFusion J2EE server (JRun), or on the J2EE server you're deploying on (in your case, Glassfish).

Right?

Alex Bleasdale said...

That's definitely clearer, jg. Thanks :)