You are on page 1of 7

Hint

MySQL integration into Java Web Services with Axis, Tomcat, and Eclipse

Starting point for writing this hint was my stumbling and trying phase when I wanted to use MySQL database access myself for my Java Web Services and could find no hint in all my web services books, only some documentation from Tomcat and help in mailing lists and google search. So, in order to help others at the same point I was in, here we are... All I am describing here might not be the ideal solution, but at least it works on my systems and might be helpful.

Please don't hesitate to contact me when you have any corrections or improvements ! axel.burwitz@arcor.de

The decription was done for the following components base:


Gentoo Linux with Kernel 2.6.7 - gentoo- r11 Sun Java SDK , Linux, sun- jdk- 1.4.2.04 Axis 1.1 Eclipse 3.0.1 Apache WebServer 2.0 Apache Tomcat/5.0.18 MySQL 4.0.20

Eclipse and MySQL JDBC driver

First step is to integrate the standard jdbc mysql driver archive mysql- connector - java- 3.0.11 - stable- bin.jar via Properties of your eclipse project to the Java Build Path as shown here (better you zoom the view ;- ) )

Then save and that's it here.

Apache Tomcat configuration

- MySQL driver Copy the MySQL driver archive mysql - connector - java- 3.0.11 - stable- bin.jar into the Tomcat directory $CATALINA/webapps/axis /WEB- INF/lib, in my case /opt / to mcat /webapps/axis /WEB- INF/lib and add this path and .jar to the CLASSPATH , along as done with the other axis .jar's: CLASSPATH=$CLASSPATH:/opt / tomcat /webapps/axis/WEBINF/lib /axis - ant.jar:/opt / tomcat /webapps/axis /WEBINF/lib /axis.jar:/opt / to mcat /webapps/axis/WEB- INF/lib /commonsdiscovery.jar:/opt / to mcat /webapps/axis/WEB- INF/lib /commonslogging.jar:/opt / t o mcat /webapps/axis /WEBINF/lib /jaxrpc.jar: /opt / to mcat /webapps/axis/WEB- INF/lib /log4j 1.2.8.jar:/opt / to mcat /webapps/axis/WEBINF/lib /saaj.jar:/opt / to mcat /webapps/axis/WEBINF/lib /wsdl4j.jar:/opt / t o mcat /webapps/axis /WEB- INF/lib / xm l apis.jar:/opt / t omcat /webapps/axis/WEBINF/lib / xercesImpl.jar:/opt / t omcat /webapps/axis /WEB- INF/lib / mysql connector - java- 3.0.11 - stable- bin.jar

Adapting server.xml -

Insert into the $CATALINA/conf /server.xml (see also the Tomcat documentation) these lines and adapt to your situation: <Context path=" /axis " docBase=" axis" debug="5" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix ="localhost_DBTest_log." suffix =".txt" timestamp="true"/ > <Resource name="jdbc/ axis" auth="Container" type="javax.sql.DataSource"/ > <ResourceParams name="jdbc/ axis"> <parameter> <name>factory< / name > <value>org.apache.commons.dbcp.BasicDataSourceFactory< /value> < / parameter> <! - - Maximum number of dB connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to 0 for no limit. --> <parameter> <name>maxActive< / name > <value>100 < / value> < / parameter> <! - - Maximum number of idle dB connections to retain in pool. Set to - 1 for no limit. See also the DBCP documentation on this and the minEvictableIdleTimeMillis configuration parameter. --> <parameter> <name>maxIdle< / name > <value>30 < / value > < / parameter> <! - - Maximum time to wait for a dB connection to become available

in ms, in this example 10 seconds. An Exception is thrown if this timeout is exceeded. Set to - 1 to wait indefinitely. --> <parameter> <name>maxWait< / name > <value>10000 < / value > < / parameter> <! - - MySQL dB username and password for dB connections - - > <parameter> <name>username< / name > <value> mydatabaseuser < /value> < / parameter> <parameter> <name>password< / name > <value> mydatabaseuserpassword < /value> < / parameter> <! - - Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next if you want to use this driver - we recommend using Connector/J though <parameter> <name>driverClassName< / name > <value>org.gjt.mm.mysql.Driver< / value> < /parameter> --> <! - - Class name for the official MySQL Connector /J driver - - > <parameter> <name>driverClassName< / name> <value>com.mysql.jdbc.Driver< /value> < / parameter> <! - - The JDBC connection url for connecting to your MySQL dB. The autoReconnect=true argument to the url makes sure that the mm.mysql JDBC Driver will automatically reconnect if mysqld closed the connection. mysqld by default closes idle connections after 8 hours. --> <parameter> <name>url < / name > <value>jdbc:mysql:/ / l ocalhost:3306 / dvdstore2sql ?autoReconnect=true< /value>

< / parameter> < /ResourceParams> < /Context > just in front before the < /Host > line. I have coloured the values you should adapt to your situation.

- Adapting web.xml Insert the following web- app description into the web.xml in your web application path, in most cases will be $CATALINA/webapps/axis/WEB- INF/web.xml

<web - app xmlns="http: / / java.sun.com /x ml / ns / j 2ee" xmlns:xsi="http: / / w ww.w3.org /2001 /XMLSchema- instance" xsi:schemaLocation="http: / / java.sun.com /xml / ns / j 2ee http:/ / java.sun.com /x ml / ns / j 2ee/web - app_2_4.xsd" version="2.4"> <description> DVDStore2SQL< /description > <resource- ref> <description >DB Connection< / description > <res- ref- name>jdbc / axis< / res - ref- name> <res- type>javax.sql.DataSource< / res - type> <res- auth>Container< / res - auth> < /resource- ref> < /web - app>

I have coloured the values you should adapt to your situation. Don't forget to restart Tomcat.

Things that made me stumble

watch out the correct spelling and names of your application , in my case dvdstore2sql , see above, in server.xml and web.xml, and that it is exact as in the .wsdd file watch out that the database user and password in server.xml and your program statement match.. when you have the SQL- call itself in an own separate class that is called by the web service class, anyway deploy the web service class wsdd every time again when you change anything in the separate SQLcall class... (but don't ask me why)

HTH and regards, Axel Burwitz

You might also like