-->
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.  [ 5 posts ] 
Author Message
 Post subject: Multiple SessionFactory
PostPosted: Wed Sep 22, 2004 9:35 am 
Newbie

Joined: Mon Sep 20, 2004 10:27 am
Posts: 7
Hibernate version:2.1.6

Hi Guys:

For my Stand alone application , I m trying to use Hibernate for persistence layer. Currently, the stand alone application is using direct JDBC for Data access. We are trying to change it to Hibernate. We are connecting to 4 different databases with different data modelling. I know that I need to configure multiple SessionFactory objects.

Is there a clean way of doing it.

Code:

public class FactoryDude {
         
        public static SessionFactory createDB1SessionFactory(){
                  ...
         }

         public static SessionFactory createDB2SessionFactory(){
                  ...
         }
}



In my DAO layer , instead of using SessionFactory could I use SessionManager interface .
Code:

public interface SessionManager {

       public Session getSession();

       public void removeSession();

}



The SessionManager implementation class will hold the Session in ThreadLocal.

This is where I am kind of stuck.

Lets Consider ,

UserDAO (needs to get data from DB1) and ItemDAO ( needs to get data from DB2). Both of them will having their own instance of SessionManager.

In this scenerio , how does ThreadLocal works. Will I be always getting DB1Session when called from UserDAO and DB2Session when called from ItemDAO?

I am sorry for this long message, I was trying to be as clear as possible.


Thank you,

_________________
Rgds,

--Siva Jagadeesan
Application Developer
ThoughtWorks, Inc


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 5:09 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Case a)
In one thread only on DB (with is SessionFactory) is accessed: You need only one ThreadLocal.

Case b)
In one thread all DBs (with their respective SessionFactorys) are accessed: You need a ThreadLocal per SessionFactory.

A ThreadLocal is kind of a singleton per Thread.

HTH
Ernst


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Thu Sep 23, 2004 9:41 am 
Newbie

Joined: Mon Sep 20, 2004 10:27 am
Posts: 7
Thanks Ernst

It makes sense to me now.

_________________
Rgds,

--Siva Jagadeesan
Application Developer
ThoughtWorks, Inc


Top
 Profile  
 
 Post subject: Multiple Session Factories
PostPosted: Mon Sep 27, 2004 12:32 pm 
Beginner
Beginner

Joined: Thu May 20, 2004 3:40 pm
Posts: 33
Hello!

We would also like to use Multiple Session Factories. I keep getting the following error:

Can you help?? What am I missing??

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at com.gs.aml.util.HibernateUtil.currentSession(HibernateUtil.java:78)
at com.gs.fw.gc.core.refs.DataSrcInstanceTB.main(DataSrcInstanceTB.java:115)

Here is the code:

Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet


if (s == null)
{
SessionFactory sf = (SessionFactory) new InitialContext().lookup("KJSSessionFactory");
s = sf.openSession();

}
return s;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 2:02 am 
Regular
Regular

Joined: Tue Dec 09, 2003 2:39 pm
Posts: 106
Location: Toronto, Canada
This is a JDNI configuration issue. It occurs generally when the InitialContext doesn't have default properties configured and the JNDI server properties may not have been configured properly.

Check the J2EE and your app server docs for more info.

Cheers,
Roll


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.