-->
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.  [ 6 posts ] 
Author Message
 Post subject: Clearing cache and NonUniqueObjectException
PostPosted: Wed Jul 28, 2004 1:03 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
Greetings.

I started a similar discussion long time ago and would like to summarize everything in a new thread.

My application is using a single session, which cache gets cleared every now and then to avoid performance problems.

Here is a scenario that results in NonUniqueObjectException to be thrown when object A is saved:

1. Retrieve object A from the database
2. ... Do some heavy db stuff without changing A ...
3. Clear session cache (therefore loose reference to A)
4. Retrieve object B, which references object A (causes A to be brought in the session cache again).
5. A.setProperty("foo")
6. Save A (Throws NonUniqueObjectException)

I have my own Broker and Transaction classes, which forward calls to the Hibernate classes. All my business queries and updates go through my two classes, so all updates are treated the same way.

I spent several hours reading this forum and trying all kinds of tricks, but to no vain. Could anyone please point me in the right direction.

Thanks.

Alec


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
My application is using a single session, which cache gets cleared every now and then to avoid performance problems.


This is anti-pattern, see "Session and Transaction scope" on the Wiki. Read Hibernate in Action for a very detailed discussion of application design.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 1:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually, there are certain, VERY specific use cases where a single long session is acceptable. Its arguably acceptable if you want desktop application style semantics. The application is then responsible for explicitly refresh()ing things.


In this case, you can use saveOrUpdateCopy() to avoid the NUOE.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 2:10 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
I did order "Hibernate in Action", but haven't received it yet. So, meanwhile I would have to use this forum :)

It is not necessary for me to use a very long session, so I agree with Gavin that I may need to close and open the session every now and then.

But, this conflicts with the architecture that I want to use for my business layer. I want all database requests to go through my Broker class, which will delegate them them to the Hibernate broker. So, my broker class should be the only class dealing with Hibernate sessions and transactions. This means that I have only two options:

1. Have ALL requests processed within one session: open a session when the broker is initialized and clear it's cache to avoid performance problems

2. Have EACH request processed between: open a session when the broker method is called, process request, and close the session before returning

I am currently using the first approach, which results in NUOE.

Both of these options are considered as anti-paterns in Wiki "Session and Transaction scope". It seems that if I want to group multiple DB requests in one session I would need to put the opening and closing session logic in my presentation layer, which I don't think is a good idea.


Should I just switch to the second option or there are other ways to solve this without putting the Hibernate session logic in the presentation layer?

Thanks.

Alec


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 3:25 pm 
Newbie

Joined: Thu Aug 19, 2004 7:57 am
Posts: 14
gavin wrote:
Actually, there are certain, VERY specific use cases where a single long session is acceptable. Its arguably acceptable if you want desktop application style semantics. The application is then responsible for explicitly refresh()ing things.


In this case, you can use saveOrUpdateCopy() to avoid the NUOE.


Gavin,

Do you have time too look at my issue? I think it's similair to this issue, but it's happening on a delete. Im using the ThreadLocal pattern from the documentation, as this will be a Tomcat hosted application. My issue is currently happening at the application facade level where I have all transactional scope.

Here's the original post.

http://forum.hibernate.org/viewtopic.php?t=933689

_________________
Andy Czerwonka


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 22, 2004 9:46 am 
Newbie

Joined: Thu Aug 19, 2004 7:57 am
Posts: 14
gavin wrote:
Actually, there are certain, VERY specific use cases where a single long session is acceptable. Its arguably acceptable if you want desktop application style semantics. The application is then responsible for explicitly refresh()ing things.


In this case, you can use saveOrUpdateCopy() to avoid the NUOE.


The problem with me is that I'm getting the exception on session.delete, not saveOrUpdate. Is there now a similair semantic?

_________________
Andy Czerwonka


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