Hello list.
I am trying to use a JDBC DataSource to connect to a PostgreSQL server
from within an Axis WebService running in Tomcat.
What confuses me is that I managed to get this to work on one server,
but now am failing miserably to do so on a second server.
The first server is a Debian Lenny with Tomcat 5.5 and Axis 1.4.1,
running in a Xen DomU on a 64 bit AMD machine.
The second server is also running Lenny with the same Tomcat and Axis
versions, but in a VirtualBox VM on a 32 bit Intel machine.
I have compared the whole directory tree of /var/lib/tomcat5.5, also
/usr/share/java, /etc/default/tomcat5.5 and the startup scripts in
/etc/init.d/tomcat5.5.
Everything is identical between the two servers, and still, #1 works, #2
doesn't.
The error message tells me that it can't find the postgres JDBC driver.
However, there is a symbolic link in /var/lib/tomcat5.5/share/lib
pointing to the driver in /usr/share/java. Also, I tried to open a
connection "directly", that is, using DriverManager.getConnection(),
it works, so it seems to be able to find the driver.
This is the configuration of the DataSource (in
/var/lib/tomcat5.5/conf/server.xml):
<Context path="/axis2" reloadable="true" cachingAllowed="false">
<Resource name="jdbc/pigaiadb" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="10" maxWait="1000"
removeAbandoned="true" removeAbandonedTimeout="60"
username="user" password="password"
driverClassName="org.postgresql.Driver"
validationQuery="select 1"
url="jdbc:postgresql://localhost/database" />
</Context>
This is how I access the DataSource in the Axis WebService's LifeCycle
object startup routine:
public void startUp(ConfigurationContext configctx, AxisService
service) {
Context initCtx = null;
Context envCtx = null;
DataSource ds = null;
try {
initCtx = new InitialContext();
} catch (NamingException e) {
LOG.error("InitContext Exception : " + e.getMessage());
}
try {
envCtx = (Context) initCtx.lookup("java:comp/env");
} catch (NamingException e) {
LOG.error("envContext Exception : " + e.getMessage());
}
try {
ds = (DataSource) envCtx.lookup("jdbc/pigaiadb");
} catch (NamingException e) {
LOG.error("DataSource Exception : " + e.getMessage());
}
try {
service.addParameter(new Parameter(Constants.DATASOURCE, ds));
} catch (AxisFault e) {
LOG.error("Failed to add datasource parameter to context: "
+ e.getMessage());
}
}
And this is how I try to get the connection from the DataSource:
try {
conn = ((DataSource)
serviceContext.getAxisService().getParameterValue(Constants.DATASOURCE)).getConnection();
} catch (SQLException e) {
LOG.error("SQLException while trying to get connection : "
+ e.getMessage());
}
... which results in the following error message:
SQLException while trying to get connection : Cannot load JDBC
driver class 'org.postgresql.Driver'
and this stack trace:
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at
org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130)
at
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
...
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());
}
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. I would greatly appreciate some pointers as to how I
could nail down this problem. How comes Class.forName works when I call
it directly, but not when the DataSource class calls it internally?
Thanks a lot in advance
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]