These two have the same effect:

public synchronized void foo() {
    ...
}

public void foo() {
    synchronize(this) {
        ...
    }
}

So synchronized methods in a class cannot be entered by multiple
threads calling the methods in one object. In your example, however,
it's the access_db call that should probably be synchronized. But
different objects can be running its code at the same time, which is
not a problem unless it refers to static variables somewhere.

Database "synchronization", aka locking, has nothing to do with
Java thread synchronization. Most JDBC drivers nowadays are thread-safe,
but concurrent access to (possibly changing) database tables is a
separate problem.

Pick up a copy of Doug Lea's "Concurrent Programming in Java", it's
worth it.

Rod McChesney, Korobra


Xizhen Wang wrote:
>
> Hi! ALL, I have a question regarding synchronization in Java when I
> write servlet.
>
> public synchronized void a(){
> String table_name = "user";
> access_db(table_name); //access table "user" in database DB
> }
>
> public synchronized void b(){
> String table_name = "user";
> access_db(table_name); //access table "user" in database DB
> }
>
> private void access_db(String db_table_name) {
> ......
> }
>
> I think method a and b can go into access_db at the same time since they
> are 2 distinct methods. right? so, that means they are not really
> synchronized at the program level. So, I am wondering if the database
> (Oracle for example) can synchronized the accesses itself automatically.
> if so, can the database administrator control the level of
> synchronization (database, table or the record level)? Also, do method a
> and b really need to be synchronized if the database can synchronize
> itself?
>
> I am puzzled.. Can anyone help?
>
> thanks!
>
> ___________________________________________________________________________
> 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

Reply via email to