在Tomcat中绑定JNDI数据源? [英] Bind JNDI datasource in tomcat?
问题描述
是否可以通过程序将DataSource绑定到Tomcat的6 JNDI?
Is it possible to bind programatically a DataSource to Tomcat's 6 JNDI?
我想要创建一个DataSource,然后通过JNDI例如)。
I want to create a DataSource on the fly and then make it available through JNDI ( to ColdFusion for instance ).
这是我有:
public void bindToConext(DataSource dataSource) throws NamingException, SQLException {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
envContext.bind("jdbc/mydatasource", dataSource);
}
但我得到这个异常:
javax.naming.OperationNotSupportedException: Context is read only
有办法吗?
推荐答案
这是不可能的,因为tomcat的上下文在启动后变为只读。
Well it was not possible because tomcat's context becomes read-only after startup.
所以我们做的是使用SimpleJNDI,这是一个内存上下文(更像一个荣耀的HashMap),并为我们工作。
So what we did was to use SimpleJNDI which is a in memory context ( more like a glorified HashMap ) and that worked for us.
它需要一个 jndi.properties
文件,该文件必须在类路径中,以及您在哪里定义寻找资源的目录和初始上下文工厂
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
为了使用ColdFusion进行绑定,我们首先以编程方式创建数据源,然后将其绑定到上下文:
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 );
...
然后使用CF admin api创建一个类型为JNDI的CF数据源使用jndiname
Then using CF admin api we create a CF datasource of type JNDI just using the jndiname
这篇关于在Tomcat中绑定JNDI数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!