users@jersey.java.net

Re: Restful web service threading question

From: KSChan <mr.kschan_at_gmail.com>
Date: Wed, 23 Jan 2008 20:10:03 +0800

Paul,

I didn't recognize the String mutex = "" is a constant ^^" since it works, i
use it -.-"

Do you mean that the mutex must declared with static modifier so that i
could work?

I have another question ...

What is the difference between Jersey and Metro in the glassfish project ??
I think they are both dealing with web service ...

ks.

On Jan 23, 2008 12:46 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:

> Hi ks,
>
> Wow!
>
> I had to write a little program to test this out as i was very confused
> why the synchronization block would work for multiple instances of the
> Draft class.
>
> Doh! the "" is a constant hence a non-static field with that value for
> two different instances will have the same reference.
>
> If the mutex value is changed to:
>
> String mutex = new String("");
>
> then things will not work.
>
> Paul.
>
> KSChan wrote:
> > Paul,
> >
> > indeed, the class likes this ..
> >
> > @UriTemplate( "/draft" )
> > @ProduceMime( "text/xml" )
> > @ConsumeMime( "application/x-www-form-urlencoded" )
> > class Draft {
> > String mutex = "";
> >
> > @UriTemplate( "/aTemplate" )
> > @HttpMethod( "GET" )
> > public Response webServiceOperation( ... ) {
> > synchronized ( mutex ) {
> > // obtain JDBC connection here
> > }
> > // carry DB operation here
> > }
> > }
> >
> > what i do is to provide ontology processing through Jena API. I don't
> > know what's happening in Jena but when there is concurrent request, the
> > API's operation break ... i worked around with the above
> synchronization.
> >
> > ks.
> >
> > On Jan 22, 2008 6:33 PM, Paul Sandoz <Paul.Sandoz_at_sun.com
> > <mailto:Paul.Sandoz_at_sun.com>> wrote:
> >
> > Hi ks,
> >
> > What are the annoations on the Draft class?
> >
> > Is the Draft class a singleton instantiated once per-application
> rather
> > than per-request?
> >
> > If Draft is instantiated per-request then i am surprised your
> solution
> > below will work as the mutex reference will be unique for each
> request.
> >
> > If Draft is instantiated once per-application then this should work,
> > although it may not be necessary to use mutex as you can do the
> > following instead:
> >
> > class Draft {
> > public Response webServiceOperation( ... ) {
> > synchronized ( this ) {
> > // obtain JDBC connection here
> > }
> > // carry DB operation here
> > }
> > }
> >
> > if synchronizing on 'this' does not interfere with other operations
> you
> > have defined.
> >
> > I am not sure how you are using the JDBC connection stuff but you
> might
> > want to take a look at the Bookmark example, which uses JPA. There
> is
> > limited support for DB stuff for injecting an EntityManagerFactory
> > corresponding to a persistence unit name (see the web.xml file).
> From
> > that thread safe class an entity manager can be created to perform
> DB
> > operations using JPA.
> >
> > Paul.
> >
> > KSChan wrote:
> > > Marc,
> > >
> > > I have solved the problem by using synchronization as normal
> > threading
> > > model.
> > >
> > > In my case, I have concurrent AJAX calls to the web service in
> > which the
> > > web service obtains a JDBC connection from the glassfish
> connection
> > > pool. I don't know whether the connection obtained is the same
> > among the
> > > threads but I got error when the DB operation is carried out
> > sometimes.
> > >
> > > My solution is like this ..
> > >
> > > class Draft {
> > > String mutex = "";
> > > public Response webServiceOperation( ... ) {
> > > synchronized ( mutex ) {
> > > // obtain JDBC connection here
> > > }
> > > // carry DB operation here
> > > }
> > > }
> > >
> > > ks.
> > >
> > > On Jan 21, 2008 4:57 PM, Paul Sandoz <Paul.Sandoz_at_sun.com
> > <mailto:Paul.Sandoz_at_sun.com>
> > > <mailto:Paul.Sandoz_at_sun.com <mailto:Paul.Sandoz_at_sun.com>>> wrote:
> > >
> > > CC'ing as ks is not a member of the users list.
> > >
> > > Paul.
> > >
> > > Marc Hadley wrote:
> > > > On Jan 19, 2008, at 1:31 AM, KSChan wrote:
> > > >>
> > > >> I am facing a problem that more than one concurrent web
> > service
> > > >> threads are accessing the same resource and making
> something
> > > wrong ...
> > > >>
> > > >> How can I deal with this?
> > > >>
> > > > The default is that Jersey will create a new resource
> class
> > > instance for
> > > > each request (thread) so unless you have some static
> > fields that
> > > aren't
> > > > protected by a mutex then you should be OK. Can you
> > describe in a
> > > little
> > > > more detail what your resource class looks like and what
> > problem
> > > you are
> > > > seeing ?
> > > >
> > > > Thanks,
> > > > Marc.
> > > >
> > > > ---
> > > > Marc Hadley <marc.hadley at sun.com <http://sun.com>
> > <http://sun.com>>
> > > > CTO Office, Sun Microsystems.
> > > >
> > > >
> > > >
> > > >
> > >
> >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail:
> > users-unsubscribe_at_jersey.dev.java.net
> > <mailto:users-unsubscribe_at_jersey.dev.java.net>
> > > <mailto:users-unsubscribe_at_jersey.dev.java.net
> > <mailto:users-unsubscribe_at_jersey.dev.java.net>>
> > > > For additional commands, e-mail:
> > users-help_at_jersey.dev.java.net <mailto:
> users-help_at_jersey.dev.java.net>
> > > <mailto:users-help_at_jersey.dev.java.net
> > <mailto:users-help_at_jersey.dev.java.net>>
> > > >
> > >
> > > --
> > > | ? + ? = To question
> > > ----------------\
> > > Paul Sandoz
> > > x38109
> > > +33-4-76188109
> > >
> > >
> > >
> > >
> > > --
> > > I, have a dream. I want to make my dream come true.
> > >
> > > http://mr.kschan.googlepages.com
> >
> > --
> > | ? + ? = To question
> > ----------------\
> > Paul Sandoz
> > x38109
> > +33-4-76188109
> >
> >
> >
> >
> > --
> > I, have a dream. I want to make my dream come true.
> >
> > http://mr.kschan.googlepages.com <http://mr.kschan.googlepages.com>
>
> --
> | ? + ? = To question
> ----------------\
> Paul Sandoz
> x38109
> +33-4-76188109
>



-- 
I, have a dream. I want to make my dream come true.
http://mr.kschan.googlepages.com