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:
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?
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