Well, this was part of my question. Synchronized block guarantees thread
safety, but it also becomes a major bottlencek for handling concurrent
requests.
[...]
As I said in my own follow-up, the SingleThreadModel marker does cause
Tomcat to create a new instance of the servlet when the requests collide.
And I don't see anything wrong in having a separate object handle the
request - this way, both "conflicting" requests can proceed without waiting
for one another.
I'm not sure you understood what I said. Either your servlet uses
shared state, or it doesn't. SingleThreadModel doesn't magically make
shared state into non-shared state. It just changes where you can put
the two.
If you use shared state, then you should use synchronized blocks to
manage access to it, regardless of whether SingleThreadModel is used or
not. If you're not using shared state, then of course you shouldn't use
synchronized blocks, since that would introduce unnecessary mutual
exclusion in your application.
As I said earlier, if you want an object per request to store shared
state, go ahead and create it; just don't use the servlet class for that
purpose!
What I observe in the Tomcat behavior is how I actually understood the
meaning and purpose of SingleThreadModel. I must be missing something very
fundamental, since I saw a number of caveats similar to yours about it, but
none of them explained what the "ambiguity and limitations" really are, and
how they affect the integrity/performance/whatever of the servlet.
The ambiguity is that the name "SingleThreadModel" really implies that
only a single thread at a time can run. That would be the ultimate
mutual exclusion, and I think is what was originally intended by the
interface. It's not very useful, though, in a production application
that gets any kind of load. Because it's not very useful that way, the
fine folks over at Apache read the spec very closely and decided that
they could follow the letter of the specification by just creating
several objects of the servlet class, and continuing to service multiple
requests at a time. That was a reasonable thing to do -- turning a
completely useless feature into one that's merely bad -- but it changed
the intent of the specification. As far as I know, Sun never chose to
clarify whether that technique should be considered acceptable or not.
Nevertheless, practically everyone does it.
The limitations are fundamental in the idea of SingleThreadModel itself.
The SingleThreadModel marker interface only applies to the servlet
class. Real applications can sometimes have quite a large number of
servlet classes, and put as little code as possible into the servlet
classes themselves. Often the same piece of code is run from several
servlets, and you're back to the same problem again. Basically, the
problem is that SingleThreadModel is a crutch, and like actual crutches,
it limits the amount of stuff you can do while still taking advantage of
the crutch. It excludes certain things that are very good practices.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation