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

JDK 1.4 Beta: Problems on create

 
Ajay G
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JDK Beta 1.4, create function in the database should throw DuplicateKeyException if the given key already exists in the database. This pretty much means that you have to scan the entire database once to find if the given entry exists in the database. (assuming that we are not keeping an in-memory index structure for all the database entries). We also have to find and reuse a vacant slot in case we find any in middle of the database to insert the given record. We can also do this while doing the search. I maintain an array of empty slots using record number while I am searching through the database for the existence of the input data.
The question is that: if two different threads call create with the same data, we should allow only one thread to insert the data while the second one should throw a DuplicateKeyException. This is possible only when we write lock the entire database to do first write and then second write would fail if the database is writelocked. This should force the second thread to do a find on the database again which will naturally fail this time since the new record has been added to the database. On the other hand, this algorithms seems quite complicated from Developer point of view but does address a genuine problem. I wanted to poll people in this forum to see if they might have any opinions or thoughts to sure on how did they do this in their assignment.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm of the opinion that you're reading too much into it, and doing more then you need to.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In case of two creates with the same data at the same time, If u synchroinze ur create method only one can create where the other has to wait till the first complete its creation then if the second trys to create. ur create method will throw duplicatekey so whats there to worry.
-rameshkumar
 
Junilu Lacar
Bartender
Pie
Posts: 8834
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ajay G",
Your display name is still not in compliance with the JavaRanch Naming Policy. Basically, you will need to spell out what G stands for (from your login name, "Gupta", I suppose). Also, you need not re-register. Just click on this link to edit your profile and change your display name. I know it may seem fickle but those are the rules around these parts.
As for your poll, I am simply synchronizing the entire create method. This keeps threads from stepping on each other. Any concerns such as the impact on responsiveness will be documented in my choices.txt
As for keeping track of deleted records, I was thinking of keeping a list of them in memory but I didn't want to read the whole database at once. I was toying with the idea of having a very low priority thread go through the database and collect the information. The create() method would first check the in-memory list to see if there were any recycleable records before it tried to actually append a new record. The low priority thread would only run once and only when there were no other higher priority threads active. But then again, this may be going above and beyond what's required. Like Max said, don't try to read in too much into the requirements.
The main thing to remember is to keep a good balance between simplicity and fulfilling the requirements as stated. Also, document any concerns and considerations you made in making your design decisions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic