-->
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: Multiple Database Connections in hibernate.cfg.xml
PostPosted: Wed May 18, 2005 6:17 pm 
Newbie

Joined: Mon May 16, 2005 10:57 pm
Posts: 1
Location: Regina, Saskatchewan, Canada
I'm using Hibernate 3.0 with MySql 4.1.

I have created one session-factory in the hibernate.cfg.xml to connect to my database and my code is working. How does my code need to change if I add another session-factory/database connection? I currently am using a class called HibernateUtil (as per the examples) to access the Session, the code is below. So, how do things work if I have several session-factories defined?

Code:
public class HibernateUtil
{   
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
           System.out.println("ERROR INITIALIZING!");
           ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();           
            session.set(s);
        }
        return s;
    }

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

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 19, 2005 9:17 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
It's what's I want to know too ;-(

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 10:15 am 
Newbie

Joined: Wed Sep 28, 2005 4:07 am
Posts: 11
I think here is the answer:


http://forum.hibernate.org/viewtopic.php?t=936971&start=0&postdays=0&postorder=asc&highlight=multiple+sessionfactories[/url]

_________________
Lars Fiedler


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.