commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dawid Van Der Merwe <Daw...@discovery.co.za>
Subject RE: <new dbcp user> Greatly confused.
Date Fri, 20 Jun 2003 06:54:39 GMT
Hi Rob.

First make sure you've got the correct jar files in your class path and that
you can connect to the mySql db in a standard way from a jsp page.

Then try removing the

      <parameter>
        <name>factory</name>
        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
      </parameter

attribute from your configuration. It does not appear to be necessary for
obtaining a javax.sql.DataSource.

The cast to BasicDataSource is unnecessary.

Did you add a 

	<resource-ref>
	  <description>Resource for DB connections</description>
	  <res-ref-name>jdbc/JNDI_NAME</res-ref-name>
	  <res-type>javax.sql.DataSource</res-type>
	  <res-auth>Container</res-auth>
	</resource-ref>

section to your web.xml file?

It will not work without it.


Also make sure the dhcp and pool jar files are in the common/lib folder.

Some distributions of tomcat like the embedded version shipping with JBoss
do not have them.




-----Original Message-----
From: Robert Bateman [mailto:bobbateman@sequoiallc.com] 
Sent: 19 Thursday June 2003 11:58
To: commons-user@jakarta.apache.org
Subject: <new dbcp user> 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.n
amin
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


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org

Mime
View raw message