Thursday, May 10, 2007

Today I am working with tomcat trying to create a dao using jndi and then writing a test. For those of you that don't know this Tomcat does not allow access from outside the container to the jndi in the way that JBoss does. In order to do this you will need to use a third party tool like Simple-jndi and can be found at: http://www.osjava.org/simple-jndi/.

Fairly simple to use for the most part add it into your project classpath, and set up your jndi for Tomcat in the way the doc says. http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html To make it work correctly you will need to create a few folders and files. One of which is a jndi.properties file. Add the following into this file.

java.naming.factory.initial=org.osjava.sj.SimpleContextFactory
org.osjava.sj.root=file://config/
org.osjava.sj.delimiter=/
org.osjava.sj.space=java:/comp/env

The last line is very important to keep just like it is. If you get the following error when trying to connect this is probably where your issue is.

java.lang.ClassCastException: org.osjava.sj.memory.MemoryContext
at com.yourcompany.util.ServiceLocator.......

Also I am using eclipse and in the root of my project I created a folder I created a folder structure that looks like this [project root]/config/jdbc/

Within that folder I then place a properties file named [jndiName].properties The jndiName is important because Simple-jndi uses the name to look up the properties file. Also for jdbc structure it is important to have the folder structure setup the way I have shown above to ensure it works correctly.

Have fun and it actually works very slick!