-->
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 Databases - Sample Code
PostPosted: Tue Jul 05, 2005 7:06 am 
Newbie

Joined: Tue Jan 06, 2004 8:06 am
Posts: 13
Hi all

I've written code catering to multiple database connection. Am posting it here for everyone future reference.

Few pre-processing notes:

[hbmMap] Due to the modular nature of my project, the hbm.xml are specified in a custom cfg file instead of normal hibernate.cfg.xml. I pre-processed hbm.xml into map of {DB_INSTANCE,hbm list} i.e.
{"default", ["User.hbm.xml","Role.hbm.xml"]}
{"report", ["Journal.hbm.xml"]}


[DB_INSTANCE.db.xml] Every db instance must have a DB_INSTANCE.db.xml in WEB-INF/classes path. To simplify the lookup, the DB_INSTANCE matches that of hbmMap. So for this example, I will have default.hbm.xml and report.hbm.xml in classes path. This db.xml is exact of hibernate.cfg.xml (renamed as *.cfg.xml is reserved for other setting).


I am still evaluating the following points:
- performance of session / sessionFactory maintained in map
- impact of map to ThreadLocal session

Appreciate if someone can help vet through and comment.



Code:
public class HibernateUtil
{
   // Logger initialization
   private static final Log LOG = LogFactory.getLog(HibernateUtil.class);

   // map that holds sessionFactory
   private static Map<String,SessionFactory> sessionFactory = new HashMap<String,SessionFactory>();
   
   // map that holds current open sessions
   public static final Map<String,ThreadLocal<Session>>session = new HashMap<String,ThreadLocal<Session>>();
   
   public static void initSession(Map<String,List<String>> hbmMap, ClassLoader mainClass)
      throws HibernateException, java.io.IOException {


      //======================================================================================
      // to handle multiple database connections,
      // 1. [instance].db.xml specify db properties
      // 2. mapping in *.cfg.xml specify which db the hbm belongs to via db attribute (default if not specified)
      // 3. CfgLoader generates hbmMap, where KEY=db name, and VALUE=hbm list
      // 4. Each db goes into seperate sessionFactory
      //======================================================================================


      // for each db instance in the configuration
      for(String instance : hbmMap.keySet() ) {

         try {
            if(session.get(instance) == null) {
               Configuration cfg = new Configuration();

               LOG.info("   : ---------- " + instance + " ----------");
         
               // load the hbmList
               for(String hbmFile : hbmMap.get(instance)) {
                  LOG.info("   : " + hbmFile);
                  cfg.addResource(hbmFile, mainClass);
               }

               // create session factory
               SessionFactory sf = cfg.configure("/" +instance+ ".db.xml").buildSessionFactory();
               sessionFactory.put(instance, sf);
               session.put(instance, new ThreadLocal<Session>());
           
            } else {
               LogFactory.getLog(HibernateUtil.class).info("Session [" +instance+ "] already initialized");
            }

         } catch (HibernateException ex) {
            LOG.error(ex);
            throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
         }
      }
   }

   //--------------------------------------------------------------------------
   // this open the default session
   //--------------------------------------------------------------------------
   public static Session currentSession() throws HibernateException {
      return currentSession("default");
   }


   //--------------------------------------------------------------------------
   // this close default session
   //--------------------------------------------------------------------------
   public static void closeSession() throws HibernateException {
      closeSession("default");
   }


   //--------------------------------------------------------------------------
   // open session from named instance
   //--------------------------------------------------------------------------
   public static Session currentSession(String instance) throws HibernateException {
      Session s = (Session) session.get(instance).get();
      // Open a new Session, if this Thread has none yet
      if (s == null) {
         s = sessionFactory.get(instance).openSession();
         session.get(instance).set(s);
      }
      return s;
   }


   //--------------------------------------------------------------------------
   // close session from named instance
   //--------------------------------------------------------------------------
   public static void closeSession(String instance) throws HibernateException {
      Session s = (Session) session.get(instance).get();
      session.get(instance).set(null);
      if (s != null)
         s.close();
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 4:32 pm 
Beginner
Beginner

Joined: Thu May 26, 2005 12:31 pm
Posts: 25
Thanks mate. will try this.


Top
 Profile  
 
 Post subject: Multiple Database Connection
PostPosted: Wed Nov 30, 2005 11:06 am 
Newbie

Joined: Wed Nov 30, 2005 6:45 am
Posts: 1
hi,

Can you provide some example on how it should work with Hibernate cfg ?
With your previous example, I roughly get some idea but still not sure on the implementation.

private static Map<String,SessionFactory> sessionFactory = new HashMap<String,SessionFactory>();
For the above, what is this <String,SessionFactory> means?


Thanks.


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.