This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threadsafe applet - servlet question  RSS feed

 
Sam Tilley
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Whats the best way to make a servlet thread safe. My servlet creates an object from another class which contains a thread and accesses a database regularly to pull out information and put it in an Arraylist.
An applet then regularly accesses the servlet on a threaded timer, which then access the object to return the ArrayList, the servlet then pulls data out of the arraylist and passes it back to the applet. With me so far
However in the future there will be several applets accessing the servlet. Whats the best way to configure my servlet so that it can be accessed by loads of applets at once without having any problems ( i am currently using SingleThreadModel but Core Servlets said that performance can be hurt using this method if they are accessed frequently).
Ideally there will be just one servlet creating one object which access the database once. Then multiple servlets will be able to access the same data from the servlet (showing delayed stock prices) and print it out in the applet. Both the applet and object accessing the database have their own threads and have their own sleep methods to pause them for set periods of time.
Thanks for any help, and sorry for the confusing description
Thanks
Sam
[ February 27, 2003: Message edited by: Sam Tilley ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If none of the applets accessing the servlet is modifying the servlet in any way you should be ok if you obey a few simple rules. For example, you should be cautious about using instance variables.
When you are updating this object that accesses the database, be sure that requests don't try to get data that is only partially updated. Keeping a valid ArrayList while a new one is being created for example.
The Servlet API is designed to help you keep requests separate and non-interfering. Using the SingleThreadModel is certainly a bad idea.
There have been alot of related discussions on this forum so look around in the older threads.
Bill
 
Sam Tilley
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou William,
My object is synchronized in its access to the database and i have also synchronized its methods for returning the array list and other variables. I had a quick search but will have a better one when i get a chance.
So the SingleThreadModel is a bad idea, rats i thought it might be. My problem is that i want to show the same data to each applet, and thought that using SingleThread would be a way to do this. By keeping it MultiThreaded it starts up a new instance of the servlet each time, and i need to make sure that this won't overload anything if we get too many.
Oh and yes, the applet doesn't modify anything, it just makes a connection and then receives back information.
If you have any other pointers please pass them this way anyone, and i will do some better research
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!