如何从Web容器外部查找JNDI数据源? [英] How do I lookup a JNDI Datasource from outside a web container?

查看:196
本文介绍了如何从Web容器外部查找JNDI数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了以下环境:


  • Java 1.5

  • Sun Application Server 8.2

  • Oracle 10 XE

  • Struts 2

  • Hibernate

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

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

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.

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

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");

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

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.

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

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)

推荐答案

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

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:

在本地运行使用Application Server数据源的Bean

这篇关于如何从Web容器外部查找JNDI数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