• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Lock ?

 
Rajesh So
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please tell me why do we all require locks when the db is synchronized for add, delete, modify, find and seek. At any point of time, db is used for only one purpose. When there is a modification taking place in db,all other threads are waiting for thier operation. Hence, is'nt lock/unlock redundant? Has the assignment asked us to implement this for future requirements?
Please help me understand this, if I am in a wrong thinking.
Thanks in advance,
Rajesh
 
Calvin Gao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My experience is that a database might not be used only by one process(single or multi-thread) at a time. Multi-threading can be synchronized within one process, but sychronization can not do much across processes. You can think it either as for future requirements(if you have only one program running against the db) or as a good practice dealing with databases.
 
Rajesh So
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shall I lock records only for modify and deleting? I feel locking is not neccessary for reading, finding and adding. Am I right?
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I passed my scjd ~15 months ago. So I forget a lot about the assignment. I also struggle about lock/unlock, but eventually overcome it. I can still remember my turning point:
http://www.coderanch.com/t/179955/java-developer-SCJD/certification/Paul-Anil
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only need to lock a record when you modify it.
Deleting is not part of this assignment. - And yes, you need to lock here as well
Rene
 
Aruna Raghavan
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There were discussions on this topic as recently as couple of days ago. You need a write lock when you modify. add/delete are not part of this assignment. As to why even use a lock- think about the tiered architecture. You need protection at the business logic layer. I think Don helped me to grasp this one.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rene Larsen:
You only need to lock a record when you modify it.
Deleting is not part of this assignment. - And yes, you need to lock here as well
Rene

To my understanding, there are two kinds of locks here: the one you will implement and the built-in lock with db write operation.
For the first lock, the only function is to protect a common sense: when you see a ticket left for a flight, you can book it if you like (it is not absolutly necessary, there are pros and cons, but the spec seems require it).
For the second one, there is a list of fuctions: modify, write, .... (it is there already, so you don't need to worry about it).
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please tell me why do we all require locks when the db is synchronized for add, delete, modify, find and seek. At any point of time, db is used for only one purpose. When there is a modification taking place in db,all other threads are waiting for thier operation. Hence, is'nt lock/unlock redundant?

From the first (superficial) look, the lock/unlock methods are, indeed, redundant. However, if you think it through, the locks are neccessary to implement a business requirement that any particular client may not book more seats than available for the flight. Think of this scenario in this particular order:
Remote ClientA checks to see how many seats are available, -- there are 2 available
Remote ClientB checks to see how many seats are available, -- there are 2 available
Remote ClientA invokes the modify() method on Data and reduces the seat count in database to 0.
Remote ClientB invokes the modify() method on Data and reduces the seat count in database to -2 (oops!).
To solve this problem, you need some inter-client synchronization, i.e, a server side semaphore.
Eugene.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eugene Kononov:
[QB]Remote ClientA invokes the modify() method on Data and reduces the seat count in database to 0.
Remote ClientB invokes the modify() method on Data and reduces the seat count in database to -2 (oops!).QB]

Seat count may not drop below 0, depending on how people define the db file. ClientB will most likely end up booking nothing.
Image you are the ClientB, you just saw 2 tickets left for booking, now you end up empty-handed. How do you feel about the application? What are you going to do next time?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Liu:
To my understanding, there are two kinds of locks here: the one you will implement and the built-in lock with db write operation.

What build-in lock are you talking about?
The methods 'modify, delete...' and the database-file 'db.db' don't have any build-in lock mechanism.
Rene
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean the computer hard disk drive head.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aruna Raghavan:
I think Don helped me to grasp this one.

I am glad I can be of help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic