wood burning stoves 2.0*
The moose likes Servlets and the fly likes Threading in a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Threading in a Servlet" Watch "Threading in a Servlet" New topic
Author

Threading in a Servlet

Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
My learned colleagues, i have a small problem that i would like to pass around.
My basic premise is that i would like to use my applet to access data in a database and display it. To do this i have my small applet accessing a servlet every minute which then accesses the database. This works well however the applet is going to be sitting on a website and so at any one time several people could be looking at it, meaning that the database is being accessed each time by each user and potentially overloading the database.
What i would like to do is have my servlet access the database every minute using a thread and the sleep() method, storing the data in a vector (or arraylist). The applet could then call the servlet every minute and instead of initiating a call to the database would simply pull the data out of the most recent line of the vector and present this. My applet talks to my servlet fine, my problem is putting a thread in my servlet to talk to my database.
I almost need my servlet running in the background continually updating its data once a minute while the applet cherry picks data from the servlet as it needs it, without interrupting the servlet's thread. I am finding a real problem getting the thread in my servlet to work properly and putting the result of my ResultSet in the vector so that i can pull it out again. I have had a look around to see if anyone else has done anything similar but can't find it.
How do all these other data applets (such as stock tickers, charting applets etc) work on this situation???
Thanks for any help


Sam Tilley SCJP, SCWCD
Gopi Balaji
Ranch Hand

Joined: Jan 23, 2003
Posts: 84
An idea.
When your application (the one which the Servlet is part of) begins, create a class (a Runnable instance), whose responsibility is to query the DB every minute, and store the result in an Application-scope variable.
The applet talks to the Servlet, which retrieves the values from the Application-scope variable and sends it over to the applet.
-GB.
Sam Tilley
Ranch Hand

Joined: Dec 05, 2001
Posts: 160
Thanks Gopi, that sounds like a good idea, so you mean put all my database access information in this runnable class, and keep the servlet simply as a go-between??
I like it. The only other thing is that i need to store data in a vector or something because i have to provide 10 minute delayed data, ill have to look into whether i can do this in a application scope. Ill have a crack at it anyway
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threading in a Servlet
 
Similar Threads
Prioritize user request
NX: File Consistency
Using JDBC with Web Start
A weird idea - any ideas where to start or look
Threadsafe applet - servlet question