views:

57

answers:

0

Hi there,

I have been trying to set up a Database Connection Pool for my test webapp just to learn how it's done really. I have managed to get a DataSource object connected to my database which supplies me with Connection objects now, so that's good.

I must admit I don't really know exactly how it's working. I wrote some test code to see if I could figure out how the InitialContext object is working:

package twittersearch.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import twittersearch.model.*;
public class ContextTest extends HttpServlet {

    public void doGet(HttpServletRequest request,
                   HttpServletResponse response) 
       throws IOException, ServletException {
  Context ctx = null;
  Context env = null;
  try {
      ctx = new InitialContext();
            Hashtable<?, ?> h = ctx.getEnvironment();
            Enumeration<?> keyEn = h.keys();
            while(keyEn.hasMoreElements()) {
                Object o = keyEn.nextElement();
                System.out.println(o);
            }
            Enumeration<?> valEn = h.elements();
            while(valEn.hasMoreElements()) {
                Object o = valEn.nextElement();
                System.out.println(o);
            }
   env = (Context)ctx.lookup("java:comp/env");
   h = env.getEnvironment();
   Enumeration<?> keys = h.keys();
   Enumeration<?> values = h.elements();
   System.out.println("Keys:");
   while(keys.hasMoreElements()) {
       System.out.println(keys.nextElement());
   }
   System.out.println("Values:");
   while(values.hasMoreElements()) {
       System.out.println(values.nextElement());
   }
   Collection<?> col = h.values();
   for(Object o : col) {
       System.out.println(o);
   }
   DataSource dataSource = (DataSource)env.lookup("jdbc/twittersearchdb");
   Connection conn = dataSource.getConnection();
   if(conn instanceof Connection) {
       System.out.println("Have a connection from the pool");
   }
  } catch(Exception e) {
      e.printStackTrace();
  }
 }
}

This gives me output of:

java.naming.factory.initial
java.naming.factory.url.pkgs
org.apache.naming.java.javaURLContextFactory
org.apache.naming
Have a connection from the pool
Keys:
Values:
Have a connection from the pool

What I don't understand

  1. I have got the InitialContext object which, as I understand it, I should be able to get a Hashtable from with keys and values of all the bindings for that context. As the first four lines of the output show, there were only two bindings.Yet I am able to use ctx.lookup("java:comp/env") to get another context that has bindings for Resources for my webapp. There was no "java:comp/env" in the keys from the test output from the InitialContext object. Where did that come from?

  2. Also as you can see I tried to printout the keys and values from the java:comp/env context and got no output and yet I am able to use env.lookup("jdbc/twittersearchdb") which gets me the DataSource that I have specified in my context.xml. Why do I have no output for the bindings for the "java:comp/env" context?

  3. Can I just confirm that as I have specified a Resource element in my context.xml, the container is creating a DataSource onject on deployment of the webapp and the whole Context / InitialContext thing is just a way of using JNDI to access the DataSource object? And if that's the case, why is JNDI used when it seems easier to me to create a DataSource in an implementation of ServletContextListener and have the datasource as a ServletContext attribute?

  4. Does the DataSource object actually manage the ConnectionPool or is that the Container and so is the DataSource object just a way of describing the connection?

  5. How do we access the container directly? What is the object that acctually represents the container? Is it ServletContext? I'm just trying to find out what the container can do for me.

Apologies for the length of this post. I really want to clear up these issues because I'm sure all this stuff is used in every webapp so I need to have it sorted.

Many thanks in advance

Joe