tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Unable to connect to DB using DataSource
Date Thu, 04 Dec 2008 22:00:38 GMT
Hash: SHA1


I'm no expert with Axis WebService, but I do have a question about your
use of the javax.sql.DataSource object:

Stefan Deitmer wrote:
> This is how I access the DataSource in the Axis WebService's LifeCycle
> object startup routine:
>    public void startUp(ConfigurationContext configctx, AxisService
> service) {


>            service.addParameter(new Parameter(Constants.DATASOURCE, ds));

Is it customary to push a DataSource into the service's "parameters"?
Since the DataSource is always available via JNDI, why not simply grab
it from the JNDI context whenever you need it?

Going through JNDI has the added benefit of always getting you the right
object. If you were to reconfigure (or replace, really) the DataSource
in the JNDI context, your updates wouldn't be available to the web service.

>    SQLException while trying to get connection : Cannot load JDBC driver
> class 'org.postgresql.Driver'

I suspect you are just missing the PostgreSQL driver jar file. Can you
find it in any of the (hopefully) obvious places? I would start by
looking in /usr/share/tomcat5.5 and its subdirs.

> Replacing above code with the following works without errors:
>        try
>        {
>            Class.forName("org.postgresql.Driver");
>            conn =
> DriverManager.getConnection("jdbc:postgresql://localhost/database",
> "user", "password");
>        }catch (Exception e) {
>            LOG.error("SQLException while trying to get connection: "
>                    + e.getMessage());
>        }

Sounds like the driver JAR file is packaged with your webapp (that is,
in yourapp/WEB-INF/lib/postgresql.jar). If you want to use Tomcat's
connection pooling (and you probably /do/), then you need to MOVE that
JAR file from your webapp's lib directory into Tomcat's libraries
directory. For Tomcat 5.5, you should probably put it into common/lib
under your Tomcat's real installation directory (maybe

> This is my first project with Tomcat, Axis, JDBC and PostgreSQL, and
> also the first time I'm seriously using Java in a very long time,
> so please have some patience with me if I'm missing something essential
> here.

You provided a perfect amount of information, actually. Thanks for
providing all the info you did!

> How comes Class.forName works when I call
> it directly, but not when the DataSource class calls it internally?

When using the DataSource, you are attempting to use Tomcat's connection
pool. Tomcat needs to be able to load the driver from a JAR file
/before/ consulting your webapp's libraries. When you use the
DriverManager, you are bypassing all that and loading the driver
"normally" through your webapp's classloader.

- -chris
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message