These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Cache troubles when using queries ?
PostPosted: Sun Oct 01, 2006 11:03 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 1:48 pm
Posts: 20
Hi!
I have the following code in a method :
Code:
session.beginTransaction();
List recherche = session.createCriteria(Histoire.class)
   .add(Restrictions.eq("membre",this.getIdMembre()))
   .list();
for(int i=0;i<recherche.size();i++){
   Histoire histoire = (Histoire) recherche.get(i);
   this.histoires.add(histoire);
   System.out.println(histoire.getTheme() + " " + histoire.getDetail());
return this.histoires;   


The problem is that when I call and recall this method within a session, the console displays a query from hibernate, but I alway get the same results, even if my table content has changed!

Would that be a cache problem? How can I solve this trouble?

Hibernate version: 3.1.3

Name and version of the database you are using: MySQL 4

The generated SQL (show_sql=true):
Code:
Hibernate: select this_.idHistoire as idHistoire3_0_, this_.Membre_idMembre as Membre2_3_0_ from Histoire this_ where this_.Membre_idMembre=?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 01, 2006 5:30 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
How is the table data changed? Is it done in the same session, if not the session cache would get out of sync with the database.

The query you see retrieves only the ID's from the database. Then the entities are looked up in the session cache by their ID.

You can call:
Code:
session.refresh(entity);

To synchronize entity with the database within the session.
Alternatively you can evict() and reload the entity or clear() the session.

Other possibilities:
- Tthe second level cache being out of sync
- Transaction isolation or isolation due to different db connections


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 01, 2006 5:32 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Are you actually commiting the transaction and closing the session? Changes in other transactions are not supposed to be visible in your current transaction. One of the design patterns in Hibernate is to use long running transactions that can encompass several http screens (a conversation). Maybe you are misusing this pattern here?

I hope this helps :)

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 9:40 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 1:48 pm
Posts: 20
I was modifing the DB in a completely different context (using PHPMyAdmin).

I am a little bit lost with the use of these "sessions". When should a session be open or closed?

I have fixed the problem opening a new session each time I need to do a query. But does comiting implies a closure of the session?
I am using Hibernate in a J2EE context (with JSF) and am getting lost!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 10:40 am 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Here is the snippet of code that you have to use. If working with Spring or other frameworks some of this may be executed in interceptors for you. For sure if YOUR code gets a session then YOUR code needs to close it. Otherwise you keep open lots of resources in the system.

// Non-managed environment idiom
Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();

// do some work
...

tx.commit();
}
catch (RuntimeException e) {
if (tx != null) tx.rollback();
throw e; // or display error message
}
finally {
sess.close();
}

The above is from here, please read :)
http://www.hibernate.org/hib_docs/v3/re ... basics-uow


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.