0

i'm developing a little java client program to retrieve database connection from WAS connection pool through JNDI lookup. But this java client locates outside WAS scope. The code details as below:

import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;

public class WASORB {

/**
 * @param args
 * @throws NamingException 
 */
public static void main(String[] args) throws Exception {

    InitialContext initialContext = getInitialContext();

    javax.sql.DataSource ds = (DataSource)initialContext.lookup("jndi/local");

    Connection cn = ds.getConnection("*****", "*****");

    if(cn != null)
        System.out.println ("Connection ok");

    String sql = "select * from ACT";
    Statement st = cn.createStatement();
    ResultSet rs = st.executeQuery(sql);

    while(rs.next()){
        System.out.println (rs.getString(2));
    }
}

public static InitialContext getInitialContext() throws NamingException {
    Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
    env.put(Context.PROVIDER_URL,"iiop://localhost:2811"); 

    InitialContext context = new InitialContext(env);

    return context;
}

}

The code is working fine and return result correctly. But i've got two questions don't understand here:

  1. Why do i have to provide userid/password when invoking method getConnection? From my understanding, i've already configured this authentication credential within WAS DataSource. And WAS plays the likely proxy role when my java client tries to get connection. If so, why am i required to input again here?

  2. Once the client code gets running, there should be one connection within the WAS specific database connection pool from the point of my view. But i don't see any connection created in the connection pool. so why this happen? or am i understanding wrongly?

Thanks

wing2ofsky
  • 926
  • 4
  • 27
  • 48

2 Answers2

2

Think of the security ramifications if anyone could connect to your organization's database simply by bootstrapping into JNDI. The deployer of a server application vouches for the identity of an application deployed on the server by mapping the application's resource reference to the data source's JNDI entry. Since no such vetting can be done for arbitrary clients, it is necessary for them to authenticate.

As for your connection, it can't be managed by the server since it didn't come from the server. Your connection instance is running in your client (most likely a different JVM). The server only manages connections for applications running in the server.

pglezen
  • 961
  • 8
  • 18
  • In the regard of connection, if it cannot be managed by the server since it didn't come from the server, what's the meaning here for using JNDI lookup way? To some extent, as for client application, it seems more convenient and better to connect to database directly according to your explanation. Right? Because it has nothing to do with WAS connection pool. – wing2ofsky Sep 03 '12 at 10:21
  • JNDI exists for both the server and clients to use. Clients would typically use JNDI to obtain remote references to EJBs running on the server; not data sources. EJBs and servlets running on the server would, in turn, use JNDI to obtain a data source that returns server-managed database connections. Some clients, such as HTTP clients, don't ever have to bootstrap to a server's JNDI. – pglezen Sep 05 '12 at 03:31
  • Thanks for your reply @pglezen As far as my program provided above is concerned, a java client trying get database connection through JNDI lookup, i just wanna know if that database connection returned has anything to do with the WAS server database connection pool? No matter how many db connections are retrieved through my way, it never occupys anyone from the WAS connection pool. isn't it? – wing2ofsky Sep 05 '12 at 11:13
  • That's correct. The connection object you receive in your client did not come from a WAS connection pool (which only exists on the server). The information for the connection was retrieved from JNDI just like it does on the server. But the connection object is instantiated in your client. WAS will never know about it and can't manage it. This practice is typically avoided because it has the potential to flood the DB with concurrent connections from many clients. If the DB connections are restricted to come from the server, they can be controlled better. – pglezen Sep 07 '12 at 03:57
0

I already explained that in one of my previous answers to one of your questions:

How to get database connection through websphere ORB call?

Community
  • 1
  • 1
Andreas Veithen
  • 8,868
  • 3
  • 25
  • 28