• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlets and Database Connection

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic