• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FBNS: Why not synchronize the bookSeats method instead of using record locking ?

 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although nearly completed with my FBN assignment, some strange question occurred to me.
The only purpose of the locking mechanism AFAIK,is preventing that when client A is booking a flight, client B is booking some seats at the same flight concurrently. Though the Data methods are synchronized and therefore atomic, the bookSeats method is not:


It is very well possible that the bookSeats method is interrupted between the calls to getRecord and modify. That makes it possible that the record retrieved by the call to getRecord has sufficient seats available, but is modified by another client (thread) before the call to modify.
That makes the record locking necessary.

But... what if the bookSeats method itself will be made synchronized. Then it is an atomic action, which cannot be interrupted in the middle.
Would that make the locking mechanism redundant ? I am sure I must overlook someting, bt don't know what...

A difference is that making the bookSeats method synchronized corresponds to locking the whole database instead of a single record. But because I use a single Data instance for all clients and all its access methods are synchronized, the db is already locked completely when it is accessed, making it in essence a single-threaded database !

So what is the added value of the row locking mechanism ??? Don't we only profit from it when using a multithreaded database ?

Regards, Klaas
 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it has to do with the fact that synchronizing a method only works on the object level. But because each client has its own instance of the FlightDataAdapter class, object level synchronization is not enough.

In our case, however, it wold be sufficient because all FlightDataAdapter instances share the same Data instance. But this can of course change in the future, when a real DB will be used.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a graph of two clients executing concurrently. The first row is client 1, the second row is client 2.



Here is the first part of one possible scenario where two threads are executing bookSeats concurrently. Two clients are booking seats for different records. The database methods are synchronized.

When one thread executes file I/O, that thread is swapped out and another thread is swapped in.

I made a special assumption: when one thread returns from a synchronized database method and releases the lock, that thread is swapped out and a waiting thread is swapped in. Without this assumption, in this simple model, no thread executes while the other thread is waiting on I/O.

As you can see, there are two intervals where one thread is waiting on I/O and another thread is executing. The first such interval is very small because it is the beginning of the booking code. The second such interval is longer because it is the middle of the booking code.

Therefore, with two threads, it is possible to attain parallel activity, one thread is doing file i/o and the other thread is executing a part of bookSeats.
[ October 08, 2004: Message edited by: Marlene Miller ]
 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Marlene, although pretty complicated I think I can grab the essentials: Although the DB is single-threaded, a thread CAN give way to another thread when it is waiting for IO.
For me it makes no difference anymore, I just submitted my assignment and go for the essay exam this afternoon...
Regards Klaas
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Klaus. I am happy you understood what I was trying to say. I wish you the best results on your exam and assignment. Marlene
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic