• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can cache be loaded throught private constructor

 
Margarita Babkova
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

i began actual work on my data class, and after some reading on this forum decided to use a singleton with lazy initialization; eager would be better but i am not sure how I will pass a database location.
I read it is possible by extending interface and provide database location through there...I am not sure i can use static block with hard coded value or have setter for dbPath, because i will still need class instance to use setter...
For now
I am making a call to load a cache in a private constructor with argument, while I saw that some people where doing inside getInstance(String dbPath) method.
Will be my static variable cache thread save if it created this way?
I guess i will have to add second getInstance method without argument - but i really do not like it - becomes confusing, and needs good explanation. plus, more validation for dbPath if one already used.
both getInstance are synchronized.



thank you in advance.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Margarita Babkova,

Welcome to CodeRanch!

Please, CarefullyChooseOneForum. This question has been asked by you in another forum (Threads and Synchronization) and also has been answered.

Please close one of the two (duplicate) topics.

Also, as suggested, please make the constructor as private (otherwise, you'll end up in creating different instances of FileAccessManager), and also try not to send dbPath as parameter.

By the way, why do you need FileAccessManager to be singleton? How about creating a single instance of that class? I mean, instead of having overhead of calling getInstance method, which is static synchronized, and having a private constructor and all, why not to create a single instance of FileAccessManager (make it final if you want) and keep on working on it?

Just my two cents.
 
Margarita Babkova
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I have second variation of my File Access class that does not use singleton. And should be instantiated only once
Big question will be my static HashMap loaded the moment server will start up and before any of the threads will attempt to use it?


thank you.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Margarita Babkova wrote:Big question will be my static HashMap loaded the moment server will start up and before any of the threads will attempt to use it?

Technically, no. The HashMap will be loaded during the first object of FileAccess class is created. But anyways, in your code, the HashMap is private, so, nobody from outside the class will be modifying it. The moment you create the first (and hopefully only) object of FileAccess, the HashMap will be ready with file data. And if everything goes smooth, your design will take care that FileAcess object will be initialized during (or before) first request to database(i.e. file).

However, your private method readDataBase need not be returning a Map. Just think about it : you are initializing a cache with empty HashMap, and in constructor, you are invoking a method which will return another HashMap. You are assigning this newly returned HashMap to cache. So, what happened to that empty HashMap?

How about a void method which will 'put' the values in FileAccess' cache itself?

I hope this helps.
 
Margarita Babkova
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i think i get it now...my FileAccess class does not really needs to be a singleton, and i do not need to mess around with two different getInstance(String path) and getInsance() methods.

in my default constructor i will just call private void readDB() method and it will take care of loading my static cache - because it was a whole point of this method.

thank you.
 
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
I had a similar issue "how to pass the path of the database file to my singleton" (which would result in 2 getInstance-methods), but I extended the given interface with an extra method to initialize the instance (and the cache) which must be called prior to any other method from the interface (otherwise a runtime exception is thrown). With this approach you can stick to the original singleton design pattern (with just 1 no-arg getInstance-method)
 
Margarita Babkova
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
either approach is good is good as long as it will guaranty single instance of cache...

thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic