-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: REFRESH
PostPosted: Mon Oct 04, 2004 9:01 am 
Newbie

Joined: Mon Oct 04, 2004 8:53 am
Posts: 10
Hibernate version: 2.1.6

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

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

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Hi,

An external aplication changes the DB and the hibernate don't refresh.
How should I set it ?

Thanks,

Glauber Andrade


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 10:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
are you using second level cache?
are you sure of your hibernate session management?
are you sure the changes made "outside" app scope are comitted?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 10:35 am 
Newbie

Joined: Mon Oct 04, 2004 8:53 am
Posts: 10
are you using second level cache?
Don't know how.

are you sure of your hibernate session management?
?!?!

are you sure the changes made "outside" app scope are comitted?
The changes are committed.

-----------------------------------------------------------------------

I am using this hibernate.cfg.xml
...
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">FALSE</property>
<property name="transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="hibernate.cache.provider_class">
net.sf.hibernate.cache.HashtableCacheProvider
</property>
...

And this code:

Session sess = currentSession();
List result = sess.find("FROM " + obj);
sess.flush();
sess.close();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 10:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what does currentSession() do?

are you using threadlocal pattern?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 10:49 am 
Newbie

Joined: Mon Oct 04, 2004 8:53 am
Posts: 10
I am using this class, with ThreadLocal. Is there any problem?

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Session sess = null;
private static Transaction tx = null;
private static final ThreadLocal session = new ThreadLocal();

static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problema na configura


Top
 Profile  
 
 Post subject: wough, again refresh...
PostPosted: Mon Oct 04, 2004 12:57 pm 
Newbie

Joined: Tue Aug 24, 2004 5:33 am
Posts: 8
Location: Russia
I have seen the same question many times. But there was no any reply. The only thing I saw were infinte questions instead any reply.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what do you mean serg?

the only way to have a strange behaviour like this, is to have "dirty" long sessions.

The other reason may also be that when the user find his bug, he doesn't tell the other what was the mistake.

I can remember a user that had exactly the same problem. And his bug was that he was updating one database but his web app and hibernate was connected to another database (really funny).

Serg, saying it, you mean that hibernate, can sometimes have a strange behaviour, i just cannot accept it, or i may not understand you so if you can reformulate....

Peace,

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: explanations
PostPosted: Mon Oct 04, 2004 3:40 pm 
Newbie

Joined: Tue Aug 24, 2004 5:33 am
Posts: 8
Location: Russia
Our group is just starting with Hibernate. We have some experiance (serious experiance) in databases and we have realized all operations using Hibernate mappings. For example, we are using one-to-many and many-to-many assotiations, we can read an object, we can create, update or delete it. But we can not refresh the list of objects.

We have the following situation. For example, we have two objects in the list (we got them from the Session.find method). Then we are goint to the IBExpert (Firebird database managment utility), changing one record and inserting a new one. Then we are clicking on commit button and returning to our application. There we are executing refresh action. Then we get the result. We can see three records. There are two old records and a new inserted. Well, we can see inserted. But we can not see any modifications in old records.

We are just starting and I have not jet explored the situation. The only result I have gotten the Hibernate gets all records but it check only primary keys. So, if it encounters a new key, it creates a new object. But old objects are simply checked out from the cache. I have digged throw the forums, but have not found any recipe.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 4:11 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
First, in your example, it is about concurrency and not hibernate related. Just replace you hibernate app by another bd client, you may have the same behaviour.
Second, you have to understand cache semantics, a cache is not aware of change made outside the app scope.

All this is expected behaviour.

1- Hibernate get 2 records in hibernate session A
2- other app (firebird admin) update one record and add a new one
3-1- if you continue working with session A (meaning you are using a long session which is rarely recommended), how, by magic it can detect changes made outside its application? are you serious?
3-2- if you work with another hibernate session, let's say session B and you are using second level cache, it can detect the new record but not the updated one, why? because YOU haven't told him that a change occured outside its app.

4- be sure to understand the goal of the hibernate session, it is a unit of work, has a short life and manage concurrency by applying the rules YOU defined in the mapping documents and via api (think about version, timestamp, lock mode, update....).

Guys, do you know there is a great book called hibernate in action, you need it, trust me.

[/b]

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 12:06 pm 
Newbie

Joined: Tue Aug 24, 2004 5:33 am
Posts: 8
Location: Russia
Thanks.

I suspected something like this, but despite any suspections I made a mistake. I treated the hibernate session as a database connection. So I tried to find a magic "refresh" method or an option.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 12:35 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
there is a refresh method session.refresh(myObj) which force another SQL SELECT but it is not called magically, you have to explicitly call it

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 12:47 pm 
Newbie

Joined: Mon Oct 04, 2004 8:53 am
Posts: 10
Anthony,

I know it is an expected behaviour, i just want to know how to refresh it.
How do I tell the hibernate that a change occured outside its app?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 1:10 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
session.refresh()

or session.clear(), you'll have an empty session

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: REFRESH
PostPosted: Fri Oct 22, 2004 6:17 am 
Newbie

Joined: Fri Oct 22, 2004 6:02 am
Posts: 2
Hi,

I have the same problem with the session.refresh() when I change the data form an external application.

I use Hibernate 2.1.6 and MySQL 4.1.6. and I don't use the second level of cache.


But fi I do an "empty" transaction before the refresh, everything is ok.

Before doing a refresh I have something like this:

Transaction t = session.beginTransaction();
t.commit();
session.refresh(My_Object);


Any ideea?

Thanks,
Ciprian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 7:02 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
well ... i discard the session after getting an exception and after performing one unit of work ...

1 - business-call : e.g. find(...) and do something with the result
2 - create session
3 - find and return objects
4 - discard session

so even if the same threads want's to execute the business-call a second or third time he will always do this on a new session. Like anthony said, a session has a short lifetime depending of one unit of work. (i trust him ;) )

I think you wouldn't get into troubles (i don't) with that recommended approach ... (refreshing the cache could be necessary sometimes, too).

gtx
curio


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.