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?).
Yes, I would have to agree. It appears you are setting the connection reference to null in your finally clause. You should be receiving a null pointer exception. Craig
Joined: Nov 19, 2000
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.
Joined: Mar 19, 2002
Right. The one thing you don't wont to do is close the connection and then set it to null before you send it back to the calling program. That is what is happening everytime you get a good connection.
Closing of the connection should be handled elsewhere.
Joined: Nov 19, 2000
Thanks a lot for your attention and help. I took out: conn.close(); and conn = null; in the DBConnection.java. My program works fine now. [ July 16, 2003: Message edited by: JiaPei Jen ]