Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Locking

 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went through quite a few posts on locking(Was surprised to find the volume). And i have narrowed down to a logic. I wanted to perfect it more. I wanted some suggestions and views about it.

I went through a few ideas and felt there were two major broadly speaking options
1) Lock individual records.
2) Lock the Entire Data class

My approach
I was thinking, I will make an abstract data class and a data class which is a singleton. The Data class will have read write access to the data. The Read only Data class will have only read access to it. That way, as many users want can have a go at the data, but if anyone wants to lock the data, they will have to obtain an exclusive lock. They can then update the db file and then return.

I know as a disadvantage, only one user will be able to write, but i am assuming, in any system, there will be waay more reads than writes. I was wondering if this is a good approach.



I have still not ruled out the idea of locking individual records. Just thinking of this as an alternative.


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Raje wrote:I will make an abstract data class and a data class which is a singleton. The Data class will have read write access to the data. The Read only Data class will have only read access to it.

Frankly, this make no sense at all. First you are talking about 2 data (with a lower case first letter) classes (one being abstract, one being a singleton). Then you switch to a Data class with read/write access and a read-only Data class with only read access. So you will have at least 2 instances of Data, so how can it be a singleton?

Gaurav Raje wrote:there will be waay more reads than writes

I agree there will be more reads than writes, certainly in the application you have to build now. But what if the application will have all the features implemented: create new records, update properties of existing ones,... So a lot more writes, although the number of reads will always be higher (but if it is waay more, I don't know)

I have no idea why you would lock an entire Data class for just updating a record. Just lock the record instead. If someone wants to update record 1, and another person wants to update record 5,... these updates should occur concurrently and someone wanting to update record 5 should not have to "wait" until someone else has finished updating another record.
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The abstract data class was the class AbstractData.
Both, Data as well as ReadData will inherit from abstractdata.
The create and update methods will be defined in the Data.java. While most of the common methods will be present in the abstractdata.(like find and get).
The requirements state that Data.java should inherit the DB interface, which it will. Since ReadData is another class, it need not inherit the interface, and hence is not contractually bound to implement create and update.

I was thinking of creating an abstract factory class which will get requests for data access. Depending on the type of request, this factory will return an object.
(Read or Data).



i agree..there makes no sense to lock the entire data class. However, while searching on the forums i encountered a solution where someone had suggested making the data class singleton. If the data class is a singleton, wouldnt that mean no two classes can modify the data at the same time... making it lock the data for individual writes?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Data class was (and still is) a singleton too. A class being a singleton is a short version of "I applied the singleton design pattern to my Data class". More info about the singleton design pattern can be found here. If you want to learn about other design patterns too, I recommend Head First Design Patterns (although I don't have read him yet, but it's on my desk and on my to do list )

So there will exist just 1 instance of my Data class EVER, just one instance. And this instance will be accessed by 1000s of threads to read, find, update,... records. So it's a lot easier than the alternative you want to use. Of course the Data class is thread-safe (otherwise it would corrupt the database file and I would have failed this certification). When 2 threads (representing 2 different client applications) want to update the same record: one thread will acquire the lock (and can proceed with updating this record), the other one will have to wait until the other thread has released its lock on that record (by calling the unlock-method). When 2 threads want to update different records: no problem at all, updates will occur concurrently, no thread has to wait on the other thread to release a lock on a record.

Hopefully that helps you to get a better understanding about "singleton"
 
Mxolisi Veco
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day.

i agree..there makes no sense to lock the entire data class. However, while searching on the forums i encountered a solution where someone had suggested making the data class singleton. If the data class is a singleton, wouldnt that mean no two classes can modify the data at the same time... making it lock the data for individual writes?


When you implement your Data class, there will be a RandomAccessFile object that will be used to access the database file. That is the object that people usually make it static and they also synchronize on that Object when reading/updating the database.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic