users@jersey.java.net

Re: [Jersey] singleton resources

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Wed, 28 Oct 2009 19:05:21 -0700

On Wed, Oct 28, 2009 at 4:12 PM, Zoltan Arnold NAGY <Zoltan.Nagy_at_sun.com> wrote:
> Hi,
>
> if I understand correctly, unless I annotate a resource class with
> @Singleton, it's going to
> be created on a per-request basis, then discarded. Is that correct?
>
> If it is, doesn't it introduces a performance penalty?
>
> If all of my methods on my resource class are thread-safe, then going with a
> singleton
> might yield better performance.

It all depends on how expensive constructing said resource is. Just
constructing simple Java objects is very cheap all things considered
(you can create millions of temporary objects per second; yet most
services can't push more than hundreds of requests through, at least
if there's a DB involved).
So creating couple of extra objects per request is onlt worth
optimizing for unless these objects are expensive to create or
initialize. On plus side, per-thread instances need no synchronization
and are simpler to deal with.

-+ Tatu +-