How do I lookup a JNDI Datasource from outside a web container?

送分小仙女□ 提交于 2019-11-29 20:44:41
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:

Running Beans Locally that use Application Server Data Sources

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.

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.

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.

runningboffin

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!