使用由Tomcat创建的另一个应用程序创建的JNDI数据源 [英] Using a JNDI datasource created by another application with Tomcat

查看:114
本文介绍了使用由Tomcat创建的另一个应用程序创建的JNDI数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个包含dataSource属性的.properties文件。
我使用以下代码设置了对此dataSource的JNDI引用:

I have a .properties file in my application which contains dataSource properties. I set up a JNDI reference to this dataSource using the following code :

// first I create MyDataSource from the properties found in the .properties file
//then :

Context initContext = new InitialContext();
initContext.createSubcontext("jdbc");
initContext.createSubcontext("jdbc/oracle");
initContext.rebind(jdbc/oracle/myDataSource, MyDataSource);

如果我在此应用程序中使用查找,则会找到dataSource:

If I use a lookup in this application, the dataSource is found :

Context initContext = new InitialContext();
BasicDataSource dataSource = 
            (BasicDataSource) initContext.lookup("jdbc/oracle/myDataSource")
//everything works fine and I can use my dataSource to getConnection,
//requests, etc...

现在我想在另一个应用程序中使用此dataSource。但是,如果我执行与之前相同的查找,我找不到myDataSource(而在tomcat中仍然存在先前的应用程序,并且在启动时通过侦听器完成jndi绑定)。

Now I would like to use this dataSource in another application. But if I do the same lookup than previously, I don't find myDataSource (whereas there is still the previous application in tomcat and the jndi binding is done on start-up with the help of a listener).

我如何在第二个应用程序中获取myDataSource,因为我无法在server.xml或context.xml文件中使用Tomcat的资源(出于不同的原因,我必须使用此.properties文件)?

How can I get myDataSource in this second application, given that I can't use a Tomcat's resource in server.xml or a context.xml file (for different reasons I have to use this .properties file)?

谢谢

推荐答案

本地JDNI目录是在Tomcat中只读。不过,您可以在LifecycleListener中绑定全局JNDI资源,然后将它们链接到您的上下文(*):

"local" JDNI directories are read-only in Tomcat. Nevertheless, you can bind "global" JNDI resources in a LifecycleListener, and then "link" them to your context(s)(*):

您需要实现组织.apache.catalina.LifecycleListener http:// tomcat .apache.org / tomcat-6.0-doc / api / org / apache / catalina / LifecycleListener.html

You need to implement org.apache.catalina.LifecycleListener http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/LifecycleListener.html

然后在server.xml中注册它这个(以及其他听众):

Then register it in your server.xml like this (along with the other listeners):

<Listener className="yourlistener.YourLifecycleListener"/>

你的听众应该等待2个事件:

Your listener should await for 2 events:

public void lifecycleEvent(final LifecycleEvent event) {

    if (Lifecycle.START_EVENT.equals(event.getType())) {
      // Create your datasource instance...
      Context initContext = new InitialContext();
              initContext.createSubcontext("jdbc");
              initContext.createSubcontext("jdbc/oracle");
              initContext.rebind("jdbc/oracle/myDataSource", myDataSource);
    } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
      // unbind...
    }
}

然后,你必须通过使用ResourceLink元素将它们从全局JNDI目录链接到本地JNDI目录来传播资源访问在您的META-INF / context.xml中:

Then you'll have to propagate resource accesses by "linking" them from "global" JNDI directory to "local" JNDI directory using ResourceLink element in your META-INF/context.xml:

<ResourceLink name="jdbc/oracle/myDataSource" global="jdbc/oracle/myDataSource"
    type="javax.sql.DataSource" />

到目前为止,这对我有用。

That worked for me so far.

(*)一些注意事项:

使用生命周期监听器有一个优势。由于无法保证上下文创建的顺序。优点是你的所有上下文都会看到这个对象被创建。

There's an advantage on using lifecycle listeners. Since the order of context creation is not guaranteed. The advantage is that all of your contexts will see this object created.

如果你需要在生命周期监听器创建时更动态地创建和配置数据源创建,请注意你可以绑定实现Factory模式的自定义类。

If you need to create and configure datasource creation more dynamically that on lifecycle listener creation, note that you can bind a custom class implementing the Factory pattern.

为避免类加载不兼容问题,请考虑将监听器,数据源等类放在Tomcat lib目录中的jar文件中,所以它们包括在普通的类加载器中。

To avoid classloading incompatibility problems, consider putting your listener, datasource, etc. classes in a jar file in the Tomcat lib directory, so they're included y the common classloader.

问候。

这篇关于使用由Tomcat创建的另一个应用程序创建的JNDI数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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