context.xml and DataSource

A

Aljosa Mohorovic

i'm getting "Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'"
error but this code works:
working code:String url = "jdbc:db2://localhost:50000/test";
String user = "user";
String password = "pass";
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection conn = DriverManager.getConnection(url, user, password);
<<<

context.xml:<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Test">
<Resource name="test"
auth="Container"
type="javax.sql.DataSource"
username="user"
password="pass"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://localhost:50000/test"
/>
</Context>
<<<

code not working:InitialContext ic = new InitialContext();
Context c = (Context)ic.lookup("java:comp/env");

try {
DataSource ds = (DataSource)c.lookup("test");
Connection connection = ds.getConnection();
} catch(Exception e) {
// log e.getMessage();
}
<<<
 
J

Juha Laiho

Aljosa Mohorovic said:
i'm getting "Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'"
error but this code works:
working code:
String url = "jdbc:db2://localhost:50000/test";
String user = "user";
String password = "pass";
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection conn = DriverManager.getConnection(url, user, password);
<<<

.... and you have the IBM JDBC driver library in WEB-INF/lib, I suppose?
If so, this all makes sense.

The code above, when being part of the web-application, will have the
jar files in WEB-INF/lib as part of its classpath.
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Test">
<Resource name="test"
auth="Container"
type="javax.sql.DataSource"
username="user"
password="pass"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://localhost:50000/test"
/>
</Context>
<<<

However, when you specify the DB connection in context.xml (or server.xml),
the classpath used to look for the JDBC library is a combination of
server classpath and common classpath. Application-specific classes
(those within WEB-INF/lib and WEB-INF/classes) are not seen by this
classloader.
code not working:
InitialContext ic = new InitialContext();
Context c = (Context)ic.lookup("java:comp/env");

try {
DataSource ds = (DataSource)c.lookup("test");
Connection connection = ds.getConnection();
} catch(Exception e) {
// log e.getMessage();
}
<<<

The code as such seems correct.

Relocate (really _relocate_; don't leave a copy within WEB-INF/lib) the
JDBC driver to somewhere where the common classloader will pick it up.
That way it'll be visible to both the server and the application
(should the application need some IBM-specific interfaces of the
library).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top