In the common DBMS, the read/write operation has been implemented the Lock-system impl开发者_运维知识库icitly so that programmer does not necessarily handle this complex locking mechanism. But why when we write code on the application level, it is common that we still need to use some lock/synchronized function when doing write.
e.g.
public synchronized writetoDBMS(){ SQL statement writing to DBMS }
@Kit Ho: synchronized keyword before method name just ensures only single thread is executing at a time the enclosed code and also other synchronized methods of same object...so other operations can be read or write operation. Now why do you see synchronized keyword on some DB related methods? well for concurrent operations to be handled by Database, you rely isolation level.Read Database Isolation
Now let us say your DB isolation level is READ UNCOMMITTED and you have methods doing read and write to same table, then u definitely want to synchronize those methods.
Using synchronized
is to lock that particular method/code, so that the other threads will have to wait till the current thread has finished executing the synchronized block of code, meaning the synchronized code cannot be executed by more than one thread at any given instance.
This offers protection only within your application.
精彩评论