如何从 Web 容器外部查找 JNDI 数据源?

发布于 2024-08-26 19:56:51 字数 813 浏览 6 评论 0原文

我设置了以下环境:

  • Java 1.5
  • Sun Application Server 8.2
  • Oracle 10 XE
  • Struts 2
  • Hibernate

我有兴趣知道如何为 Java 客户端(即 Web 应用程序之外)编写可以引用提供的 JNDI 数据源的代码由应用服务器。

Sun Application Server 的端口均采用默认值。服务器配置中有一个名为 jdbc/xxxx 的 JNDI 数据源,但我注意到 Web 应用程序的 Hibernate 配置使用名称 java:comp/env/jdbc/xxxx 代替。

到目前为止我看到的大多数示例都涉及类似的代码

Context ctx = new InitialContext();
ctx.lookup("jdbc/xxxx");

但似乎我使用了错误的 JNDI 名称,或者我需要配置 jndi.properties 或其他配置文件以正确指向侦听器?我有来自 Sun Application Server 的 appserv-rt.jar,其中有 jndi.properties,但它似乎没有帮助。

这里有一个类似的问题,但它没有给出任何代码/指的是让 iBatis 自动获取 JNDI 数据源: 从 Web 容器外部访问数据源(通过 JNDI)

I have the following environment set up:

  • Java 1.5
  • Sun Application Server 8.2
  • Oracle 10 XE
  • Struts 2
  • Hibernate

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.

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.

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

临风闻羌笛 2024-09-02 19:56:51

我陷入了这个完全相同的问题。我写了一个小教程。基本上,您必须创建自己的 DataSource 对象实现并将它们添加到您自己的自定义初始上下文中。这里有源示例:

本地运行使用应用程序的 Bean服务器数据源

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:

Running Beans Locally that use Application Server Data Sources

囍笑 2024-09-02 19:56:51

尝试简单的 JNDI。它为您提供 JNDI 服务的内存中实现,并允许您使用属性文件中定义的对象填充 JNDI 环境。还支持加载文件中配置的数据源或连接池。

要获得连接池,您必须创建一个如下文件:

type=javax.sql.DataSource
driver=com.sybase.jdbc3.jdbc.SybDriver
pool=myDataSource
url=jdbc:sybase:Tds:servername:5000
user=user
password=password

在您的应用程序中,您可以通过以下方式访问连接池:

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("path/to/your/connectionPool");

我多年来一直使用 Simple-JNDI 来实现此目的。但它不再处于积极开发之中。因为我发现了一些有关共享上下文(尤其是使用数据源)的问题,所以我决定对原始项目进行分支并添加一些新功能。现在有0.13.0。您可以在 https://github.com/h-thurow/Simple-JNDI 找到更多相关信息

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.

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.

丶视觉 2024-09-02 19:56:51

如果您正在谈论一些在容器外部运行的日常通用 Java 应用程序,那么您就不走运了。实际上,您需要配置自己的 JNDI 实现,以及它自己的配置连接池等。

但是,您可以编写 Java EE“独立”应用程序。这些是在 Java EE 应用程序客户端中运行的应用程序。基本上,它是一个已部署和打包的应用程序,然后使用 Java EE 容器提供的启动器执行。

在应用程序客户端环境中运行时,应用程序服务器的所有资源(连接池、EJB、队列等)都可供您的应用程序使用,就像代码部署在应用程序服务器中一样。

这里是 Sun App 的一些教程文档Server 8.2,它是一个 J2EE 1.4 容器。

如果可能的话,我强烈建议升级到 Glassfish v2.1,这是一个更现代、更好的全能服务器,应该可以很好地部署您的应用程序,因为它是 8.2 的后代。

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.

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.

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.

Here is some tutorial documentation for Sun App Server 8.2, which is a J2EE 1.4 container.

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.

遗弃M 2024-09-02 19:56:51

这对您来说可能有点晚了,但我已经使用了 Simple-JNDI 库 多年来,为了您提到的确切目的。我不确定它是否具有您可能需要的所有选项,但它足以满足我的命令行实用程序。

This might be a bit late for you but I have used the Simple-JNDI library for 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.

孤者何惧 2024-09-02 19:56:51

您想要的是 应用程序客户端

或者您可以建立一个普通的 JDBC 连接从您的独立客户端可能更容易创建 - 但您必须在客户端中配置连接详细信息,并且无法重用应用程序服务器中的设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文