I (and countless others) have run into this same problem.  I tried with
the MS SQL 2000 Beta Driver.  It seems to me that either due to
incorrect configuration or bugs in the driver, the connection cannot be
created and the end result is null when you go to ask for a connection.
However this is just a guess, since I could not find any kind of logging
or diagnostic output.

The current pool implementation in Tomcat is Tyrex.  I *suspect* that is
where the problem lies, it either doesn't understand that it isn't
creating connections properly, or it just isn't logging it's errors or
failure to connect anywhere (even worse).  I couldn't find anything
beyond Javadoc API for the Tyrex stuff.

So, my solution was to create my own factory until the Container,
Connection Pool, and Driver can work their respective *#!@ out.  While
this doesn't get me connection pooling, it does allow me to proceed with
development and when this stuff does get fixed, I won't have to change
my code.  

Tyrex is going away in favor of DBCP, so I will probably revisit this
issue when that shows up in the Tomcat release.  If you have a JDBC
driver that has it's own connection pooling support, you could easily
enhance my code to add the appropriate parameters.

(Yes, I know this is a work-around, but I have wasted a ***lot*** of
time trying to get this to work and finally gave up and did this, so at
least my code won't have to change when the 'true' connection pooling
stuff gets fixed, and given the amount of time and research I have put
into this, it is broken in some respect.)

Here is the code, server.xml and web.xml:


import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;

import java.sql.*;
import javax.sql.*;
import com.microsoft.jdbcx.sqlserver.*;

public class DataSourceFactory implements ObjectFactory
{
    private DataSource ds;
    private String serverName;
    private String databaseName;
    private String password;
    private String user;
    private int portNumber;
    private String description;
    
    public Object getObjectInstance(Object obj,Name name, Context
nameCtx, Hashtable environment)
        throws NamingException
    {
        // check to see if the DataSource has been initialized yet
        if(ds == null)
        {
            // initialize the DataSource
            initParams((Reference)obj);
            initDataSource();
        }
                
        return ds;
    }

    /**
     * Retrieves the parameters set in server.xml which will be used
     * to intitalize the properties of the DataSource.  Looks for the 
     * parameters named: <br>
     * 
     * <ul>
     *      <li>serverName</li>
     *      <li>databaseName</li>
     *      <li>user</li>
     *      <li>password</li>
     *      <li>portNumber</li>
     *      <li>description</li>
     * </ul>
     */
         
    void initParams(Reference ref)
    {
        Enumeration parameters = ref.getAll();
        
        while(parameters.hasMoreElements())
        {
            RefAddr addr = (RefAddr)parameters.nextElement();
            String name = addr.getType();
            
            if(name.equals("serverName"))
            {
                serverName = (String)addr.getContent();
            }
            else if(name.equals("databaseName"))
            {
                databaseName = (String)addr.getContent();
            }
            else if(name.equals("user"))
            {
                user = (String)addr.getContent();
            }
            else if(name.equals("password"))
            {
                password = (String)addr.getContent();
            }
            else if(name.equals("portNumber"))
            {
                portNumber =
Integer.parseInt((String)addr.getContent());
            }
            else if(name.equals("description"))
            {
                description = (String)addr.getContent();
            }
        }
    }
    
    /**
     * Sets the properties of the SQLServerDataSource using the
parameters
     * from server.xml.
     */
     
    void initDataSource()
    {
                SQLServerDataSource ds = null;
                
                try
                {
                        ds = new SQLServerDataSource();

                        ds.setServerName(serverName);
                        ds.setDatabaseName(databaseName);
                        ds.setPassword(password);
                        ds.setUser(user);
                        ds.setPortNumber(portNumber);
                        ds.setDescription(description);     
        }
                catch(Exception e)
                {
                        System.out.println("DataSource intitialization
failed.");
                        System.out.println(e);
                }
    }
}

----------
server.xml
----------
<Resource name="db/ord_unos" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="db/ord_unos">
        <parameter>
                <name>factory</name>
              <value>net.kanati.db.DataSourceFactory</value>
        </parameter>
        <parameter>
                <name>serverName</name>
                <value>localhost</value>
        </parameter>
        <parameter>
                <name>databaseName</name>
                <value>ord_unos</value>
        </parameter>
                <parameter>
                <name>user</name>
                <value>sa</value>
        </parameter>
        <parameter>
              <name>password</name>
              <value>hom35weet</value>
        </parameter>
        <parameter>
              <name>portNumber</name>
              <value>2142</value>
        </parameter>
        <parameter>
                <name>description</name>
              <value>ORD UNOS Data</value>
        </parameter>
</ResourceParams>

-------
web.xml
-------
<resource-env-ref>
        <description>Database Connection to ORD UNOS</description>
        <resource-env-ref-name>
            db/ord_unos
        </resource-env-ref-name>
        <resource-env-ref-type>
            javax.sql.DataSource
        </resource-env-ref-type>
 </resource-env-ref>

______________________________________________
Brad Schneider           Kanati, LLC
[EMAIL PROTECTED]          2140 Commerce St
Direct 214-215-5459      Dallas TX 75201-4306
Fax 214-540-0280         www.kanati.net



-----Original Message-----
From: Nikola Milutinovic [mailto:[EMAIL PROTECTED]] 
Sent: Monday, March 18, 2002 2:22 PM
To: Tomcat Users List
Subject: Re: JDBC/JNDI problem, continued - PUZZLED

Nikola Milutinovic wrote:

> Hi all.
> 
> PostgreSQL 7.2
> PostgreSQL JDBC 7.2
> Tomcat 4.0.1
> 
> Problem: When I try to lookup a resource regularely (I think) defined,
I 
> get "null". The JDBC resource is defined as per Tomcat docs and
general 
> advice on this list.
> 
> I have made some test, I can lookup the following:
> 
> java:comp
> java:comp/env
> java:comp/env/jdbc
> 
> But when I try "java:jdbc/env/jdbc/AddressBookDB" I get "null". The 
> resource in question should be OK.
> 
> Is there a way to see what goes on in Tomcat's JNDI tree? Some sort of

> JNDI browser?

So, I decided to write my own JNDI browser, after all, I have Java, JSP
and JNDI 
in Tomcat. :-)

Relatively simple code:

<p>Context looked up: <strong><%= contextPath %></strong></p>
<%
InitialContext initCtx;
Context envCtx;

initCtx = new InitialContext();
NamingEnumeration enum = initCtx.listBindings( contextPath );
int i = 1;
%>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<%
   while( enum.hasMore() ) {
%>
   <tr>
     <td><%= i %></td>
     <td><%= ((Binding)enum.next()).toString() %></td>
   </tr>
<%
   }
%>
</table>

This is what I get when I set context to "java:comp/env/jdbc":

----------------------------------------------
Context looked up: java:comp/env/jdbc

1 AddressBookDB: org.apache.naming.ResourceRef:Reference Class Name: 
javax.sql.DataSource Type: scope Content: Shareable Type: auth Content:
Container
----------------------------------------------

So, it is there. Why does "lookup()" on that particular resource return
"null"?

Nix.


--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>


--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

Reply via email to