You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Malia Noori <mn...@blueridgenetworks.com> on 2004/09/30 16:05:41 UTC

method level synchronization doesn't work

Hi,

 

I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC.  I
have a JSP that calls a web bean.  This web bean has a section of code that
I want to synchronize, but when I synchronize the method, Tomcat doesn't
actually synchronize the threads.  (I tried this by having 2 users access my
JSP at the same time).  When I synchronize the code in the method by using a
mutex, it works.  

 

So, doing this doesn't work:

 

public synchronized void amethod()

{

 

  //some code that access the database and needs to be synchronized

}

 

But doing this works:

 

public void amethod()

{

 

String mutex = "";

 

synchronize(mutex)

{

 

  //some code that access the database and needs to be synchronized

 

}

}

 

 

Why does synchronization on the method doesn't work?  Does Tomcat not allow
locking of object caused by method level synchronization?  Any help will be
appreciated.

 

Thanks,

Malia

 


Re: method level synchronization doesn't work

Posted by Peter Lin <wo...@gmail.com>.
you might want to consider using a stored procedure to make it look
like a single call from tomcat. is the goal here to make sure the
counter is incremented correctly? I'm guessing there's situations
where the counter shouldn't be incremented?

you can also use a subquery to increment the counter. there's plenty
of articles on how to do this the simple way. I hope that helps

peter



On Thu, 30 Sep 2004 10:43:22 -0400, Malia Noori
<mn...@blueridgenetworks.com> wrote:
> Actually, the data that I am modifying requires a transaction and
> synchronization.  It increments a counter stored in the database.  So, I
> have to do a select to get the current value, increment the counter, and
> then insert the new value.  So if two threads are accessing it at the same
> time, the counter will not be properly incremented.  What's puzzling is that
> method level synchronization does not work while synchronizing on a block of
> code inside a method works.
> 
> Thanks,
> Malia
> 
> 
> 
> -----Original Message-----
> From: Peter Lin [mailto:woolfel@gmail.com]
> Sent: Thursday, September 30, 2004 10:26 AM
> To: Tomcat Users List; mnoori@blueridgenetworks.com
> Subject: Re: method level synchronization doesn't work
> 
> am I missing something, but looks like you're trying to build some
> kind of web cache. why not use Hibernate or something that already
> does caching for you instead?
> 
> the only time I can see a need to sync, is if the request contains
> data that requires a transaction. Which in that case, you're better
> off doing an insert, then a second select query with the same
> connection.
> 
> or is the scenario a distributed objects setup?
> 
> peter
> 
> On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori
> <mn...@blueridgenetworks.com> wrote:
> > Hi,
> >
> > I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC.
> I
> > have a JSP that calls a web bean.  This web bean has a section of code
> that
> > I want to synchronize, but when I synchronize the method, Tomcat doesn't
> > actually synchronize the threads.  (I tried this by having 2 users access
> my
> > JSP at the same time).  When I synchronize the code in the method by using
> a
> > mutex, it works.
> >
> > So, doing this doesn't work:
> >
> > public synchronized void amethod()
> >
> > {
> >
> >  //some code that access the database and needs to be synchronized
> >
> > }
> >
> > But doing this works:
> >
> > public void amethod()
> >
> > {
> >
> > String mutex = "";
> >
> > synchronize(mutex)
> >
> > {
> >
> >  //some code that access the database and needs to be synchronized
> >
> > }
> >
> > }
> >
> > Why does synchronization on the method doesn't work?  Does Tomcat not
> allow
> > locking of object caused by method level synchronization?  Any help will
> be
> > appreciated.
> >
> > Thanks,
> >
> > Malia
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by Peter Lin <wo...@gmail.com>.
My feeling on this is, doing a sync on a data bean in this specific
case is not worth it. If the transaction is complex, which this case
isn't, use something like java transaction API.

doing either a subquery or the prepared statement example is a far
better way. doing a sync unnecessarily is a bad idea. I would advise
not doing it, unless it's absolutely the last choice.

peter



On Fri, 1 Oct 2004 10:48:16 -0500, Mike Fuellbrandt
<mi...@gmail.com> wrote:
> Correct me if I'm wrong, but couldn't the code synchronize on a static
> final member of the jsp?
> 
> private static final String COUNTER_INCREMENT_LOCK = newString("lock");
> 
> and then you would use the second method from the first post to synchronize on?
> 
> (It's been a long time since I considered synchronization so I could
> be really wrong here...)
> 
> Mike Fuellbrandt
> 
> On Thu, 30 Sep 2004 16:43:33 -0500, QM <qm...@brandxdev.net> wrote:
> > On Thu, Sep 30, 2004 at 05:31:25PM -0400, Malia Noori wrote:
> > : Thank you for replying, but I would appreciate it if you could expand on
> > : your explanation.  Here is my original post:
> > :
> > : Actually, the data that I am modifying requires a transaction and
> > : > synchronization.  It increments a counter stored in the database.  So, I
> > : > have to do a select to get the current value, increment the counter, and
> > : > then insert the new value.
> > 
> > This needn't be done within code; you could perform a database-level
> > transaction.  For example, using raw SQL calls that would come down to:
> >
> >         . set the Connection's autoCommit to false
> >         . do the DB work
> >         . call the Connection's commit() method
> >
> > I recall EJBs have their own transaction mechanism, and I'd be
> > shocked[1] if something as popular as Hibernate didn't have one as well.
> > Check the respective docs.
> >
> > Whatever the case, there should be no need to try to synchronize this at
> > the app/code level.
> >
> > -QM
> >
> > [1] = I've been shocked before, though... ;)
> 
> 
> >
> >
> >
> >
> > --
> >
> > software  -- http://www.brandxdev.net
> > tech news -- http://www.RoarNetworX.com
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by Mike Fuellbrandt <mi...@gmail.com>.
Correct me if I'm wrong, but couldn't the code synchronize on a static
final member of the jsp?

private static final String COUNTER_INCREMENT_LOCK = newString("lock");

and then you would use the second method from the first post to synchronize on?

(It's been a long time since I considered synchronization so I could
be really wrong here...)


Mike Fuellbrandt 

On Thu, 30 Sep 2004 16:43:33 -0500, QM <qm...@brandxdev.net> wrote:
> On Thu, Sep 30, 2004 at 05:31:25PM -0400, Malia Noori wrote:
> : Thank you for replying, but I would appreciate it if you could expand on
> : your explanation.  Here is my original post:
> :
> : Actually, the data that I am modifying requires a transaction and
> : > synchronization.  It increments a counter stored in the database.  So, I
> : > have to do a select to get the current value, increment the counter, and
> : > then insert the new value.
> 
> This needn't be done within code; you could perform a database-level
> transaction.  For example, using raw SQL calls that would come down to:
> 
>         . set the Connection's autoCommit to false
>         . do the DB work
>         . call the Connection's commit() method
> 
> I recall EJBs have their own transaction mechanism, and I'd be
> shocked[1] if something as popular as Hibernate didn't have one as well.
> Check the respective docs.
> 
> Whatever the case, there should be no need to try to synchronize this at
> the app/code level.
> 
> -QM
> 
> [1] = I've been shocked before, though... ;)
> 
> 
> 
> 
> --
> 
> software  -- http://www.brandxdev.net
> tech news -- http://www.RoarNetworX.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by QM <qm...@brandxdev.net>.
On Thu, Sep 30, 2004 at 05:31:25PM -0400, Malia Noori wrote:
: Thank you for replying, but I would appreciate it if you could expand on
: your explanation.  Here is my original post:
: 
: Actually, the data that I am modifying requires a transaction and
: > synchronization.  It increments a counter stored in the database.  So, I
: > have to do a select to get the current value, increment the counter, and
: > then insert the new value.

This needn't be done within code; you could perform a database-level
transaction.  For example, using raw SQL calls that would come down to:

	. set the Connection's autoCommit to false
	. do the DB work
	. call the Connection's commit() method


I recall EJBs have their own transaction mechanism, and I'd be
shocked[1] if something as popular as Hibernate didn't have one as well.
Check the respective docs.

Whatever the case, there should be no need to try to synchronize this at
the app/code level.

-QM

[1] = I've been shocked before, though... ;)


-- 

software  -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: method level synchronization doesn't work

Posted by Mike Curwen <g_...@globallyboundless.com>.
Yup, latest 4.1 does support subqueries.  But the folks around here have
something against "gamma".  ;)
 
Actually.. that's me, but we've got 4.1 installed on our dev box and are
playing.

And I do this the simplest way... I use autoincrement.  But I'd love
subqueries for other reasons.

