Hi Tiana,
[0]
I guess you also can use a "static field" to lock your code:
//in Static Initializing of your class
static java.lang.Object sync= new java.lang.Object();
...
...
public String assignNum(...){
synchronized(sync){
...
sync.notifyAll();
}
}
...
[1]
And I guess [0] is similar with "static method", but I don't know
how about the efficiency.
[2]
But if your Servlet engine will make more than one "independent instance"
of your class, perhaps [0] or [1] will not work well. I don't know if it is
possible,
but because from your email, your already "lock" the code, but you still
get a "cocurrent inserting/updating" of your database, so I just "imagine"
it is possible :-) So now is it possible to find a "global object within
the whole Servlet engine :-)" to "lock" your code ? :-)
1.99 cent provider -- Bo :-) :-)
Oct.05, 2000
Tiana Zhang wrote:
> Hello,
>
> The synchronized method I created in the servlet is going to increment a number
> in the database and display it in the browser. Because I don't want multiple
> user update the database at same time, I added synchronized keyword in the
> method.
>
> My question is: does synchronize guarantee that only one thread does the update
> database? Why after many times of testing multiple browsers, I still got same
> number on different threads which means access to the synchronized method is not
> exclusive?
>
> many thanks in advance.
>
> Tiana
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html