You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Per Kreipke <pe...@onclave.com> on 2002/08/31 21:17:25 UTC

[Q] Singleton Objects across webapps...

A "best practices" question:

What is the best way of sharing a single, changeable copy of common
information across multiple servlets?


Example Scenario:

Using two servlets (webdav and cocoon), I need to share common information
between them.

There are two choices for how to host the servlets, each of which affects
the options for sharing info:

- host the servlets in different contexts (a likely requirement if security
constraints differ)

- host the servlets in the same context.


Then, within each servlet you could share the information through:
- singleton classes
 + pro: conceptually simple
 + con: singleton pattern isn't bulletproof
 + con: threading issues?
 + con: lifecyle issues (can't use servlet destroy() if have multiple
servlets)

- avalon roles
 + pro: convenient, correct lifecycle management
 + pro: using avalon in other apps (James) in same JVM could also share info
 + con: poolable (e.g. multiple instances) and not the same as singleton

- context attributes
 + pro: conceptually simple, no singleton coding necessary
 + con: HTTP specific, information can't be shared with non TC app (James)
 + con: can't share across contexts, all servlets must live in same webapp

- enterprise javabeans
 + pro: managed
 + con: requires J2EE server?

1) Are there other options I'm missing?

2) What have people found to be the best pattern?

3) Besides the servlet 2.3 reference docs and O'Reilly books, can anyone
recommend reading material that includes these issues (most introductory
books cover the same 'your first webapp' type of material).


Thanks in advance for your opinion,
Per


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [Q] Singleton Objects across webapps...

Posted by Will Hartung <wi...@msoft.com>.
Sharing data between servlets obviously depends on several things.

If your servlets are within the same webapp, then the ServletContext is the
most obvious choice to store the shared information, however, you will need
to synchronize the data access and modification within the object. If you
use the ServletContext, you don't have to worry about the Singleton pattern
either.

If your servlets are in different webapps, then you can't count on anything,
notably Singletons.

Specifically, although it is commonly done, there is no guarantee that two
different webapps are even running within the same JVM. But, more important
is the differences in the classloaders between the two webapps. A class
within Java is distinguished not only by name, but by its class loader.
Mixing the class loaders into "common" areas can make a mess of things in a
hurry, particularly with Singletons.

So if you're trying to share information between disparate webapps, you
really need to go outside the container. By "outside" the container, I mean
some kind of external service, either a database, JNDI, EJB, RMI, JINI
JavaSpaces, or something else you've created on your own. A very simple
"outside" service is yet another webapp designed specifically to manage your
shared information.

By creating another servlet, you get all of the container provided
management to start, stop, and remove the servlet "for free". The only real
sticky wicket would be ensuring that your little state manager webapp starts
up before the other webapps require its services. Also, you'll probably want
to narrow who has access to this webapp to just the local webapps, or some
other authentication mechanism.

But, in the end, it all boils down to what kind of state you're sharing.

Regards,

Will Hartung




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [Q] Singleton Objects across webapps...

Posted by Per Kreipke <pe...@onclave.com>.
I hate to repost but I'm not clear why no one has any advice.

Am I asking such a simple question that I just missed it in the FAQ
somewhere?

_any_ pointer is appreciated.

Per

> A "best practices" question:
>
> What is the best way of sharing a single, changeable copy of common
> information across multiple servlets?
>
>
> Example Scenario:
>
> Using two servlets (webdav and cocoon), I need to share common information
> between them.
>
> There are two choices for how to host the servlets, each of which affects
> the options for sharing info:
>
> - host the servlets in different contexts (a likely requirement
> if security
> constraints differ)
>
> - host the servlets in the same context.
>
>
> Then, within each servlet you could share the information through:
> - singleton classes
>  + pro: conceptually simple
>  + con: singleton pattern isn't bulletproof
>  + con: threading issues?
>  + con: lifecyle issues (can't use servlet destroy() if have multiple
> servlets)
>
> - avalon roles
>  + pro: convenient, correct lifecycle management
>  + pro: using avalon in other apps (James) in same JVM could also
> share info
>  + con: poolable (e.g. multiple instances) and not the same as singleton
>
> - context attributes
>  + pro: conceptually simple, no singleton coding necessary
>  + con: HTTP specific, information can't be shared with non TC app (James)
>  + con: can't share across contexts, all servlets must live in same webapp
>
> - enterprise javabeans
>  + pro: managed
>  + con: requires J2EE server?
>
> 1) Are there other options I'm missing?
>
> 2) What have people found to be the best pattern?
>
> 3) Besides the servlet 2.3 reference docs and O'Reilly books, can anyone
> recommend reading material that includes these issues (most introductory
> books cover the same 'your first webapp' type of material).
>
>
> Thanks in advance for your opinion,
> Per


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>