> -----Original Message-----
> From: Peter Lin [mailto:woolfel@gmail.com] 
> Sent: Thursday, September 30, 2004 10:24 PM
> To: Tomcat Users List
> Subject: Re: method level synchronization doesn't work
> 
> 
> that would explain some of the problems you're having. I 
> thought the latest mysql supports subqueries.
> 
> http://dev.mysql.com/doc/mysql/en/Subqueries.html
> 
> 
> if you're not using the latest mysql, it might be a good idea 
> to upgrade, so you can do this the simple way.
> 
> I hope that helps
> 
> peter


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by Peter Lin <wo...@gmail.com>.
that would explain some of the problems you're having. I thought the
latest mysql supports subqueries.

http://dev.mysql.com/doc/mysql/en/Subqueries.html


if you're not using the latest mysql, it might be a good idea to
upgrade, so you can do this the simple way.

I hope that helps

peter


On Thu, 30 Sep 2004 21:14:07 -0500, Mike Curwen
<g_...@globallyboundless.com> wrote:
> Ok, since it's been brought forward...
> 
> how do you update and retrieve all in one query?  This is for: "I need a new
> primary key, and I need it to be thread safe."
> 
> something that would run on mysql?
> 
> unless you can get this magic query, it's not quite as simple as "let the
> databse handle it", is it?
> 
> 
> 
> 
> > -----Original Message-----
> > From: Peter Lin [mailto:woolfel@gmail.com]
> > Sent: Thursday, September 30, 2004 7:42 PM
> > To: Tomcat Users List; fzlists@omnytex.com
> > Subject: Re: method level synchronization doesn't work
> >
> >
> > that would be the easiest way.
> >
> > but you made it too easy, I thought people are suppose to
> > suffer and stumble :)
> >
> > peter
> >
> >
> > On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti
> > <fz...@omnytex.com> wrote:
> > > Why not just let SQL do the update?  Do the following:
> > >
> > > update my_table set counter_field = counter_field + 1 (where clause
> > > here if needed)
> > >
> > > (off the top of my head, my syntax might be off)
> > >
> > > In other words, why not let the database handle the
> > concurrency issue?
> > > That's kind of what they're there for (partly anyway).
> > >
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Well, I'm not sure it was stated that a return value was needed.  The 
way I read the original post was basically (to paraphrase): "I need to 
update a counter field, and the way I intend to do it is to retrieve the 
current value, modify it, and then write it out again as two separate 
queries".  If that's the scenario, I don't see any reason the SQL 
statement I gave (assuming I got the syntax right) wouldn't work just fine.

If your saying that you need to do the update and then retrieve the 
value for some reason, then I agree, it's not as straight-forward.  One 
thing that MIGHT work, although I'd frankly have to go try it myself to 
be sure, is a subquery, something along the lines of:

select (update table set counter=counter+1) as counter from table

Even if the theory is correct, I'm not sure about how 
database-independant it may or may not be.

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

Mike Curwen wrote:
> Ok, since it's been brought forward...
> 
> how do you update and retrieve all in one query?  This is for: "I need a new
> primary key, and I need it to be thread safe."
>  
> something that would run on mysql?
>  
> unless you can get this magic query, it's not quite as simple as "let the
> databse handle it", is it?
> 
> 
> 
>>-----Original Message-----
>>From: Peter Lin [mailto:woolfel@gmail.com] 
>>Sent: Thursday, September 30, 2004 7:42 PM
>>To: Tomcat Users List; fzlists@omnytex.com
>>Subject: Re: method level synchronization doesn't work
>>
>>
>>that would be the easiest way.
>>
>>but you made it too easy, I thought people are suppose to 
>>suffer and stumble :)
>>
>>peter
>>
>>
>>On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti 
>><fz...@omnytex.com> wrote:
>>
>>>Why not just let SQL do the update?  Do the following:
>>>
>>>update my_table set counter_field = counter_field + 1 (where clause 
>>>here if needed)
>>>
>>>(off the top of my head, my syntax might be off)
>>>
>>>In other words, why not let the database handle the 
>>
>>concurrency issue? 
>>
>>>That's kind of what they're there for (partly anyway).
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 
> 
> 
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: method level synchronization doesn't work

Posted by Mike Curwen <g_...@globallyboundless.com>.
Ok, since it's been brought forward...

how do you update and retrieve all in one query?  This is for: "I need a new
primary key, and I need it to be thread safe."
 
something that would run on mysql?
 
unless you can get this magic query, it's not quite as simple as "let the
databse handle it", is it?


> -----Original Message-----
> From: Peter Lin [mailto:woolfel@gmail.com] 
> Sent: Thursday, September 30, 2004 7:42 PM
> To: Tomcat Users List; fzlists@omnytex.com
> Subject: Re: method level synchronization doesn't work
> 
> 
> that would be the easiest way.
> 
> but you made it too easy, I thought people are suppose to 
> suffer and stumble :)
> 
> peter
> 
> 
> On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti 
> <fz...@omnytex.com> wrote:
> > Why not just let SQL do the update?  Do the following:
> > 
> > update my_table set counter_field = counter_field + 1 (where clause 
> > here if needed)
> > 
> > (off the top of my head, my syntax might be off)
> > 
> > In other words, why not let the database handle the 
> concurrency issue? 
> > That's kind of what they're there for (partly anyway).
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by Peter Lin <wo...@gmail.com>.
that would be the easiest way.

but you made it too easy, I thought people are suppose to suffer and stumble :)

peter


On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti
<fz...@omnytex.com> wrote:
> Why not just let SQL do the update?  Do the following:
> 
> update my_table set counter_field = counter_field + 1 (where clause here
> if needed)
> 
> (off the top of my head, my syntax might be off)
> 
> In other words, why not let the database handle the concurrency issue?
> That's kind of what they're there for (partly anyway).
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Why not just let SQL do the update?  Do the following:

update my_table set counter_field = counter_field + 1 (where clause here 
if needed)

(off the top of my head, my syntax might be off)

In other words, why not let the database handle the concurrency issue? 
That's kind of what they're there for (partly anyway).

Malia Noori wrote:
> Thank you for replying, but I would appreciate it if you could expand on
> your explanation.  Here is my original post:
> 
> Actually, the data that I am modifying requires a transaction and
> 
>>synchronization.  It increments a counter stored in the database.  So, I
>>have to do a select to get the current value, increment the counter, and
>>then insert the new value.  So if two threads are accessing it at the same
>>time, the counter will not be properly incremented.  What's puzzling is
> 
> that
> 
>>method level synchronization does not work while synchronizing on a block
> 
> of
> 
>>code inside a method works. 
>>
> 
> 
> Regardless of what persistence mechanism I use (i.e. Hibernate, EJB, etc), I
> have to synchronize on the method that increments the counter stored in the
> database.  I can't avoid this due to business logic.  Please let me know if
> there is alternative to synchronization.   
> 
> Thanks,
> Malia
> 
> 
> -----Original Message-----
> From: QM [mailto:qm300@brandxdev.net] 
> Sent: Thursday, September 30, 2004 3:01 PM
> To: Tomcat Users List
> Subject: Re: method level synchronization doesn't work
> 
> On Thu, Sep 30, 2004 at 01:14:26PM -0400, Malia Noori wrote:
> : I thought Tomcat instantiates only 1 instance of JSP servlet to handle all
> : requests.  Isn't that the reason why it's not suggested to have instance
> : variables?
> 
> Close, but not quite. ;)
> 
> The servlet spec permits a container to create as many instances of a
> servlet class as it sees fit.  The same goes for creating and destroying
> a servlet object several times over a context (webapp) lifetime.
> 
> That said, a servlet's main methods -- doGet(), doPost(), service() --
> may be called concurrently for the same servlet object.  These methods
> are thus meant to be thread-safe, and as such they shouldn't modify
> object instance variables.  (It's fine to call such variables, as long
> as they are read-only or at least treated as such.)
> 
> 
> : If it is the case that 2 instance of JSP servlets gets created
> : to handle each request, what's the best object to synchronize on?
> 
> I missed your original post, so I can say only this: when you find
> yourself painted into a corner, it's time to revisit the design stage.
> 
> -QM
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


AW: method level synchronization doesn't work

Posted by Steffen Heil <li...@steffen-heil.de>.
Hi

> I have to synchronize on the method ...

No you do not need to synchonize on the method, you need to synchronize.
You can synchronize on anything.

The easiest would be to do the following:

public static synchronized void amethod()
{
  //some code that access the database and needs to be synchronized
}

Just make your method static.

Synchonization will not occur on the instance object any more, but on the
Class object of your instance.
Since tomcat may instantiate multiple servlets, it may only do so in the
same classloader. Therefor all instances belong to the same Class object and
will synchronize as you required.

Regards,
  Steffen

RE: method level synchronization doesn't work

Posted by Malia Noori <mn...@blueridgenetworks.com>.
Thank you for replying, but I would appreciate it if you could expand on
your explanation.  Here is my original post:

Actually, the data that I am modifying requires a transaction and
> synchronization.  It increments a counter stored in the database.  So, I
> have to do a select to get the current value, increment the counter, and
> then insert the new value.  So if two threads are accessing it at the same
> time, the counter will not be properly incremented.  What's puzzling is
that
> method level synchronization does not work while synchronizing on a block
of
> code inside a method works. 
>

Regardless of what persistence mechanism I use (i.e. Hibernate, EJB, etc), I
have to synchronize on the method that increments the counter stored in the
database.  I can't avoid this due to business logic.  Please let me know if
there is alternative to synchronization.   

Thanks,
Malia


-----Original Message-----
From: QM [mailto:qm300@brandxdev.net] 
Sent: Thursday, September 30, 2004 3:01 PM
To: Tomcat Users List
Subject: Re: method level synchronization doesn't work

On Thu, Sep 30, 2004 at 01:14:26PM -0400, Malia Noori wrote:
: I thought Tomcat instantiates only 1 instance of JSP servlet to handle all
: requests.  Isn't that the reason why it's not suggested to have instance
: variables?

Close, but not quite. ;)

The servlet spec permits a container to create as many instances of a
servlet class as it sees fit.  The same goes for creating and destroying
a servlet object several times over a context (webapp) lifetime.

That said, a servlet's main methods -- doGet(), doPost(), service() --
may be called concurrently for the same servlet object.  These methods
are thus meant to be thread-safe, and as such they shouldn't modify
object instance variables.  (It's fine to call such variables, as long
as they are read-only or at least treated as such.)


: If it is the case that 2 instance of JSP servlets gets created
: to handle each request, what's the best object to synchronize on?

I missed your original post, so I can say only this: when you find
yourself painted into a corner, it's time to revisit the design stage.

-QM

-- 

software  -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by QM <qm...@brandxdev.net>.
On Thu, Sep 30, 2004 at 01:14:26PM -0400, Malia Noori wrote:
: I thought Tomcat instantiates only 1 instance of JSP servlet to handle all
: requests.  Isn't that the reason why it's not suggested to have instance
: variables?

Close, but not quite. ;)

The servlet spec permits a container to create as many instances of a
servlet class as it sees fit.  The same goes for creating and destroying
a servlet object several times over a context (webapp) lifetime.

That said, a servlet's main methods -- doGet(), doPost(), service() --
may be called concurrently for the same servlet object.  These methods
are thus meant to be thread-safe, and as such they shouldn't modify
object instance variables.  (It's fine to call such variables, as long
as they are read-only or at least treated as such.)


: If it is the case that 2 instance of JSP servlets gets created
: to handle each request, what's the best object to synchronize on?

I missed your original post, so I can say only this: when you find
yourself painted into a corner, it's time to revisit the design stage.

-QM

-- 

software  -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: method level synchronization doesn't work

Posted by Malia Noori <mn...@blueridgenetworks.com>.
I thought Tomcat instantiates only 1 instance of JSP servlet to handle all
requests.  Isn't that the reason why it's not suggested to have instance
variables?  If it is the case that 2 instance of JSP servlets gets created
to handle each request, what's the best object to synchronize on?
Synchronizing on empty string is not the best option because any other block
of code using an empty string will cause thread contention.  Thank you for
your help!

-Malia 

-----Original Message-----
From: Jon Wingfield [mailto:jon.wingfield@mkodo.com] 
Sent: Thursday, September 30, 2004 11:04 AM
To: Tomcat Users List
Subject: Re: method level synchronization doesn't work

I'd be surprised if synchronization was broken. Given one assumption I 
think I can explain what you are seeing.

Assumption: The two concurrent requests are handled by two different 
instances of a jsp servlet.

If there are two instances then each request will be able to 
sucsessfully obtain the monitor when calling the synchronized methods.

So, why does using a synchronized block work? It works because you are 
synchronizing on a literal string. Literal strings get interned into a 
pool of objects by the jvm. So the mutex reference actually points to 
the same piece of memory for both instances of your jsp. Hence the 
synchronization works.

