-->
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.  [ 11 posts ] 
Author Message
 Post subject: question about best practices
PostPosted: Thu Nov 17, 2005 7:15 pm 
Newbie

Joined: Mon Sep 12, 2005 3:58 pm
Posts: 8
I have a GUI application which uses Hibernate, on a single-database-per-user basis. The database is HSQLDB in file mode, and there is just a single one per user.

What is the best practice for Session management ? There are times when I dispatch a worker thread to perform some database related activity (import, delete) and want to get back a persisted object. In addition the primary GUI thread needs to be able to use a Session to display persisted objects.

What is the recommended way to demarcate Sessions ? I've had trouble where if I close a Session used to do something in a background thread, I get a lazy initialization error (session closed or no session) when I attempt to access a collection related to the object returned from the background thread. Is keeping a long session open appropriate ? What if I want to pass objects to and from the primary GUI thread to background threads but there are collections which are lazily initialized in those objects ?

Any pointers would be helpful..

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 8:11 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
the best for your case is 'one session for application'
open session on start and don't close while user exit application
you can use lazy loading with this


Top
 Profile  
 
 Post subject: regarding one-session-per-application approach
PostPosted: Thu Nov 17, 2005 8:27 pm 
Newbie

Joined: Mon Sep 12, 2005 3:58 pm
Posts: 8
Ok, but I have had issues with one-session-per-application.

Hibernate specifies that the Session object is not thread-safe, so I run into the danger of "object associated with 2 session" errors.

Unless you are proposing that I use a singleton Session ?

Can anyone comment about how that might be affected by multithreading in this type of scenario ?

Specifically, I have a Swing standalone application where each user gets his own (HSQLDB file-based) database.

But the application is obviously not single-threaded, background threads are opened up to handle db-related tasks and may share objects with the primary GUI thread.

So if I use a single Session object, how does it get used by multiple threads, and why does this not violate the fact that Sessions are not thread-safe ?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 8:34 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
yes, it is singleton session and you lock object when call query/dml in thread

you have to lock when you use multi session pattern


Top
 Profile  
 
 Post subject: ok getting somewhere
PostPosted: Thu Nov 17, 2005 9:05 pm 
Newbie

Joined: Mon Sep 12, 2005 3:58 pm
Posts: 8
What is DML ?

thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 9:20 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
data manipulation language (insert,update,delete)


Top
 Profile  
 
 Post subject: Right T-O
PostPosted: Thu Nov 17, 2005 9:25 pm 
Newbie

Joined: Mon Sep 12, 2005 3:58 pm
Posts: 8
Ok, right, as opposed to DDL. I thought perhaps you were referring to something hibernate-specific.

Now with regards to using a singleton Session... so it does get used by multiple threads, just not at the same time? Can you point me to official documentation which recommends this approach ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 9:37 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
no, i haven't documentation.I am independent and this is my opinion , only
There is thread about swing application on this forum and it advice 'one long session', too

you can't use session for call, too (ThreadLocal session) but it isn't important for one user and you have to lock object in multithread and it is ahrder work with lazy loading


Top
 Profile  
 
 Post subject: huh ?
PostPosted: Thu Nov 17, 2005 11:12 pm 
Newbie

Joined: Mon Sep 12, 2005 3:58 pm
Posts: 8
I'm afraid I didn't parse that last bit. Exactly what do you mean by lock object ? Can you provide sample code snippet to demonstrate what you mean ?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 11:59 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
write any documentation about java threading - you find locking concepts

for example, if you load hibernate list in more threads then you can choose any object (for example list) and lock it for change from another thread :
synchronized(list) {
// do query
}

you are sure now that two thread will not change list in one moment
this is too simple - find any documenation aboute threading
there is nice book Doug Lea Concurrent Programming in Java - see http://gee.cs.oswego.edu/dl/cpj/index.html


Top
 Profile  
 
 Post subject: got it
PostPosted: Fri Nov 18, 2005 3:01 pm 
Newbie

Joined: Mon Sep 12, 2005 3:58 pm
Posts: 8
ok thanks, I understand what you mean...


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