views:

1059

answers:

2

I have a servlet that I would like to run within ColdFusion MX 7. I would like to make use of an existing ColdFusion DSN as a javax.sql.DataSource, if possible.

I thought something like

coldfusion.server.ServiceFactory.getDataSourceService().getDatasource(dsname);

would work, but unfortunately the servlet returns

java.lang.NoClassDefFoundError: coldfusion/server/ServiceFactory
+1  A: 

That code will work fine, you just don't have ServiceFactory in your classpath. Ie, Java can't load that class. Try including a dependency on cfusion.jar from C:\CFusionMX7\lib.

Chase Seibert
Now getting: coldfusion.server.ServiceFactory$ServiceNotAvailableException: The DataSource service is not available. neo-query.xml is fine, queries from .cfm pages still work.
AlexJReid
Hmm, I have only done this is a java class being called from ColdFusion with CFOBJECT. Is that what you're doing? You may need to be in that context to get to the datasources.
Chase Seibert
It is looking that way. I've got a servlet that needs access to a database. I'll see if creating a new JNDI datasource in jrun-resources.xml works, should do.
AlexJReid
A: 

It seems the simplest way to do this is to add an additional JNDI datasource into jrun-resources.xml. This can then be accessed in the conventional way:

Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("mydatasource");

It does mean duplicating database connection configuration, but I would rather do this than work with the largely undocumented coldfusion.server.* classes.

AlexJReid