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:[EMAIL PROTECTED] 
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:[EMAIL PROTECTED] 
> Sent: Thursday, September 30, 2004 10:26 AM
> To: Tomcat Users List; [EMAIL PROTECTED]
> 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
> <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to