• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions to -- Ken Krebs

 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ken:
Thank you very much for your posting of your design. It help me a lot. I have read it several times but still have some questions, could you explain?
The ServicesImpl singleton is the only object that interacts with the Data singleton and hence the only direct user of the locking/unlocking API.
Q1:
Is it necessary to make both ServiceImpl and Data singleton? How about only make Data singleton?
-- I synchronized on my Contractor cache while iterating over it during search (find) operations.
-- I did not synchronize read/search and I did use a cache of Contractors.
Q2:
does synchronize Contractor cache has the same effect as to synchronize all read/write/search methods of Data class?
When you synchronize Contractor cache or synchronize all read/write methods in Data class, does it like lock the whole db file? It guarantees only one client can access the Contractor cache at one time (or only one method in Data class at one time can access the db file), so, are there any needs to use lock and unlock?
I used the synchronization wrappers provided by the Collections class to implement my collections.
Q3:
I don't understand, could you explain?
Q4:
When you use a cache of Contractors, when to save it back to the disk file?
What is the benefit to use a cache of Contractors instead of read / write db file directly?
Thaks.
Peter
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Q1:
Is it necessary to make both ServiceImpl and Data singleton? How about only make Data singleton?

No. I used singletons only because it was simpler.
Q2:
does synchronize Contractor cache has the same effect as to synchronize all read/write/search methods of Data class?
When you synchronize Contractor cache or synchronize all read/write methods in Data class, does it like lock the whole db file? It guarantees only one client can access the Contractor cache at one time (or only one method in Data class at one time can access the db file), so, are there any needs to use lock and unlock?

No.
No.
The synchoronization on the cache is used to prevent it from being corrupted by concurrent access while I am iterating over it.
quote: I used the synchronization wrappers provided by the Collections class to implement my collections.
Q3:
I don't understand, could you explain?

The Collections Framework provides a number of classes that you can use for different types of collections and maps. Most of these classes are not synchronized as there is a performance hit for the use of synchronization. The Collections class provides static utility methods that allow you to obtain synchronized versions of the classes. For more details, check the Javadoc.
Q4:
When you use a cache of Contractors, when to save it back to the disk file?
What is the benefit to use a cache of Contractors instead of read / write db file directly?

In my submission, I read the file once and do not keep the it open. I save only when the state has changed. The use of a cache:
1. provides better performance because the data is stored in memory and doesn't require physical disk access unless necessary
2. enhances reliability because the data is stored in memory and doesn't require physical disk access unless necessary
These results are not necessarily measureable in a lightly loaded system but would allow the application to scale better. The very small bit of additional complexity is justified by the benefits cited IMO.
[ January 10, 2004: Message edited by: Ken Krebs ]
 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Ken.
Peter
 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my submission, I read the file once and do not keep the it open. I save only when the state has changed.

Does "the state has changed" mean after write / modify the data? Whenever you modify, you wrtie to physical file? So your write methods write to both cache and physical file, do they?
Does your Data class(the class that implements Sun's interface, I don't know if your assignment use this name) throw IOExceptions or wraped IOExceptions?
Because you implements a full cache, I think it is possible to avoid class Data throw IOException. I want do this:
Vector v = new Vector() // or other collection class instead of Vector
//read physical file and add all records to the v
Data d = new Data(v);
Thus Data doesn't throw IOException because it just use cache. My Data class provides only one constructor that ask a Vector as parameter. Is this OK?
 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector v = new Vector() // or other collection class instead of Vector
//read physical file and add all records to the v
Data d = new Data(v);

I want to avoid Data class throw IOException, it seems impossible. The mechnism of write physical file must be inside the Data class and it must throw IOExeption. If I put write physical file's mechnism outside the Data class, When the Data class is tested by Sun, it will automatic fail, although the whole assignment works.
Comments?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

Thus Data doesn't throw IOException because it just use cache. My Data class provides only one constructor that ask a Vector as parameter. Is this OK?

The only danger is that if you had a power failure, you would loose all bookings. If the server hasn't been shut down for a week, that is all bookings for the week.
Most systems which have some form of caching will still write to the database in a background thread, and write log files so that no data is ever lost. But there is little value in writing log files in our case - there is not that much performance hit on writing the real file - and without it, the user will not get any notification if there is a problem.
I want to avoid Data class throw IOException, it seems impossible. The mechnism of write physical file must be inside the Data class and it must throw IOExeption. If I put write physical file's mechnism outside the Data class, When the Data class is tested by Sun, it will automatic fail, although the whole assignment works.

This has been discussed many times in this forum. Do a search for wrapping the IOException inside an extension of RuntimeException.
Regards, Andrew
[ January 11, 2004: Message edited by: Andrew Monkhouse ]
 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic