-->
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.  [ 3 posts ] 
Author Message
 Post subject: unique object references in a multi threaded swing gui
PostPosted: Thu Oct 30, 2008 8:27 am 
Newbie

Joined: Thu Oct 30, 2008 8:11 am
Posts: 2
i am building a hibernmate application basted on swing. now i want, that all my objects, loaded by hibernate, are unique in the application. explaining:
a search gui-component is generating hibernate objects and distributing them to the thole application. from a secound search, i must have the same objects, when they are already in other gui components. so i can register listener on the objects, to notify all on changing some data.

i can do this with 1 session, always open, but then, the memory of my application is after a while a copy of the whole database.
and another problem is, that 2 asynchronius gui component can use the session parallel. this is not granded (not threadsafe).

what i want. is it possible?:
is it possible to use a 2nd level cache to hold all references to hibernated objects. so that when a session ist opened and a object is requestetd, i always get the same one (same object reference)?
now the 2nd level cache have to use softreferences or weakreferences to avoid a memory overflow. but with soft- and weakreferences it is alway guaranteed, the a object, what is in use by the gui, will not be garbage collected, and object only hold by the cache can be deleted.

that will solve all my problems, implementing a multithreaded swing gui with hibernate.

thanks a lot
stephan diehl


Top
 Profile  
 
 Post subject: ok, it is not working ... what can i do ?
PostPosted: Thu Oct 30, 2008 9:33 am 
Newbie

Joined: Thu Oct 30, 2008 8:11 am
Posts: 2
i have implemented a socound level ehcache in hibernate and test my solution.

i have created an object, saved it and then i load it with two differend entitymanagers. the cach statistic shows to me, that there where 2 cache hits and 0 cache miss. but the references of the objects are not the same.

i found why. the secound level cache onle saves the data of the object, not the object itself, like the id as the key and a string array as data.

what can i do to have always a unique object representing the same data.

thanks a log
stephan diehl


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2008 3:45 pm 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
First, are you sure an entity will be accessed by several threads? Just because you use Swing doesn't mean it is (there is only one event dispatcher thread ...). But if it is, you'll have to synchronize access to hibernate and hibernate-loaded entities.

In Hibernate, sessions are isolated, i.e. an entity belongs to at most one session. This corresponds to transactional isolation, which is often desired when having independent work threads.

Therefore, I see 3 options:

1) Use one session.
2) Use one session at a time, and introduce all loaded entities to the new session by reattaching them.
3) Use sessions however you like, and do the identity fixing yourself.

As you said, a session also keeps all entities it contains alive (it must, in order to detect changes that must be written to the database).

With 2) you have to keep track of loaded entities. A interceptor around your hibernate sessions can do that, for instance with a weak hash map.

3) Is somewhat ugly, but I happen to have written code for that once. You're in luck, it's even open source:

paper: http://el4j.sourceforge.net/docs/pdf/Id ... cement.pdf

impl: http://el4j.svn.sourceforge.net/viewvc/ ... Fixer.java
http://el4j.svn.sourceforge.net/viewvc/ ... Fixer.java


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