• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Request a simple example of persistance using servlets

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently I am opening ,then reading a simple odbc:jdbc db then formatting to html and finally closing the db from my servlet each time a connection is made to my servlet.
I know this is not efficent as opening the DB each time costs me 2 seconds and then reformatting another 2 seconds. What I would like to do is use persistance and have to avoid all this. But each time display the correct most up to data from the DB.
So how do I use persistancy?
Most importantly how do I keep my DB connection open, and avoid having to re-open, close ... between each new page access?
Would I have to read the DB to a structure (say a vector) and then if the DB has changed (how do I figure this out?) re-read to the vector.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if u want a persistance connection..
u can make it in init() method of servlet..
since init() method get called only once in a lifetime of servlet
u will get the result what u expected...
Bhupendra
 
mo patel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a good start, but anybody show me a simple example
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mo patel:
Thats a good start


That's arguable... there are several problems with this approach.

  • It is unsafe unless your servlet implements SingleThreadModel. A JDBC connection will not appreciate being used by multiple threads.
  • It scales badly, as (assuming the servlet implements SingleThreadModel) the server will instantiate your servlet lots of times under load. Before you know it you will have lots of db connections open that do nothing most of the time, and all those servlets eating memory.
  • It reduces reliability. Database connections don't keep indefinitely; eventually they break, time out, or fail otherwise. Unless you implement good error recovery your servlet will stop working sooner or later.

  • The answer to all these points is a good connection pool. Many application servers have it built in. If not, there are ready-made connection pools available, some for free.
    - Peter
 
mo patel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So using my current simple implementation is still the best way outside of using connection pools. To re-iterate my current way for each web connection
0) Enter servlet
1) Open DB
2) Read DB
3) Format to HTML
4) Close DB
5) exit servlet
 
You didn't tell me he was so big. Unlike this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic