[英]How to bind a JDBC datasource to JNDI context “java:comp/env/jdbc” when not to use web.xml
How to bind a JDBC datasource to JNDI context java:comp/env/jdbc
only using code-based approach. 如何仅使用基于代码的方法将JDBC数据源绑定到JNDI上下文
java:comp/env/jdbc
。
We need to write resource-ref
in the web.xml
for binding a dataSource to JNDI local context java:comp/env/jdbc
. 我们需要在
web.xml
编写resource-ref
,以将数据resource-ref
绑定到JNDI本地上下文java:comp/env/jdbc
。 But I want to use only org.springframework.web.context.AbstractContextLoaderInitializer
instead of web.xml
(the old approach). 但是我只想使用
org.springframework.web.context.AbstractContextLoaderInitializer
而不是web.xml
(旧方法)。
We know the method InitialContext#createSubcontext
. 我们知道方法
InitialContext#createSubcontext
。 But some application servers(eg Websphere) do not accept to edit the context java:comp/env/jdbc/
. 但是某些应用程序服务器(例如Websphere)不接受编辑上下文
java:comp/env/jdbc/
。
Any solutions? 有什么办法吗?
Versions: 版本:
You could try a full programmatic approach using Spring's SimpleNamingContextBuilder: 您可以尝试使用Spring的SimpleNamingContextBuilder的完整编程方法:
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
DataSource ds = new DriverManagerDataSource(...);
builder.bind("java:comp/env/jdbc/myds", ds); // you control the datasource
builder.activate();
It's mainly there for test-purposes. 它主要用于测试目的。 If you choose to use it then you need to provide your own connection pool (eg Apache's Jakarta Commons DBCP).
如果选择使用它,则需要提供自己的连接池(例如Apache的Jakarta Commons DBCP)。
I once used for testing when I deployed to cloudbees. 部署到cloudbees时,我曾经进行过测试。 see this blogpost
看到这个博客文章
This works in "plain" Tomcat. 这适用于“普通” Tomcat。 I don't have a EE server to test with, but you could try it and see what happens (I guess the JNDI binding name has to be unique to the server though).
我没有要测试的EE服务器,但是您可以尝试一下,然后看看会发生什么(尽管我猜JNDI绑定名称对于服务器必须是唯一的)。
What is your application server? 您的应用服务器是什么?
See if this helps. 看看是否有帮助。
How to use JNDI DataSource provided by Tomcat in Spring? 春季如何使用Tomcat提供的JNDI数据源?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.