Bind JNDI datasource in tomcat?

只愿长相守 提交于 2019-12-01 17:21:17

Well it was not possible because tomcat's context becomes read-only after startup.

So what we did was to use SimpleJNDI which is a in memory context ( more like a glorified HashMap ) and that worked for us.

It needs a jndi.properties file that has to be in the classpath and where you define the directory where to look for resources and the initial context factory

java.naming.factory.initial=org.osjava.sj.SimpleContextFactory
org.osjava.sj.root=some/relative/path
org.osjava.jndi.delimiter=/
org.osjava.sj.jndi.shared=true

To make the binding with ColdFusion first we create the data source programatically and then bind it to the context:

DataSource ds = ...
Context c = new InitialContext();
c.bind( "jdbc/my/blah/"+var , ds );
...

Then using CF admin api we create a CF datasource of type JNDI just using the jndiname

Tomcat's working context is java:comp/env. This context is read-only. But you can create your own contexts, using Tomcat's JNDI Implementation, as long as you keep yourself out of "java:comp/env".

Context ctx = new InitialContext()
ctx.createSubcontext("any_name").createSubcontext("any_sub_name");
ctx.bind("any_name/any_sub_name/myDataSource", myDataSource);

By default Tomcat's contexts are shared, so the DataSource is retrievable from anywhere in your app this way:

Context ctx = new InitialContext()
DataSource ds = (DataSource)ctx.lookup("any_name/any_sub_name/myDataSource");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!