File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlets and Database Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets and Database Connection" Watch "Servlets and Database Connection" New topic
Author

Servlets and Database Connection

Sobha George
Greenhorn

Joined: Jan 12, 2002
Posts: 3
I have a Servlet that retrieves information from Database. It then stores the Resultset in the session and forward to another servlet. After that it closes the connection in the finally block. This seems to be working so far, I am not sure if this would always work, since the ResultSet is not Serializable. But I couldn't find a better way to pass around the information from one servlet to another (unless I process the data and store in a Set/Map). Does anybody has any experience with this?
Thank you,
Shobha
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
This really wants to be a request-scoped rather than a session-scoped object. That would remove the serialization concerns as well.
- Peter
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

hi,
I don't understand why result set needs to be serializable when u r forwarding the request to another servlet withing the same web app.
It's better to store the result set in request scope and not session scope.


Groovy
Bill Siggelkow
Ranch Hand

Joined: Jun 27, 2001
Posts: 57
A good question ... as soon as you close the connection you are going to lose the ResultSet ... result sets are essentially a "view" of the data and rely on a connection. The solution is to marshall the data from the result into some Collection (as you stated). Another option would be to use a CachedRowSet or similar RowSet provided by JDBC 2.0 (if you can find a decent implementation).


Bill Siggelkow
Jade Cove Solutions
Meera Chandrasekaran
Greenhorn

Joined: Jan 08, 2002
Posts: 10
Hi Shobha,
As Bill said, resultset is invalid after the connection is closed. Also it is a good programming practice to iterate thro the result set as early as possible and copy data into set/map or some simple data holder classes with get/set methods, depending on your application and close connection. you can then pass the data holders around as your application logic says.
 
Don't get me started about those stupid light bulbs.
 
subject: Servlets and Database Connection