SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP 5, SCWCD 5, SCBCD 5
Jonathan Elkharrat wrote:i don't know exactly how you implemented your class, but i have a get instance that return a new instance
and everything run in this (single) instance..
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP 5, SCWCD 5, SCBCD 5
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP 5, SCWCD 5, SCBCD 5
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Sean Keane wrote: So your CRUD methods will be using the lock associated with the instance of the Data class, whereas the getInstance() method will be using the lock associated with the Class itself. Therefore two threads can simultanously be executing the getInstance() method and a CRUD method. This could leave the cache in an invalid state.
SCJP 5, SCWCD 5, SCBCD 5
Jonathan Elkharrat wrote:there's no reason for the cache to get corrupted.
either if you synchronize on this or on specific lock, as long as you have only one instance
of the DAO you'll have only one instance of lock and only one operation at a time...
why do you think it'll cause a problem??
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP 5, SCWCD 5, SCBCD 5
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP 5, SCWCD 5, SCBCD 5
Sean Keane wrote:So it is possible in my implementation for Thread-A to be executing one of the CRUD methods to be suspended, and in the meantime Thread-B could call getInstance(databaseLocation) causing the cache to be reloaded with possible different content...then Thread-A begins executing again unaware that the contents of the cache has changed underneath it.
Sean Keane wrote:
So they will return a uninitialised instance of the Data class from the getInstance() method - i.e. the cache is not loaded and any CRUD operations will fail.
They then use the instance method init() to initialised the instance of the Data class - i.e. load the records from the file into the cache.
This seems quite messy. I don't like the idea of returning an uninitialised instance of the Data class.
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs wrote:
Sean Keane wrote:So they will return a uninitialised instance of the Data class from the getInstance() method - i.e. the cache is not loaded and any CRUD operations will fail.
They then use the instance method init() to initialised the instance of the Data class - i.e. load the records from the file into the cache.
This seems quite messy. I don't like the idea of returning an uninitialised instance of the Data class.
It might be messy, but it doesn't violate the principle Each individual method should have high cohesion. The getInstance method returns an instance, the init-method initializes this instance. The name of the getInstance with dbLocation-parameter should actually be getAndInitializeInstance. Of course the init-method has to handle the same issue (do not initialize the instance more than once), but it has the benefit of being an instance method and thus other methods are blocked as long as this method is executing.
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Sean Keane wrote:So I cautiously confident that the code is thread-safe. What do you guys think?
Roel De Nijs wrote:
Jonathan Elkharrat wrote:if you hardcode the dbName then it would look like this:
If you want to pass the certification without resubmitting I would not hard-code the dbName![]()
SCJP 5, SCWCD 5, SCBCD 5
Sean Keane wrote:1) The API is intuitive
- (i.e. documenting the behaviour in JavaDoc does not constitute making non-intuitive code intuitive in my eyes)
Sean Keane wrote:That is why you have messy code in ever single instance method.
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Sean Keane wrote:Roel, out of my three points I listed you missed the one I said was the most important
The API being intuitive is subjective. But the most important point is the class invariant.
Sean Keane wrote:So apologies if I caused any offence.
Roel De Nijs wrote:
Sean Keane wrote:Roel, out of my three points I listed you missed the one I said was the most important
The API being intuitive is subjective. But the most important point is the class invariant.
I didn't miss that one, I agree with the explanation of the class variant to use the 2 getInstance-method approach (maybe I had to mention this too in my previous post). But if it's the most important point, you had to put that one on the 1st place (and maybe even leave out the API being intuitive because that's a subjective one).
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP 5, SCWCD 5, SCBCD 5
Jonathan Elkharrat wrote:what about this solution?
SCJP 5, SCWCD 5, SCBCD 5
Jonathan Elkharrat wrote:if he received the application "as is" and don't want to mess with the parameter he'll use default.
Roel De Nijs wrote:
Jonathan Elkharrat wrote:if he received the application "as is" and don't want to mess with the parameter he'll use default.
That still doesn't answer my question: where should he/she put the database file, so your Data class will be able to read it?
SCJP 5, SCWCD 5, SCBCD 5
Jonathan Elkharrat wrote:in the same folder, of course.
when running ajar file any filename specified is looked in the current directory, i think...
SCJP 5, SCWCD 5, SCBCD 5
Sean Keane wrote:that was pretty clear from "The key point here for me is the class invariant"
Sean Keane wrote:Or if you were implementing this again would you still go with returning an uninitialised instance of the Data class, and if so, why
?
Jonathan Elkharrat wrote:i'm wondering what happen if you run the appliction like that, does it read from the database bundled inside the jar?
SCJP 5, SCWCD 5, SCBCD 5
Roel De Nijs wrote:I have done some extra reading on the "class invariant" and one of the characteristics is "Each operation on a class can assume it will find the invariant true on entry and must leave the invariant true on exit." and is your exit-method (when you write everything back to the file) not breaking the invariance. I know my exit-method would, because if you call this-method you can again not call any other method than the init-method.
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Sean Keane wrote:What is your exit-method doing beyond writing the contents of the cache back to the file that only allows the init-method to be called afterwards? Why would you modify the cache in your exit method?
Roel De Nijs wrote:
Sean Keane wrote:What is your exit-method doing beyond writing the contents of the cache back to the file that only allows the init-method to be called afterwards? Why would you modify the cache in your exit method?
Like the name indicates it's the method you call when you don't need the dao anymore.
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
SCJP 5, SCWCD 5, SCBCD 5
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|