• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can cache be loaded throught private constructor

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
either approach is good is good as long as it will guaranty single instance of cache...

thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic