You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Ed Staub <es...@mediaone.net> on 2000/09/01 03:02:42 UTC
Are XSP Pages singletons?
Are XSP pages singletons, like Servlets?
In other words, can class variables be used dynamically without fear of
thread collision?
Thanks,
-Ed Staub
Re: Are XSP Pages singletons?
Posted by Ricardo Rocha <ri...@apache.org>.
Ed Staub wrote:
> The Servlet 2.2 spec says in section 2.3:
> "By default, there must be only one instance of a servlet class per servlet
> definition in a container..."
Similarily, there's only one instance of an XSP page class per Cocoon
session. This means all instance variables are shared across page
requests.
> ???????????????????????????????????????????????????????????????????
> Is it possible for two threads to be running populateDocument() on the
> same instance of an XSP page concurrently?
> ???????????????????????????????????????????????????????????????????
The answer is yes: each thread spawned by the servlet engine to
serve an HTTP request will execute populateDocument(). This implies you
should take care of synchronizing access to critical instance variables
because they're shared.
Ricardo
==============================================================================
Ed Staub wrote:
>
> Stefano and Matt,
>
> I wrote earlier:
>
> > In other words, can class variables be used dynamically without fear of
> > thread collision?
>
> Sorry, I was not clear, let me try again!
> By "class variable" I meant "instance variable", i.e., a non-static variable
> declared at the class level.
>
> Stefano wrote:
>
> >And, anyway, servlets are not singletons at all: it's up to the
> >container to choose how many instances there are if you don't implement
> >SingleThreadModel (which is totally stupid and many of us on the Servlet
> >API JSR want to deprecate)
>
> This has me really confused!
>
> The Servlet 2.2 spec says in section 2.3:
> "By default, there must be only one instance of a servlet class per servlet
> definition in a container. In the case of a servlet that implements the
> SingleThreadModel interface, the servlet container may instantiate multiple
> instances of that servlet so that it can handle a heavy request load while
> still serializing requests to a single instance. In the case where a
> servlet was deployed as part of an application that is marked in the
> deployment descriptor as distributable, there is one instance of a servlet
> class per servlet definition per VM in a container. If the servlet
> implements the SingleThreadModel interface as well as is part of a
> distributable web application, the container may instantiate multiple
> instances of that servlet in each VM of the container."
>
> So that's why I talked about singleton servlets. Stefano, can you explain?
> Has normal practice advanced beyond the standard?
>
> -----------
>
> But what I _really_ wanted to ask was:
>
> ???????????????????????????????????????????????????????????????????
> Is it possible for two threads to be running populateDocument() on the
> same instance of an XSP page concurrently?
> ???????????????????????????????????????????????????????????????????
>
> Obviously this is important to know in order to avoid writing invalid pages.
> All of the examples in the XSP doc would work either way, and the doc
> doesn't state whether it's a single-instance model or not.
>
> In the proposal I floated earlier today ("RE: Aha! got it! 64k limit" from
> Fri 1 Sept 2000 3:27 PM) I created a PopulatingContext object to hold what
> are currently locals in populateDocument(), because I wasn't sure if it was
> safe to move some of the PopulateContext() locals into instance variables.
> Hence the question!
>
> Thanks,
> -Ed Staub
>
> -----Original Message-----
> From: Stefano Mazzocchi [mailto:stefano@apache.org]
> Sent: Friday, September 01, 2000 6:22 PM
> To: cocoon-users@xml.apache.org
> Subject: Re: Are XSP Pages singletons?
>
> Ed Staub wrote:
> >
> > Are XSP pages singletons, like Servlets?
> >
> > In other words, can class variables be used dynamically without fear of
> > thread collision?
>
> I wouldn't even dare trying: this is a sure way to get trouble in a
> server enviornment...
>
> And, anyway, servlets are not singletons at all: it's up to the
> container to choose how many instances there are if you don't implement
> SingleThreadModel (which is totally stupid and many of us on the Servlet
> API JSR want to deprecate)
>
> --
> Stefano Mazzocchi One must still have chaos in oneself to be
> able to give birth to a dancing star.
> <st...@apache.org> Friedrich Nietzsche
> --------------------------------------------------------------------
> Missed us in Orlando? Make it up with ApacheCON Europe in London!
> ------------------------- http://ApacheCon.Com ---------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: cocoon-users-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: cocoon-users-help@xml.apache.org
Re: Are XSP Pages singletons?
Posted by Stefano Mazzocchi <st...@apache.org>.
Giacomo Pati wrote:
>
> On Sat, Sep 02, 2000 at 04:35:39PM +0200, Stefano Mazzocchi wrote:
> > Ed Staub wrote:
> >
> > > In the case where a
> > > servlet was deployed as part of an application that is marked in the
> > > deployment descriptor as distributable, there is one instance of a servlet
> > > class per servlet definition per VM in a container.
> >
> > This is another section where the spec is very bad. A servlet to be
> > "distributable" must follow very restrictive practices (for example,
> > avoid instantiating threads, accessing files directly, etc...), much
> > like the EJB spec declares for EJBs.
>
> Does the sitemap compilation engine follow bad programming
> practice by starting a separate thread for sitemap code
> generation and compilation?
Yes, it does, but only in terms of "distributable" servlets.
In highly clustered environments, servlets should be easy to distribute,
but they see a servlet as HelloWorldServlet instead of a servlet engine
extension package, as Cocoon is.
Cocoon and Turbine are probably the most complex servlets ever and the
spec doesn't even go that far.
Sure, there will be tons of sections where we'll not use good
programming patterns from the Servlet API, but do not worry, this is
just because there is no ServletEngine extention API we can use in
alternative (well, Catalina has such hooks, and Catalina (aka tomcat
4.0) will be portable on all web servers, so we do have that choice
without reducing our scope in terms of platforms).
But anyway, we'll keep providing a Servlet API entrance to the system
and venture new connection mechanisms with the engines if they are
available and provide better functionality/performance.
But nothing to worry about at this point.
--
Stefano Mazzocchi One must still have chaos in oneself to be
able to give birth to a dancing star.
<st...@apache.org> Friedrich Nietzsche
--------------------------------------------------------------------
Missed us in Orlando? Make it up with ApacheCON Europe in London!
------------------------- http://ApacheCon.Com ---------------------
Re: Are XSP Pages singletons?
Posted by Giacomo Pati <Gi...@pwr.ch>.
On Sat, Sep 02, 2000 at 04:35:39PM +0200, Stefano Mazzocchi wrote:
> Ed Staub wrote:
>
> > In the case where a
> > servlet was deployed as part of an application that is marked in the
> > deployment descriptor as distributable, there is one instance of a servlet
> > class per servlet definition per VM in a container.
>
> This is another section where the spec is very bad. A servlet to be
> "distributable" must follow very restrictive practices (for example,
> avoid instantiating threads, accessing files directly, etc...), much
> like the EJB spec declares for EJBs.
Does the sitemap compilation engine follow bad programming
practice by starting a separate thread for sitemap code
generation and compilation?
Giacomo
--
PWR GmbH, Organisation & Entwicklung Tel: +41 (0)1 856 2202
Giacomo Pati, CTO/CEO Fax: +41 (0)1 856 2201
Hintereichenstrasse 7 Mobil: +41 (0)78 759 7703
CH-8166 Niederweningen Mailto:Giacomo.Pati@pwr.ch
Web: http://www.pwr.ch
Re: Are XSP Pages singletons?
Posted by Stefano Mazzocchi <st...@apache.org>.
Ed Staub wrote:
>
> Stefano and Matt,
>
> I wrote earlier:
>
> > In other words, can class variables be used dynamically without fear of
> > thread collision?
>
> Sorry, I was not clear, let me try again!
> By "class variable" I meant "instance variable", i.e., a non-static variable
> declared at the class level.
>
> Stefano wrote:
>
> >And, anyway, servlets are not singletons at all: it's up to the
> >container to choose how many instances there are if you don't implement
> >SingleThreadModel (which is totally stupid and many of us on the Servlet
> >API JSR want to deprecate)
>
> This has me really confused!
>
> The Servlet 2.2 spec says in section 2.3:
> "By default, there must be only one instance of a servlet class per servlet
> definition in a container.
Yes, yes, I used the term "container" incorrectly. In the spec
"container" is a servlet context, I used "container" to name the engine.
Sorry, my mistake.
> In the case of a servlet that implements the
> SingleThreadModel interface, the servlet container may instantiate multiple
> instances of that servlet so that it can handle a heavy request load while
> still serializing requests to a single instance.
This is what some of us want to deprecate: it's clearly misleading since
there is nothing in a normal servlet to removes the ability to stand
heavy request load while still behavior properly, it's just a matter of
good concurrent programming practice
SingleThreadModel was introduced as a way to allow newbie servlet
writers to fix concurrency problems by simply having the servlet
container give a new instance to each different thread, but clearly
enforces bad server side programming practices and doesn't really help
performance (actually it decreases it)
> In the case where a
> servlet was deployed as part of an application that is marked in the
> deployment descriptor as distributable, there is one instance of a servlet
> class per servlet definition per VM in a container.
This is another section where the spec is very bad. A servlet to be
"distributable" must follow very restrictive practices (for example,
avoid instantiating threads, accessing files directly, etc...), much
like the EJB spec declares for EJBs.
We are working on cleaning this up.
> If the servlet
> implements the SingleThreadModel interface as well as is part of a
> distributable web application, the container may instantiate multiple
> instances of that servlet in each VM of the container."
> So that's why I talked about singleton servlets. Stefano, can you explain?
> Has normal practice advanced beyond the standard?
No, the problem is "container" definition: if a container is a JVM, then
the spec says that you can have one and only one servlet instance per
servlet class. This is correctly a singleton pattern.
If a container is a servlet sandbox and a JVM can have many containers
(like normally happens for ISPs, for example), then you can have many
instances of the same servlet class (for example, two Cocoons running),
but only one per sandbox.
So, a servlet is a singleton in a sandbox, but it's may not be in a
servlet engine.
Sorry, I should have stated by terminology clearer. Anyway, the spec
says that "container" equals "sandbox" NOT "servlet engine".
--
Stefano Mazzocchi One must still have chaos in oneself to be
able to give birth to a dancing star.
<st...@apache.org> Friedrich Nietzsche
--------------------------------------------------------------------
Missed us in Orlando? Make it up with ApacheCON Europe in London!
------------------------- http://ApacheCon.Com ---------------------
RE: Are XSP Pages singletons?
Posted by Ed Staub <es...@mediaone.net>.
Stefano and Matt,
I wrote earlier:
> In other words, can class variables be used dynamically without fear of
> thread collision?
Sorry, I was not clear, let me try again!
By "class variable" I meant "instance variable", i.e., a non-static variable
declared at the class level.
Stefano wrote:
>And, anyway, servlets are not singletons at all: it's up to the
>container to choose how many instances there are if you don't implement
>SingleThreadModel (which is totally stupid and many of us on the Servlet
>API JSR want to deprecate)
This has me really confused!
The Servlet 2.2 spec says in section 2.3:
"By default, there must be only one instance of a servlet class per servlet
definition in a container. In the case of a servlet that implements the
SingleThreadModel interface, the servlet container may instantiate multiple
instances of that servlet so that it can handle a heavy request load while
still serializing requests to a single instance. In the case where a
servlet was deployed as part of an application that is marked in the
deployment descriptor as distributable, there is one instance of a servlet
class per servlet definition per VM in a container. If the servlet
implements the SingleThreadModel interface as well as is part of a
distributable web application, the container may instantiate multiple
instances of that servlet in each VM of the container."
So that's why I talked about singleton servlets. Stefano, can you explain?
Has normal practice advanced beyond the standard?
-----------
But what I _really_ wanted to ask was:
???????????????????????????????????????????????????????????????????
Is it possible for two threads to be running populateDocument() on the
same instance of an XSP page concurrently?
???????????????????????????????????????????????????????????????????
Obviously this is important to know in order to avoid writing invalid pages.
All of the examples in the XSP doc would work either way, and the doc
doesn't state whether it's a single-instance model or not.
In the proposal I floated earlier today ("RE: Aha! got it! 64k limit" from
Fri 1 Sept 2000 3:27 PM) I created a PopulatingContext object to hold what
are currently locals in populateDocument(), because I wasn't sure if it was
safe to move some of the PopulateContext() locals into instance variables.
Hence the question!
Thanks,
-Ed Staub
-----Original Message-----
From: Stefano Mazzocchi [mailto:stefano@apache.org]
Sent: Friday, September 01, 2000 6:22 PM
To: cocoon-users@xml.apache.org
Subject: Re: Are XSP Pages singletons?
Ed Staub wrote:
>
> Are XSP pages singletons, like Servlets?
>
> In other words, can class variables be used dynamically without fear of
> thread collision?
I wouldn't even dare trying: this is a sure way to get trouble in a
server enviornment...
And, anyway, servlets are not singletons at all: it's up to the
container to choose how many instances there are if you don't implement
SingleThreadModel (which is totally stupid and many of us on the Servlet
API JSR want to deprecate)
--
Stefano Mazzocchi One must still have chaos in oneself to be
able to give birth to a dancing star.
<st...@apache.org> Friedrich Nietzsche
--------------------------------------------------------------------
Missed us in Orlando? Make it up with ApacheCON Europe in London!
------------------------- http://ApacheCon.Com ---------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-users-help@xml.apache.org
Re: Are XSP Pages singletons?
Posted by Stefano Mazzocchi <st...@apache.org>.
Ed Staub wrote:
>
> Are XSP pages singletons, like Servlets?
>
> In other words, can class variables be used dynamically without fear of
> thread collision?
I wouldn't even dare trying: this is a sure way to get trouble in a
server enviornment...
And, anyway, servlets are not singletons at all: it's up to the
container to choose how many instances there are if you don't implement
SingleThreadModel (which is totally stupid and many of us on the Servlet
API JSR want to deprecate)
--
Stefano Mazzocchi One must still have chaos in oneself to be
able to give birth to a dancing star.
<st...@apache.org> Friedrich Nietzsche
--------------------------------------------------------------------
Missed us in Orlando? Make it up with ApacheCON Europe in London!
------------------------- http://ApacheCon.Com ---------------------
Re: Are XSP Pages singletons?
Posted by Matthew Cordes <mc...@maine.edu>.
Hey Ed,
> Are XSP pages singletons, like Servlets?
Can you explain why you think servlets are singletons?
As I understand a singleton is an object that can only exist once per
virtual machine ( e.g. JDBC's DriverManager, Apache's Turbine connection
pool DBMonitor, etc ). Creating a singleton is not difficult ( I can
send you some code ) or you could look at the the other two examples I
mentioned.
Servlets are not singletons ( as I understand ). If I connection to
tomcat on your machine and request XXXServlet which you are already
viewing AFAIK another instance of XXXServlet is created for me to use,
thus it exists twice within a single VM making it is not a singleton.
> In other words, can class variables be used dynamically without fear of
> thread collision?
This needs explanation too. How about an example?
-matt