aspose file tools*
The moose likes Object Relational Mapping and the fly likes Problem with Cache (filter/Hibernate) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with Cache (filter/Hibernate)" Watch "Problem with Cache (filter/Hibernate)" New topic
Author

Problem with Cache (filter/Hibernate)

vijendra singh
Greenhorn

Joined: Oct 31, 2006
Posts: 17
Hi I don't know how the Hibernate Filter works and how does it maintain cache.
But i would like to maintain cache for my application.
In Hibernate when i am using two different application using same database.there when i insert some value in table using hibernate(here i think it maintains some internal cache.)Now when i enter 2nd value(its generating some internal id [reading from and/storing to cahe])
Now when in second application using hibernate again i try to insert some third value to same table i doesn't permit to insert this new value(As what we are thinking here internal hibernate cache is having some conflict with these two self generated ids.)

Can this kind of problem be resolved if i seriously want to use hiberate cache mechanism.

the other thing is that if i create my own cache mechanism (not by hiberanete) maintenane or say the speed will vary as every time updating
values will upload and next time will be able to read from cache.

For big application the time will increase.


Any suggestions.

Thanks.


Vijendra
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Moved to the ORM foum where Hibernate is discussed.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate has two caches. The "First Level Cache" is the Session cache and is implicit. Every object loaded into the same Session is in this cache. The "Second Level Cache" is a mechanism Hibernate provides to allow object to be loaded into another caching technology (such as JBoss Cache). You need to configure both the second level cache plus define in your mapping files which objects participate in this cache.


its generating some internal id

Hibernate, assuming your mapping files are correct, uses a records Primary Key to identify objects. It doesn't generate any other ID, internal or otherwise, to identify objects in the Session.


Now when in second application using hibernate again i try to insert some third value to same table i doesn't permit to insert this new value(As what we are thinking here internal hibernate cache is having some conflict with these two self generated ids.)

What is the Exception you see? When you say "self-generated" do you mean you are using the "assigned" primary key generation strategy and it is your own application that is providing the primary key value?


the other thing is that if i create my own cache mechanism (not by hiberanete) maintenane or say the speed will vary as every time updating
values will upload and next time will be able to read from cache.

Not sure I understand you here. Are you asking if using a cache will make the application run faster? If so, then the answer is usually yes, since you no longer have to go to the database for every object. However you increase the likelyhood of stale objects.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hibernate knows two different types of cache:
1st level
2nd level

The first level cache has the same scope as the Session (hibernate Session, nothing to do with HTTP Session). There is nothing you can do to turn it off, it is there (well you can flush the session and evict entities from cache but usualy there is no need to do this)

The second level cache needs to be configured manually and unless you have configured your entities (in the mapping files or trough annotations) to be cacheable hibernate will not touch them.

What do you mean by error ? Can you show some exceptions ? Some code ?

Im just guessing and giving you some clues...(need more information from you)

Since hibernate uses the entity ID (the primary key) to lookup/store entities in the cache i doubt your problem has anything to do with 2nd level cache and since it is on different JVMs it is surely not the 1st level cache.

If you you have several applications working on the same DB, then you must use a ID generation mechanism which will create unique IDs, no matter what application calls it (e.g. Sequence, not "just counting up")

If you want to use a cache from different applications, then this cache must work in a cluster (e.g. jBoss tree cache). Otherwise the cache becomes "out of sync" and you read stale data from the cache.

And i would not suggest you create your own caching stuff... it's more complicated than you might think.


pascal
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with Cache (filter/Hibernate)