A.J. Côté wrote:Hello,
Strange requirement. Are your tables read-only? If not you will need to hit the database for writes anyway.
Anyway, for read-only tables, I would probably just copy the db to an in-memory database or use a simple one file based database engine with the file kept on a RAM drive.
A.J. Côté wrote:
You should need to fine tune the size of the memory cache available to hibernate/Ehcache anyway so, by making it bigger or big enough, most of the cacheable data would be cached anyway. Why should you insist on having *everything* in the cache?
Claude Moore wrote:
Ankur Luthra wrote: I need to serve the requests using Cache and not hit Database at all.
May I ask you: why ? Is it by design ? I'm not an expert of Ehcache, but generally speaking the main purpose of a cache is NOT to keep all data in memory, only more accessed ones. Depending on how much is your database large, your approach may even never be feasible.
Chan Ag wrote:I'm signing off for the day cause I need to get back to my assignments. There are other nicer, wiser people around and I'm sure they'll help you. But you might want to give a thought to the things I said.
Take a break of at least some 15-20 minutes before posting again. :-) And in that period, reflect on your requirement. You might get a deeper understanding of it by doing that.
I'm sure you will be able to solve things yourself/or with minimal additional help.
Chan Ag wrote:
Init() can called one time by multiple Threads using different instances of Test Class. I get your point that in every Thread ,code is recreating the static HashMap.
So if I initialize the HashMap it in the beginning ,do you think issue will not occur.
i.e. something like, ................................
Like I said, forget about the ConcurrentModificationException now. There are many basic things that we may need fix here. They are easy to understand but let's do it one step at a time.
So like I said before, what is your requirement. Not how many threads, how many objects and stuff.. but what you want to do.
Like - 'I want to create a map that contains some key value pairs. This map needs to be accessible by all objects of my class. But I need to initialize the map only once. .. and stuff.. '
I'm not sure though if that is your requirement.
But, if you do this-
Init() can called one time by multiple Threads using different instances of Test Class.
Do you realize what you're doing when each of your thread is attempting to ( let's imagine there was no ConcurrentModificationException ) re-create a similar map in every invocation. Even if your init method was a static method, wouldn't that be a pointless thing to do?
So like I said, come up with reasons on why you're doing what you are doing.
Chan Ag wrote:How about we begin with you stating your requirement clearly so people might be able to suggest better.
Chan Ag wrote:
If you see the Test class ,the static Map is used inside synchronized instance method init().So if two threads try to call this method using the same object they can never enter together .One has to wait for other to leave and the only place this object is modified is inside this synchronized instance method.
So can we rule out the situation when this method is called using same instance of Test Class.?
The only case when this exception could occur is when two threads try to call init() method using different instances of Test .Right?
That would be right given what you said.
But I'm wondering why are you doing what you are doing? Did you intend to call the init method only once? If yes, you are clearly not doing that.
If you intend to call the init method multiple times, why are you recreating the static map from a static array?