connection pooling: where have I gone wrong?

Hi all, I really can't see where I've gone wrong setting up connection pooling. Can anyone show me what I need to do to fix this?

I am getting a NullPointerException at this line:

statement = connection.createStatement();

in web.xml:

<listener>

<listener-class>DBCPoolingListener</listener-class>

</listener>

<!-- This component has a dependency on an external resource-->

<resource-ref>

<description>DB Connection Pooling</description>

<res-ref-name>jdbc/TestDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

<servlet>

<servlet-name>Login</servlet-name>

<servlet-class>Login</servlet-class>

</servlet>

in server.xml:

<Context path="/testconn" docBase="testconn" debug="5"

reloadable="true" crossContext="true">

<Resource name="jdbc/TestDB" auth="Container"

type="javax.sql.DataSource" removeAbandoned="true"

removeAbandonedTimeout="30" maxActive="100"

maxIdle="30" maxWait="10000" username="username"

password="password"

driverClassName="oracle.jdbc.driver.OracleDriver"

url="WorkingDbUrl"/>

</Context>

<!-- Configuring the request and response endpoints -->

<Connector port="80" maxHttpHeaderSize="8192"maxProcessors="150"

maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="150"

connectionTimeout="20000" disableUploadTimeout="true" />

code that uses the db connection:

import java.sql.*;

import java.util.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

publicclass Loginextends HttpServlet{

protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

String sql="SELECT * FROM users";

Connection connection = DBCPoolingListener.getConnection();

Statement statement =null;

try{

statement = connection.createStatement();

// more code

code that creates the connection:

import javax.servlet.*;

import javax.servlet.http.*;

import javax.sql.DataSource;

import java.sql.*;

import javax.naming.*;

publicclass DBCPoolingListenerimplements ServletContextListener{

InitialContext context =null;

Context envContext =null;

DataSource ds =null;

static Connection connection;

publicvoid contextInitialized(ServletContextEvent sce){

try{

context =new InitialContext();

envContext = (Context) context.lookup("java:comp/env");

ds = (DataSource) envContext.lookup("jdbc/TestDB");

connection = ds.getConnection();

}catch(NamingException ne){

// log the naming exception

}catch(SQLException sqle){

// handle the SLQ exception (log)

}

}

publicstatic Connection getConnection(){

return connection;

}

publicvoid contextDestroyed(ServletContextEvent sce){

}

}

[5113 byte] By [MajorMahema] at [2007-11-26 22:42:58]
# 1
are you sure your contextInitialized is working? you seem to silently swallow exceptions there. also, you've mixed instance and static methods with some abandon in that class, not that that would lead to this
georgemca at 2007-7-10 11:58:56 > top of Java-index,Java Essentials,New To Java...
# 2

> }catch(NamingException ne){

> // log the naming exception

> }catch(SQLException sqle){

> // handle the SLQ exception (log)

> }

Since you didn't actually log / handle the exceptions, you silently ignored them. I suspect an exception was thrown, so the 'connection' member remained null, leading to the NullPointerException later on.

You're shooting yourself in the foot by swallowing those exceptions, do you realize? They would have at least given information about what the problem was. Currently without them nobody has any idea.

warnerjaa at 2007-7-10 11:58:56 > top of Java-index,Java Essentials,New To Java...
# 3

Hi there, thank you for your reply.

the contextInitialised code does run, when I restarted Tomcat I got:

Error: Cannot create JDBC driver of class '' for connect URL 'null'

>You're shooting yourself in the foot by swallowing those exceptions, >do you realize?

I do now.

I added in:

}catch(NamingException ne){

System.out.println("Error: "+ne.getMessage());

}catch(SQLException sqle){

System.out.println("Error: "+sqle.getMessage());

}

no exception was thrown at this point.

Any ideas?

Cheers,

MM

MajorMahema at 2007-7-10 11:58:56 > top of Java-index,Java Essentials,New To Java...
# 4

try this

...

connection = ds.getConnection();

// add the following

if ( null == connection ) {

throw new RuntimeException("no connection");

}

...

temporarily, to make sure a connection is actually being returned

georgemca at 2007-7-10 11:58:56 > top of Java-index,Java Essentials,New To Java...
# 5

