• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets and Database Connection

 
Sobha George
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Siggelkow
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Meera Chandrasekaran
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic