Java 如何从 Web 容器外部查找 JNDI 数据源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2521421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I lookup a JNDI Datasource from outside a web container?
提问by masotime
I have the following environment set up:
我设置了以下环境:
- Java 1.5
- Sun Application Server 8.2
- Oracle 10 XE
- Struts 2
- Hibernate
- 爪哇 1.5
- 太阳应用服务器 8.2
- 甲骨文 10 XE
- 支柱 2
- 休眠
I'm interested to know how I can write code for a Java client (i.e. outside of a web application) that can reference the JNDI datasource provided by the application server.
我很想知道如何为可以引用应用程序服务器提供的 JNDI 数据源的 Java 客户端(即在 Web 应用程序之外)编写代码。
The ports for the Sun Application Server are all at their defaults. There is a JNDI datasource named jdbc/xxxx in the server configuration, but I noticed that the Hibernate configuration for the web application uses the name java:comp/env/jdbc/xxxx instead.
Sun Application Server 的端口均采用默认设置。服务器配置中有一个名为 jdbc/xxxx 的 JNDI 数据源,但我注意到 Web 应用程序的 Hibernate 配置使用名称 java:comp/env/jdbc/xxxx 代替。
Most of the examples I've seen so far involve code like
到目前为止,我看到的大多数示例都涉及类似的代码
Context ctx = new InitialContext();
ctx.lookup("jdbc/xxxx");
But it seems I'm either using the wrong JNDI name, or I need to configure a jndi.properties or other configuration file to correctly point to a listener? I have appserv-rt.jar from the Sun Application Server which has a jndi.properties inside of it, but it does not seem to help.
但似乎我使用了错误的 JNDI 名称,或者我需要配置 jndi.properties 或其他配置文件以正确指向侦听器?我有来自 Sun Application Server 的 appserv-rt.jar,里面有一个 jndi.properties,但它似乎没有帮助。
There's a similar question here, but it doesn't give any code / refers to having iBatis obtain the JNDI Datasource automatically: Accessing Datasource from Outside A Web Container (through JNDI)
这里有一个类似的问题,但它没有给出任何代码/指的是让 iBatis 自动获取 JNDI 数据源:从外部 Web 容器访问数据源(通过 JNDI)
采纳答案by djsumdog
I got stuck on this exact same problem. I wrote a small tutorial. Basically you have to create your own implementation of the DataSource objects and add them to your own custom initial context. There are source examples here:
我被困在这个完全相同的问题上。我写了一个小教程。基本上,您必须创建自己的 DataSource 对象实现并将它们添加到您自己的自定义初始上下文中。这里有源示例:
Running Beans Locally that use Application Server Data Sources
回答by Will Hartung
If you're talking some every day generic Java application running outside of the container, then you're out of luck. Effectively you would need to configure your own JNDI implementation, with it's own configure connection pool, etc.
如果您每天都在谈论一些在容器外运行的通用 Java 应用程序,那么您就不走运了。实际上,您需要配置自己的 JNDI 实现,使用它自己的配置连接池等。
However, you can write Java EE "standalone" applications. These are applications that run within the Java EE application client. Basically, it's an app that is deployed and packaged, but then executed using a launcher that's provided by your Java EE container.
但是,您可以编写 Java EE“独立”应用程序。这些是在 Java EE 应用程序客户端中运行的应用程序。基本上,它是一个部署和打包的应用程序,然后使用 Java EE 容器提供的启动器执行。
When running within an application client environment, all of the resources of the app server (connection pools, EJBs, queues, etc.) are available to your app just like they would be if the code were deployed within the app server.
在应用程序客户端环境中运行时,应用程序服务器的所有资源(连接池、EJB、队列等)都可供您的应用程序使用,就像代码部署在应用程序服务器中一样。
Hereis some tutorial documentation for Sun App Server 8.2, which is a J2EE 1.4 container.
这是 Sun App Server 8.2(J2EE 1.4 容器)的一些教程文档。
If it's possible I'd strongly suggest upgrading to Glassfish v2.1, just a more modern, better all around server that should deploy your apps just fine as is, since it's a descendant of 8.2.
如果可能的话,我强烈建议升级到 Glassfish v2.1,这是一个更现代、更全面的服务器,应该可以按原样部署您的应用程序,因为它是 8.2 的后代。
回答by pitpod
What you want is an Application Client
你想要的是一个应用程序客户端
Alternatively you could establish a plain JDBC connection from your standalone client which might be easier to create - but you'll have to configure the connection details in the client and can not reuse the settings from your application server.
或者,您可以从独立客户端建立一个普通的 JDBC 连接,这可能更容易创建 - 但您必须在客户端中配置连接详细信息,并且不能重用来自应用程序服务器的设置。
回答by runningboffin
This might be a bit late for you but I have used the Simple-JNDI libraryfor many years for the exact purpose you mention. I'm not sure if it has all the options you may need but it sufficed for my command line utilities.
这对您来说可能有点晚了,但我多年来一直使用Simple-JNDI 库来达到您提到的确切目的。我不确定它是否具有您可能需要的所有选项,但对于我的命令行实用程序来说已经足够了。
回答by Holger Thurow
Try Simple-JNDI. It gives you an in-memory implementation of a JNDI Service and allows you to populate the JNDI environment with objects defined in property files. There is also support for loading datasources or connection pools configured in a file.
尝试简单 JNDI。它为您提供 JNDI 服务的内存实现,并允许您使用属性文件中定义的对象填充 JNDI 环境。还支持加载文件中配置的数据源或连接池。
To get a connection pool you have to create a file like this:
要获得连接池,您必须创建这样的文件:
type=javax.sql.DataSource
driver=com.sybase.jdbc3.jdbc.SybDriver
pool=myDataSource
url=jdbc:sybase:Tds:servername:5000
user=user
password=password
In your application you can access the pool via
在您的应用程序中,您可以通过以下方式访问池
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("path/to/your/connectionPool");
I haved used Simple-JNDI for this purpose for years now. But it is not under active development anymore. Because I found some issues concerning shared contexts (especially using datasources), I decided to branch the original project and to add some new features. Now there is a 0.13.0. You can find more about it at https://github.com/h-thurow/Simple-JNDI.
多年来,我一直为此目的使用 Simple-JNDI。但它不再处于积极开发状态。因为我发现了一些关于共享上下文(尤其是使用数据源)的问题,所以我决定对原始项目进行分支并添加一些新功能。现在有一个 0.13.0。您可以在https://github.com/h-thurow/Simple-JNDI找到更多相关信息。