-->
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.  [ 10 posts ] 
Author Message
 Post subject: How to have multiple sessions?
PostPosted: Mon Jan 24, 2005 9:58 am 
Newbie

Joined: Mon Jan 24, 2005 9:48 am
Posts: 6
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:2.1.7c

Name and version of the database you are using:MySQL

Hello,
I want to have multiple sessions with the same sessionFactory. In the documentation, they are no exemple when you want multiple sessions.


How do people deal with this type of situation?

My example with a simple session is (a ThreadLocal can contain only one object):

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Crée la SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problème de configuration : " + ex.getMessage(), ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Ouvre une nouvelle Session, si ce Thread n'en a aucune
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}

thanks.

_________________
Frouf


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 10:26 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 10:34 am 
Newbie

Joined: Mon Jan 24, 2005 9:48 am
Posts: 6
christian wrote:
What?

I would like to have several sessions for same sessionsFactory. The examples present in doc. make it possible to have only one session.How to have several sessions?

_________________
Frouf


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 10:36 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Don't use the examples then.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 10:48 am 
Newbie

Joined: Mon Jan 24, 2005 9:48 am
Posts: 6
christian wrote:
Don't use the examples then.

How to make? I am to oblige to build a table containing all my sessions. And in each method using a openSession(), to pass in parameter the session. No solutions exists in hibernate which makes it possible to contain the sessions of same a sessionFactory?

_________________
Frouf


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 10:51 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I really don't understand what you are trying to do. Maybe it helps if you reread what a Session and a SessionFactory is. You can call openSession() as many times as you like.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 11:04 am 
Newbie

Joined: Mon Jan 24, 2005 9:48 am
Posts: 6
christian wrote:
I really don't understand what you are trying to do. Maybe it helps if you reread what a Session and a SessionFactory is. You can call openSession() as many times as you like.


In the example of Doc., a session is stored in ThreadLocal. A HibernateUtil can thus create only one session.


Code:
...
public class HibernateUtil{

...

ThreadLocalSession;

...

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Ouvre une nouvelle Session, si ce Thread n'en a aucune
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}


Have an example managing several sessions?


Thanks. (Sorry ,i am so bad in english)

_________________
Frouf


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 11:06 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, but you can easily store a Map of Sessions in your ThreadLocal.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 11:32 am 
Newbie

Joined: Sat Jan 22, 2005 9:39 am
Posts: 10
What we want to know :

With HibernateUtil, there is one hibernate session per thread ( thanks
to ThreadLocal ).

What we want to know :
- Is it possible to have several users at the same time
in the same thread ?
- If yes, as the session manages a jdbc connection, can the jdbc
connection be shared by the different users ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 11:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
In a Servlet environment there is one thread per request.


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