Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SingleThreadModel

 
Randall Twede
Ranch Hand
Posts: 4489
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If using SingleThreadModel is a bad idea and its better to just synchronize code that needs to be synchronized, why have SingleThreadModel at all? Are there times/places where it is good to use it?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SingleThreadModel is for use when you are dependant on external resources (outside Java) which may only be accessed one at a time. A servlet which only accesses Java methods and values can always be written in a way which doesn't need SingleThreadModel.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Carver:
SingleThreadModel is for use when you are dependant on external resources (outside Java) which may only be accessed one at a time. A servlet which only accesses Java methods and values can always be written in a way which doesn't need SingleThreadModel.

Erm, actually, no.
SingleThreadModel does more than just cause requests to your servlet to be serialised. It also tells the application server that it cannot handle a high load by simply running more threads to your servlet. It has to create multiple instances instead.
When you just synchronize your code, the server doesn't know about it and will instantiate your servlet just once. As a consequence, if there is significant synchronized code, your application is likely to scale far worse than it would do with SingleThreadModel.
See section 3.2 of the servlet spec (v2.2).
- Peter
 
Randall Twede
Ranch Hand
Posts: 4489
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I mentioned my servlets currently implement SingleThreadModel. They connect to an Access database. From what little I learned about Access, it wont allow access if it is open. I am thinking I have a problem. Since I can have more than one instance of each servlet, if more than one client tries to access the data base at the same time as another, I am thinking they will just get an error.
try {
con = DriverManager.getConnection (URL,username,password);
stmt = con.createStatement();
}
catch (Exception e) {
System.err.println("problems connecting to "+URL);
}
even if they didnt implement SingleThreadModel, still I have one servlet that reads from the database and another that writes to it. So I would still have a problem? Im thinking maybe if there is an error connecting to the database, the servlet should try again.

[This message has been edited by Randall Twede (edited January 27, 2001).]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randall Twede:
As I mentioned my servlets currently implement SingleThreadModel. They connect to an Access database. From what little I learned about Access, it wont allow access if it is open.

Unfortunately, I couldn't tell you -- I prefer real databases
If Access really allows just one connection, then dropping the SingleThreadModel and using a synchronized block around your database code might be appropriate. The servlet engine would then never try to instantiate the servlet multiple times.
- Peter
 
Randall Twede
Ranch Hand
Posts: 4489
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I misunderstood or am misinformed. It might be that if I am running Access and open that database, then my application cant make connections.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic