-->
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.  [ 7 posts ] 
Author Message
 Post subject: Sessions in Fat Client Apps
PostPosted: Tue Jul 17, 2007 4:47 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Problems with Session and transaction handling?

Yes I do.

Read this: http://hibernate.org/42.html

Done. I understand the problem, but I don't know the solution. Therefore I hope somebody here can help me.

I am working on a Swing application, with simple JDBC transaction handling. Therefore I have my objects lying around for a long time, which causes problemes:

1. A user creates a new Object (a) and saves it. This obviously causes a commit and therefor the session and transaction ends. But the user has it still open in the editor. When he now selects all objects of the class from the database. The same object (in the sense of same PK-ID) does exist twice in the VM :(

I thought I could fix it by configuring a 2nd Level Cache. But it doesn't work. So question 1: Does a cached object acctually ensure object identity or does it only prevent repeatativ loads from the database?


2. If the user opens a object, then causes a commit in a different dialog. If I now try to access lazy loaded attributes I get a LazyInitializationException.
So question 2: Is there a way to 'reatach' the object to a new session, so the Lazy Load succeeds?

Thanks for any help.
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 6:06 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

use session.disconnect() and reconnect();


Amila

(Don'r fotget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 6:59 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Hi,
thanx for the quick reply. But I don't understand where to use disconnect/reconnect(and why it would help with my problem).

Could you please elaborate a little?

Thanx
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 7:29 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

after doing some transactions you can disconnect the session by using
session.disconnect(). it release db connection and all. At the time you want it again you can reconnect it and do transactions again.


I think this is what you want. I can't figureout exactly what you want. send some class models and db structure to further detail.


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 8:21 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
But when I do a commit on a transaction the session gets closed. So disconnect doesn't even work!?

Mappings and the like wouldn't help much since the problem is in the transaction handling, so let me try to explain it once more.

I have two classes which get mapped to the database: Family and Child
The class has a collection of Child'ren (mapped as lazy, one-to-many with hibernate, )
I have three dialogs in the application for displaying the objects of these classes:
- Family Editor: allows creation of Family objects, changing of Family attributes and saving them to the database
- Family Browser: Lists all Families (only surname, so Child objects aren't accessed
- Family Display: Displays a Family object including assosiated children
- At any given time we have one session for the application, which gets closed everytime a commit on the associated transaction occurs.

Now consider the following scenarios:

Scenario A
1 create a Family and store it in the database. The commit at the end closes the transaction as well as the Session. But the Family Object ist still displayed in the Editor
2 open up the Family Browser (a Session & Transaction is opened, all Family Objects get Fetched). So far we have tried either closing (a) or not closing the Transaction&Session after this (b)
3 Editing the still open Family in the editor and saving it again.
in case (a) this means we get a new Session+Transaction and everything is fine since the detached object gets reattached to the session on 'saveOrUpdate'.
in case (b) we get a Exception because the session alread contains an Object with the same ID (i.e. Primary Key)

Scenario B
1 open up the Family Browser (a Session & Transaction is opened, all Family Objects get Fetched). So far we have tried either closing (a) or not closing the Transaction&Session after this (b)
2 Opening the Family Display, will try to fetch the Child objects.
in case (a) this causes an Exception because the session is already closed.
in case (b) it works fine.

At this point one might say:'Where is the problem, go with case (b)' But the problem is any other commit caused from a different dialog would cause the session to close. And this brings us to cas (a).

So I guess the big question is: How do I do Session and Transaction handling with Hibernate in a Fat Client application, where I don't have well enclosed 'Units of work' but an arbitrary set of dialogs which might start a 'unit of work' at any time.

Could I assign a session to a dialog? But then how do I transfer a object from one dialog to the other, which then means transfering a Object from one session to another? (e.g. Opening the Family Details Form from the Family Browser)

kind regards
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 10:22 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

please see extended Session pattern for long Conversations

http://hibernate.org/43.html


Try to use second level caching with caching child collection

Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 3:28 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Ok
I think I'm starting to understand how this is supposed to work.

We essentially have a session per dialog now. Although sometimes a Dialog will start a fresh session, when it gets a new Object to work on.

Thanx for the help
Jens


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