I have been struggling with this problem for days. I am using (Tomcat) connection pool to access database. I am sure that the pool is configured correctly and DataSource is properly referenced in the web.xml. I am positive about it because I have run the CPServlet.java. And I am able to retrieve data from database and print the data out. CPServlet.java:
Problem occurs when I try to split CPServlet.java into a connection bean (DBConnection.java) and pass Connection object to a business class (VerificationServlet.java). I did not get any error message. What I got is a blank page (no datum was written out). I am lost. DBConnection.java
Moreover, I do not see anything in the log messages that points to problems. Please help. [ July 15, 2003: Message edited by: JiaPei Jen ]
In your getDBConnection() method, it looks as if you are opening a new connection and then closing it within the finally block before returning the reference, although this would seem to be null. I think you're getting a NullPointerException when you try conn.createStatement(). Try sending the output to STDOUT instead of STDERR, just in case your servlet container is swallowing this stuff somewhere (perhaps in a log file?).
How should I change the program so that the reference to the Connection object is not "null"? (i.e. Currently, I have Statment stmt = null.createStatement although I do not have the NullPointer Exception error message.) When I have InitialContext, DataSource, Connection, Statement, ResultSet in the same Servlet, I am able to retrieve data from database via the configured connection pool. Please help!
Don't close your connection in the getDBConnection() method. Instead, pass the open connection back to your servlet and let it do its thing. Of Course, don't forget to close your connection in the servlet. While the Garbage Collector will close it upon running, there is no guarantee as to when it gets collected and you don't want the connection sitting open for any longer than you need it.