Creating a Tomcat DataSource using SQL Server 2000
This was something that I needed to do for a project at work and was definitely unpleasant.
To set up a development environment for Tomcat with SQL Server 2000 as the database of choice and I also wanted to use a DataSource
object.
It improves performance and is easy to obtain using JNDI, but the set up and development process are less than straightforward. Doing so with Microsoft's SQL Server 2000 is
even less pleasant than normal due to the company's disengagement from Java. That means support is sketchy and although the documentation to Microsoft's own
JDBC drivers is not bad, they are not too helpful when getting going. Eventually I decided to skip their JDBC drivers altogether in favor for an open source alternative. This
is an outline of my findings in 36 hours or so of exploration.
I would not have been able to provide this page if it were not for the kind guidance and wisdom of John Norman. Thanks John!
Java
Ant
bin
directory to the system's path.
Finally, create an environment variable ANT_HOME that will point to Ant's directory.
Tomcat
jTDS JDBC Driver
Connection
object the usual way - using the DriverManager
object.
Ant Contrib Tasks
ant-contrib
jar file to $ANT_HOME/lib directory. That will teach Ant a bunch of new tricks.
Ant Catalina Tasks
$CATALINA_HOME/server/lib
you will find a file called catalina-ant.jar
(version name may be appended). Copy that file to Ant's lib
directory.
Deployment
servlet.jar
, the URL for the Tomcat server
and its administrative passwords.
config
- where I put web.xml
and context.xml
which are discussed below.src
java
- includes the source filesweb
- where I put the JSP files and static web contentclasses
- the destination folder for compiled filesdist
- the destination for the output of the build process, the jar and war files for the application
build.xml
has several important tasks, among them compile, deploy and undeploy. Examine them to make sure that all the values are properly set. If not, Ant will report the failures to you.
war
file once you created it to Tomcat's webapps
directory, it may not always pick up on that fact. A much better way is to use Tomcat's Manager application
(available at http://localhost:8080/manager/html which allows you to upload, reload and remove applications both manually and like we would, using Ant.
deploy
task will overwrite Tomcat's server.xml
. It does back the file up when it overwrites it, but you should be aware that it will overwrite
possibly important settings (like futile attempts to set a Context
or other JNDI settings). The alternative it offers is much nicer: you can create the same Context
block like the one you would have
put in server.xml
in its own special file - context.xml
(this became a feature of Tomcat 5). That way your application is better coupled with its own context information.
web.xml
which is to be placed inside the WEB-INF
directory of the war file, context.xml
has to be placed inside the META-INF
directory of the war file.
If the file will not be copied into that folder, the DataSource
will not work.
context.xml
and web.xml
? Look at the examples below.
DataSource
using MySQL.context.xml
and web.xml
respectively.
For comments and horror stories, feel free to e-mail me at [yuval at enavigo.com]. Last Update: July 21, 2004