Wrox Home  
Search P2P Archive for: Go

  Return to Index  

apache_tomcat thread: Problems getting a DataSource to talk from my Tomcat app to SQL Server 2000


Message #1 by "Bradley Ward" <bward@r...> on Mon, 25 Nov 2002 23:26:37
I bought the new Apache Tomcat book from Wrox Press in hopes of solving 
this problem. So far, no luck. Perhaps someone here can help.

I am trying to connect to a SQL Server 2000 database from within a servlet 
running under Tomcat 4.1. To the best of my knowledge, I've put the proper 
statements into the server.xml file and the web.xml file. But when I try 
to get a DataSource, I get a null value. I am trying two different 
approaches, both of which are recommended in various resources I have 
found in my (extensive) searching for a solution to this problem.

My code looks like this:

------------------
Context ctx = new InitialContext();
Context env = (Context)ctx.lookup("java:comp/env");
DataSource ds = (DataSource) env.lookup("jdbc/TestDB");

System.out.println("ds is " + (ds == null ? "null" : " NOT null"));

if (ds == null) {
  ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
  System.out.println("ds is " + (ds == null ? "null" : " NOT null"));
}

m_Connection = ds.getConnection();

-------------------

Back before I got the server.xml and web.xml files configured correctly, I 
would get an exception with a message about a context by that name not 
being found. Now I don't get an exception, but I also don't get a 
DataSource.

I've used another application server that had a very simple and 
straightforward way of configuring DataSources; Tomcat's seems rather 
convuluted and error prone. Is there some powerful feature that Tomcat 
provides that I'm overlooking?

Here is the entry from my server.xml file:

<Resource name="jdbc/TestDB" auth="container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/TestDB">
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://brad:1433</value>
</parameter>
<parameter>
<name>username</name>
<value>TestDB</value>
</parameter>
<parameter>
<name>password</name>
<value>password</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>foo.bar.Driver</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
</ResourceParams>


and here is the entry from my web.xml file:

<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


I would really appreciate any hints you might have, or even ideas of 
things to try! It's not like I'm trying to do something totally obscure 
here! <G>

  Return to Index