aspose file tools
The moose likes Servlets and the fly likes Request a simple example of persistance using servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Request a simple example of persistance using servlets" Watch "Request a simple example of persistance using servlets" New topic
Author

Request a simple example of persistance using servlets

mo patel
Greenhorn

Joined: Mar 07, 2001
Posts: 15
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

Joined: Dec 01, 2000
Posts: 118
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


<B>Bhupendra Mahajan</B>
mo patel
Greenhorn

Joined: Mar 07, 2001
Posts: 15
Thats a good start, but anybody show me a simple example
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Mar 07, 2001
Posts: 15
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
 
IntelliJ Java IDE
 
subject: Request a simple example of persistance using servlets
 
Threads others viewed
Right or not? Servlets share the same object.
TableModel problem
The best way to connect\disconnect to Database
Is this a good approach?
huge and slow Application
MyEclipse, The Clear Choice