Jon

Malia Noori wrote:

> Actually, the data that I am modifying requires a transaction and
> synchronization.  It increments a counter stored in the database.  So, I
> have to do a select to get the current value, increment the counter, and
> then insert the new value.  So if two threads are accessing it at the same
> time, the counter will not be properly incremented.  What's puzzling is
that
> method level synchronization does not work while synchronizing on a block
of
> code inside a method works. 
> 
> Thanks,
> Malia
> 
> -----Original Message-----
> From: Peter Lin [mailto:woolfel@gmail.com] 
> Sent: Thursday, September 30, 2004 10:26 AM
> To: Tomcat Users List; mnoori@blueridgenetworks.com
> Subject: Re: method level synchronization doesn't work
> 
> am I missing something, but looks like you're trying to build some
> kind of web cache. why not use Hibernate or something that already
> does caching for you instead?
> 
> the only time I can see a need to sync, is if the request contains
> data that requires a transaction. Which in that case, you're better
> off doing an insert, then a second select query with the same
> connection.
> 
> or is the scenario a distributed objects setup?
> 
> peter
> 
> 
> On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori
> <mn...@blueridgenetworks.com> wrote:
> 
>>Hi,
>>
>>I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC.
> 
> I
> 
>>have a JSP that calls a web bean.  This web bean has a section of code
> 
> that
> 
>>I want to synchronize, but when I synchronize the method, Tomcat doesn't
>>actually synchronize the threads.  (I tried this by having 2 users access
> 
> my
> 
>>JSP at the same time).  When I synchronize the code in the method by using
> 
> a
> 
>>mutex, it works.
>>
>>So, doing this doesn't work:
>>
>>public synchronized void amethod()
>>
>>{
>>
>> //some code that access the database and needs to be synchronized
>>
>>}
>>
>>But doing this works:
>>
>>public void amethod()
>>
>>{
>>
>>String mutex = "";
>>
>>synchronize(mutex)
>>
>>{
>>
>> //some code that access the database and needs to be synchronized
>>
>>}
>>
>>}
>>
>>Why does synchronization on the method doesn't work?  Does Tomcat not
> 
> allow
> 
>>locking of object caused by method level synchronization?  Any help will
> 
> be
> 
>>appreciated.
>>
>>Thanks,
>>
>>Malia
>>



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by Jon Wingfield <jo...@mkodo.com>.
I'd be surprised if synchronization was broken. Given one assumption I 
think I can explain what you are seeing.

Assumption: The two concurrent requests are handled by two different 
instances of a jsp servlet.

If there are two instances then each request will be able to 
sucsessfully obtain the monitor when calling the synchronized methods.

So, why does using a synchronized block work? It works because you are 
synchronizing on a literal string. Literal strings get interned into a 
pool of objects by the jvm. So the mutex reference actually points to 
the same piece of memory for both instances of your jsp. Hence the 
synchronization works.

Jon

Malia Noori wrote:

> Actually, the data that I am modifying requires a transaction and
> synchronization.  It increments a counter stored in the database.  So, I
> have to do a select to get the current value, increment the counter, and
> then insert the new value.  So if two threads are accessing it at the same
> time, the counter will not be properly incremented.  What's puzzling is that
> method level synchronization does not work while synchronizing on a block of
> code inside a method works. 
> 
> Thanks,
> Malia
> 
> -----Original Message-----
> From: Peter Lin [mailto:woolfel@gmail.com] 
> Sent: Thursday, September 30, 2004 10:26 AM
> To: Tomcat Users List; mnoori@blueridgenetworks.com
> Subject: Re: method level synchronization doesn't work
> 
> am I missing something, but looks like you're trying to build some
> kind of web cache. why not use Hibernate or something that already
> does caching for you instead?
> 
> the only time I can see a need to sync, is if the request contains
> data that requires a transaction. Which in that case, you're better
> off doing an insert, then a second select query with the same
> connection.
> 
> or is the scenario a distributed objects setup?
> 
> peter
> 
> 
> On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori
> <mn...@blueridgenetworks.com> wrote:
> 
>>Hi,
>>
>>I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC.
> 
> I
> 
>>have a JSP that calls a web bean.  This web bean has a section of code
> 
> that
> 
>>I want to synchronize, but when I synchronize the method, Tomcat doesn't
>>actually synchronize the threads.  (I tried this by having 2 users access
> 
> my
> 
>>JSP at the same time).  When I synchronize the code in the method by using
> 
> a
> 
>>mutex, it works.
>>
>>So, doing this doesn't work:
>>
>>public synchronized void amethod()
>>
>>{
>>
>> //some code that access the database and needs to be synchronized
>>
>>}
>>
>>But doing this works:
>>
>>public void amethod()
>>
>>{
>>
>>String mutex = "";
>>
>>synchronize(mutex)
>>
>>{
>>
>> //some code that access the database and needs to be synchronized
>>
>>}
>>
>>}
>>
>>Why does synchronization on the method doesn't work?  Does Tomcat not
> 
> allow
> 
>>locking of object caused by method level synchronization?  Any help will
> 
> be
> 
>>appreciated.
>>
>>Thanks,
>>
>>Malia
>>



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


