You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Robert Bateman <bo...@sequoiallc.com> on 2003/06/19 23:57:53 UTC

Greatly confused.

I'll apologize up front as I'm new with Tomcat and dbcp...

I've begun working on a JSP based web site.  And as with all things on web 
sites, I need to access dynamic data.  Following the directions for adding a 
data source to Tomcat (and following the JNDI example HOWTO from the web 
site) - I created a test JSP page.

My code pretty much mimics what the HOWTO recommends, except I'm using the new 
MySQL driver and my data base name is a bit different.

My problem is, I appear *not* to be getting a valid BasicDataSource from the 
server.xml document.  To the best my eyes can detect, I have everything 
correct.  But all of the parameters for the data source java/MySQL_TestDB are 
default or null at program access time.

server.xml contains:
----------------------
    <Resource auth="Container" 
    name="jdbc/MySQL_TestDB" 
    scope="Shareable" 
    type="javax.sql.DataSource"/>

    <ResourceParams name="jdbc/MySQL_TestDB">
      <parameter>
        <name>factory</name>
        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
      </parameter>
      <parameter>
        <name>maxActive</name>
        <value>6</value>
      </parameter>
      <parameter>
        <name>maxWait</name>
        <value>5000</value>
      </parameter>
      <parameter>
        <name>password</name>
        <value>javadude</value>
      </parameter>
      <parameter>
        <name>url</name>
        <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
      </parameter>
      <parameter>
        <name>driverClassName</name>
        <value>com.mysql.jdbc.Driver</value>
      </parameter>
      <parameter>
        <name>maxIdle</name>
        <value>1</value>
      </parameter>
      <parameter>
        <name>username</name>
        <value>javauser</value>
      </parameter>
    </ResourceParams>

My DBTest.java init() modules starts with:
---------------------------------------------
  public void init()
  {
    try
	{
	  System.out.println( "### Calling InitialContext()" );
	  Context ctx = new InitialContext();
	  if( ctx == null )
	    throw new Exception( "Boom - No Context" );
	  System.out.println( "### We *have* a context." );

            System.out.println( "### Doing lookup" );
            Context envCtx = (Context) ctx.lookup("java:/comp/env/jdbc/");
            System.out.println("### list() on /comp/env/jdbc Context : ");
            NamingEnumeration enum = ctx.list("java:/comp/env/jdbc/");
            while (enum.hasMoreElements()) {
                System.out.print("### Binding : ");
                System.out.println(enum.nextElement().toString());
            }
            System.out.println("### listBindings() on /comp/env/jdbc Context : 
");
            enum = ctx.listBindings("java:/comp/env/jdbc/");
            while (enum.hasMoreElements()) {
                System.out.print("### Binding : ");
                System.out.println(enum.nextElement().toString());
            }

      System.out.println( "### Looking up our data source." );
      DataSource ds =
	    (DataSource)ctx.lookup( "java:/comp/env/jdbc/MySQL_TestDB" );
	  BasicDataSource bds =
	     (BasicDataSource)ctx.lookup( "java:/comp/env/jdbc/MySQL_TestDB" );
	  if( bds != null )
	  {
        System.out.println( "--- DefaultAutoCommit = " + 
bds.getDefaultAutoCommit() );
        System.out.println( "--- DefaultReadOnly = " + 
bds.getDefaultReadOnly() );
        System.out.println( "--- DriverClassName = " + 
bds.getDriverClassName() );
        System.out.println( "--- MaxActive = " + bds.getMaxActive() );
        System.out.println( "--- MaxIdle = " + bds.getMaxIdle() );
        System.out.println( "--- MaxWait = " + bds.getMaxWait() );
        System.out.println( "--- NumActive = " + bds.getNumActive() );
        System.out.println( "--- NumIdle = " + bds.getNumIdle() );
        System.out.println( "--- Password = " + bds.getPassword() );
        System.out.println( "--- Url = " + bds.getUrl() );
        System.out.println( "--- Username = " + bds.getUsername() );
        System.out.println( "--- ValidationQuery = " + 
bds.getValidationQuery() );
	  }


And the console displays this at execution:
-----------------------------------------------
Creation of foo.DBTest is complete.
### Calling InitialContext()
### We *have* a context.
### Doing lookup
### list() on /comp/env/jdbc Context :
### Binding : MySQL_TestDB: org.apache.naming.ResourceRef
### listBindings() on /comp/env/jdbc Context :
### Binding : MySQL_TestDB: 
org.apache.naming.ResourceRef:ResourceRef[className=
javax.sql.DataSource,factoryClassLocation=null,factoryClassName=org.apache.namin
g.factory.ResourceFactory,{type=description,content=MySQL DB 
Connection},{type=s
cope,content=Shareable},{type=auth,content=Container}]
### Looking up our data source.
--- DefaultAutoCommit = true
--- DefaultReadOnly = false
--- DriverClassName = null
--- MaxActive = 8
--- MaxIdle = 8
--- MaxWait = -1
--- NumActive = 0
--- NumIdle = 0
--- Password = null
--- Url = null
--- Username = null
--- ValidationQuery = null


What confuses me is *why* my DataSource does not appear to be initialized.  
Seeing the information relating to the factory, I reloaded my test page a 
second time.  On that run I got a different binding (looks like a valid 
address this time...) but I still get an "null" connection.

I could understand if my DataBase was not up and that caused an SQLException - 
but this particular message - based on the HOWTO code, confuses me 
thoroughly.


Thanks in advance for your time!

Bob

-- 
Bob Bateman
Sequoia Group LLC
bobbateman@sequoiallc.com