views:

713

answers:

2

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: http://stackoverflow.com/questions/39053/accessing-datasource-from-outside-a-web-container-through-jndi

A: 

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 JEE "standalone" applications. These are applications that run within the JEE application client. Basically, it's an app that is deployed and packaged, but then executed using a launcher that's provided by your JEE 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.

Will Hartung
Thanks for the idea of writing a standalone JEE application. It's an interesting alternative, but I was hoping for the possibility of using the app server as a direct JNDI provider to obtain the remote datasource directly. The choice of Sun App Server 8.2 is due to legacy reasons unfortunately, so we can't shift to Glassfish, which would definitely be better.
A: 

What you want is an "application client" (see http://java.sun.com/developer/technicalArticles/J2EE/appclient/ for details).

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.

pitpod
Hi, thanks for the response. The application client is interesting, but it's a little too heavy for my needs. The intent was to reuse the connection defined in the web-server to maintain consistency as a single point to get a database connection.