This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Request a simple example of persistance using servlets

 
mo patel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thats a good start, but anybody show me a simple example
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic