Request a simple example of persistance using servlets
mo patel
Greenhorn
Joined: Mar 07, 2001
Posts: 15
posted
0
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
posted
0
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
posted
0
Thats a good start, but anybody show me a simple example
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
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
posted
0
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
subject: Request a simple example of persistance using servlets