I don't know where the exception (the error message you last showed) is occurring. You're now at least showing the exception message, but it's not quite enough.

> System.out.println("Error: "+sqle.getMessage());

Here you've lost another valuable part of the exception: the stack trace showing where it happened.

Do this instead (and likewise for the other place where you're doing this):

sqle.printStackTrace();

However, I suspect the issue is in where you've configured your JDBC connection pool(s) in your app or app server's config file. Sounds like you didn't set up the JDBC URL, class name, etc correctly.

> Error: Cannot create JDBC driver of class '' <- no class name?

> for connect URL 'null' < no connection URL?

warnerjaa at 2007-7-10 11:58:56 > top of Java-index,Java Essentials,New To Java...
# 6

Hi there, thanks for that, I added that in to the listener class and no error was thrown, I added that in to the servlet too and I got:

java.lang.RuntimeException: no connection

Which is what you would expect, does this mean its the static method / variable that is causing this?

Cheers,

MM

MajorMahema at 2007-7-10 11:58:56 > top of Java-index,Java Essentials,New To Java...
# 7

> Hi there, thanks for that, I added that in to the

> listener class and no error was thrown, I added that

> in to the servlet too and I got:

>

> java.lang.RuntimeException: no connection

>

> Which is what you would expect, does this mean its

> the static method / variable that is causing this?

>

>

> Cheers,

>

> MM

nah, it means you haven't configured something properly. quite what is going to be a bit of a bind to work out without actually being sat beside you, though

georgemca at 2007-7-10 11:58:57 > top of Java-index,Java Essentials,New To Java...
# 8

> However, I suspect the issue is in where you've

> configured your JDBC connection pool(s) in your app

> or app server's config file. Sounds like you didn't

> set up the JDBC URL, class name, etc correctly.

> Error: Cannot create JDBC driver of class '' <-

> no class name?

> for connect URL 'null' < no connection URL?

I think you could be right, I've never tried setting up connection pooling before so I really don't know.

I originally quoted what I put in the server.xml file all of which was put within the <host> xml tag. Could there be something wrong with that?

Cheers,

MM

MajorMahema at 2007-7-10 11:58:57 > top of Java-index,Java Essentials,New To Java...
# 9
> nah, it means you haven't configured something> properly. quite what is going to be a bit of a bind> to work out without actually being sat beside you,> though :'( guess I'll need to ramp up the number of duke points on this then ;)
MajorMahema at 2007-7-10 11:58:57 > top of Java-index,Java Essentials,New To Java...
# 10

> > nah, it means you haven't configured something

> > properly. quite what is going to be a bit of a

> bind

> > to work out without actually being sat beside you,

> > though

>

>

> :'( guess I'll need to ramp up the number of duke

> points on this then ;)

there's dukes?

georgemca at 2007-7-10 11:58:57 > top of Java-index,Java Essentials,New To Java...
# 11

> :'( guess I'll need to ramp up the number of duke

> points on this then ;)

We're not really in it for the dukes (well, there are a few exceptions who don't seem to know dukes are worthless other than making one's star turn into a different color).

I just gave up because at this point the problem is with configuring JDBC connection pools within your specific J2EE app server. I wouldn't know the right configuration without knowing your app server, and then doing a ton of searching to learn how to configure it.

I just hope I helped a teensie bit by trying to point you in the right direction -- that it's the connection pool configuration you need to concentrate on.

warnerjaa at 2007-7-10 11:58:57 > top of Java-index,Java Essentials,New To Java...
# 12
Time to call in Nerds On Site -- you know, the guys in the VW beetles?
DrLaszloJamfa at 2007-7-10 11:58:57 > top of Java-index,Java Essentials,New To Java...
# 13

I appreciate your (and everyone elses) efforts to help me out with this. I think I might need to ask someone I know for advice on this.

I'm using Tomcat 5.0 and I've tried following this tutorial to get this working:

http://www.onjava.com/pub/a/onjava/2006/04/19/database-connection-pooling-with-tomcat.html

Again, thank you for your help.

MM :)

MajorMahema at 2007-7-10 11:58:57 > top of Java-index,Java Essentials,New To Java...