-->
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.  [ 8 posts ] 
Author Message
 Post subject: Problem with Hbernate first level cache
PostPosted: Wed Apr 30, 2008 9:46 am 
Newbie

Joined: Wed Apr 30, 2008 1:11 am
Posts: 2
Hi all,

I've just had it with hibernate. I'm new to the tool but I'm faced with a problem that I thought was simple. I'm adding new users to the database in one application (web application) and just can't seem to see the newly added users in another application (james mail application).

Obviously there seems to be some caching mechanism (according to what I've read - the first-level cache). But how can I achieve what I really want which is to have one application immediately see new records added by another totally different application.

I've tried SessionFactory.evict() but it isn't giving me what I need. I've even tried a StatelessSession but it too isn't solving the problem.

I can only get to see the new users when I shutdown and restart which is clearly not desirable. I've tried reading what I think is every thing about caches but I must be missing something trivial here.

One thing is that I've tested with JDBC and it works perfect. The problem must be with the first level cache since I've disabled the second level cache.

What must I do?

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 30, 2008 10:00 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
it looks like u have one session opened during whole life of app?

sessionFactory.evict() doesnt help you, cause this evict object from 2dn lvl cache.

u can call session.evict() to evict object from 1st level cache and if u load object again by id u should get refreshed state. or u can call session.refresh() which refresh object according to state in db. but it will not refresh,evict collections and other association on object until u specify cacsade option for them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 30, 2008 11:21 am 
Newbie

Joined: Wed Apr 30, 2008 1:11 am
Posts: 2
I open a new session (using openSession()) for each of my methods and close it just before the method returns or in the finally{} block.

I'm pretty sure if SessionFactory.openSession() does what they say it does which is to 'Create database connection and open a Session on it' as the docs say then I must be opening a new connection each time I enter the methods.

The only global object in the application is the SessionFactory object.

Once again I'm trying to have one running application insert records and another totally separate running application be able to immediately see the new records.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 2:21 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
Do you flush() the session after inserting the objects?


Top
 Profile  
 
 Post subject: use flush() before commit()
PostPosted: Wed Sep 10, 2008 2:58 pm 
Newbie

Joined: Wed Sep 10, 2008 2:47 pm
Posts: 8
I had exactly the same problem, even tried the same strategies you did. I think the problem was flushing the session after the transaction commit

Try doing it in this order:


session.flush();
tx.commit();
session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 3:22 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I might also submit that perhaps you should be using getSession rather than openSession.

If you have written a framework to manage the new sessions created by openSession, then you're ok, but not too many people do this. Rather than opening a new session every time, getSession will return you the current session that is bound to the current thread.

Using openSession is typically the exception to the rule.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 4:54 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hmmm... I am wondering what is going on under the hood here. It might help to see some code (from the application that you have to restart) but I also have a theory that it could all be about transaction isolation levels.

Are you using transactions and have you made sure that you are ALWAYS either calling commit or rollback after every unit-of-work?

Are you using the default Hibernate connection pool?

Do you know what transaction isolation level your connections are using? Could it be set to REPEATABLE READ?

The reason for the questions is that we had a similar problem a long time ago during some prototype development and discovered that:

a) We didn't commit or rollback the transaction. The session itself was closed but the underlying connection was just returned to the connection pool.

b) The default transaction isolation level was REPEATABLE READ.

c) So, when a second session was created it was given the same connection as the first session had been used. And since the transaction was still open in a REPEATABLE READ mode we would always get the same result as we got from the first session.

The solution to this problem was

a) Always make sure that transactions are committed or rolled back.

b) Use a better connection pool, such as C3PO that usually commits/rollbacks transactions for you (at least I think it does because I remember that the problem got away when we started using it).

b) Set the transaction isolation level to something else, for example READ COMMITTED. This can be done in your hibernate configuration file. See 'hibernate.connection.isolation' at http://www.hibernate.org/hib_docs/v3/re ... ernatejdbc


Top
 Profile  
 
 Post subject: Same proble - C3PO solved it.
PostPosted: Wed Apr 01, 2009 4:29 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
I had the excact same problem. I was using the default Hibernate connection pool. However, as soon as i switched to C3PO connection pool, the problem was solved.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.