permaculture playing cards*
The moose likes JSP and the fly likes Please Help!  I Have Worked On IT For Days. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Please Help!  I Have Worked On IT For Days." Watch "Please Help!  I Have Worked On IT For Days." New topic
Author

Please Help! I Have Worked On IT For Days.

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
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

VerificationServlet.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 ]
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
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?).
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
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!
Ron Perich
Ranch Hand

Joined: Mar 25, 2003
Posts: 41
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.


RP
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help! I Have Worked On IT For Days.