RE: method level synchronization doesn't work

Posted by Malia Noori <mn...@blueridgenetworks.com>.
Actually, the data that I am modifying requires a transaction and
synchronization.  It increments a counter stored in the database.  So, I
have to do a select to get the current value, increment the counter, and
then insert the new value.  So if two threads are accessing it at the same
time, the counter will not be properly incremented.  What's puzzling is that
method level synchronization does not work while synchronizing on a block of
code inside a method works. 

Thanks,
Malia

-----Original Message-----
From: Peter Lin [mailto:woolfel@gmail.com] 
Sent: Thursday, September 30, 2004 10:26 AM
To: Tomcat Users List; mnoori@blueridgenetworks.com
Subject: Re: method level synchronization doesn't work

am I missing something, but looks like you're trying to build some
kind of web cache. why not use Hibernate or something that already
does caching for you instead?

the only time I can see a need to sync, is if the request contains
data that requires a transaction. Which in that case, you're better
off doing an insert, then a second select query with the same
connection.

or is the scenario a distributed objects setup?

peter


On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori
<mn...@blueridgenetworks.com> wrote:
> Hi,
> 
> I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC.
I
> have a JSP that calls a web bean.  This web bean has a section of code
that
> I want to synchronize, but when I synchronize the method, Tomcat doesn't
> actually synchronize the threads.  (I tried this by having 2 users access
my
> JSP at the same time).  When I synchronize the code in the method by using
a
> mutex, it works.
> 
> So, doing this doesn't work:
> 
> public synchronized void amethod()
> 
> {
> 
>  //some code that access the database and needs to be synchronized
> 
> }
> 
> But doing this works:
> 
> public void amethod()
> 
> {
> 
> String mutex = "";
> 
> synchronize(mutex)
> 
> {
> 
>  //some code that access the database and needs to be synchronized
> 
> }
> 
> }
> 
> Why does synchronization on the method doesn't work?  Does Tomcat not
allow
> locking of object caused by method level synchronization?  Any help will
be
> appreciated.
> 
> Thanks,
> 
> Malia
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: method level synchronization doesn't work

Posted by Peter Lin <wo...@gmail.com>.
am I missing something, but looks like you're trying to build some
kind of web cache. why not use Hibernate or something that already
does caching for you instead?

the only time I can see a need to sync, is if the request contains
data that requires a transaction. Which in that case, you're better
off doing an insert, then a second select query with the same
connection.

or is the scenario a distributed objects setup?

peter


On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori
<mn...@blueridgenetworks.com> wrote:
> Hi,
> 
> I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC.  I
> have a JSP that calls a web bean.  This web bean has a section of code that
> I want to synchronize, but when I synchronize the method, Tomcat doesn't
> actually synchronize the threads.  (I tried this by having 2 users access my
> JSP at the same time).  When I synchronize the code in the method by using a
> mutex, it works.
> 
> So, doing this doesn't work:
> 
> public synchronized void amethod()
> 
> {
> 
>  //some code that access the database and needs to be synchronized
> 
> }
> 
> But doing this works:
> 
> public void amethod()
> 
> {
> 
> String mutex = "";
> 
> synchronize(mutex)
> 
> {
> 
>  //some code that access the database and needs to be synchronized
> 
> }
> 
> }
> 
> Why does synchronization on the method doesn't work?  Does Tomcat not allow
> locking of object caused by method level synchronization?  Any help will be
> appreciated.
> 
> Thanks,
> 
> Malia
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org