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.  [ 4 posts ] 
Author Message
 Post subject: Multiple SessionFactories in config
PostPosted: Fri Nov 14, 2008 4:30 am 
Newbie

Joined: Thu Oct 30, 2008 8:49 am
Posts: 11
Location: Goslar / Germany
Helo,

here my question ;)

i want to define multipe session factories in just one config.xml. How can i choose in later code which sessionFactory should be used for session creation?

Every connection will be used in different dataprovider binaries so the usage in the special data provider should look like this:

Code:
// dataprovider for the first db:

_configuration = (new Configuration()).Configure();

// this is what i want ... or something similar
_sessionFactory = _configuration.BuildSessionFactory("firstdb");

_session = _sessionFactory.GetSession();
...


The config file:

Code:
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="firstdb"> ...  </session-factory>
  <session-factory name="seconddb"> ...  </session-factory>
</hibernate-configuration>


Or should i use multiple config files with different names an load them
with Configuration.Configure(string fileName);? But this would mean many files in the output directory which i would like to avoid.

In addition i would like to hide the connection information from the user, but as i know compiling as embedded ressource doesnt satisfy nhibernate. Some ideas how to realize it?

I looked for a programmitically way to initialize the sessionFactory in the dataprovider class (connection string, dialect etc) but the reference guide just tells how to define the mappings etc by code.

On this way i would have every database configured in the special dataprovider and additionally hidden the connection information from the user.

Greetings
Adam


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2008 4:41 am 
Newbie

Joined: Thu Oct 30, 2008 8:49 am
Posts: 11
Location: Goslar / Germany
No useful advices?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2008 11:06 am 
Newbie

Joined: Thu Oct 30, 2008 8:49 am
Posts: 11
Location: Goslar / Germany
Ok. Last one for this topic.

Here comes the solution how to make it programmatically.

Exchange this ...
Code:
_configuration = (new Configuration()).Configure();


with this ...

Code:

IDictionary<string, string> properties = new Dictionary<string, string>();
         properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
         properties.Add("connection.driver_class", "NHibernate.Driver.OracleClientDriver");
         properties.Add("connection.connection_string",
                     "CONNECTION");
         properties.Add("show_sql", "false");
         properties.Add("prepare_sql", "false");
         properties.Add("dialect", "NHibernate.Dialect.Oracle9Dialect");
         properties.Add("use_outer_join", "false");
         properties.Add("query.substitutions", "true 1, false 0, yes 'Y', no 'N'");         
         _configuration .SetProperties(properties);

         _configuration .AddAssembly("BusinessObjectsAssembly");


Not much to say. I was blinded by the light ;)


Top
 Profile  
 
 Post subject: Re: Multiple SessionFactories in config
PostPosted: Wed Aug 26, 2009 7:19 pm 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
I was trying something similar that we had different schemas on the same database and I could get my customSessionFactory based on the key which is the schema name attached to a particular user's session as

Code:
public static SessionFactory getCustomSessionFactory(String URL,
         String userName, String password) {
      
      String parentURL = cfg.getProperty("hibernate.connection.url");
      String parentUserName = cfg.getProperty("hibernate.connection.username");
      String parentPassword = cfg.getProperty("hibernate.connection.password");
      
      Configuration customCfg = new Configuration();   // configuration must have been already loaded
      try {
         // Solution 1: works but you have to set the original values back in the parent OR else!!!
         //customCfg = cfg;
         
         // Solution 2: works but too slow as it re-creates and loads and tests every config file
         customCfg.configure(CONFIG_FILE_LOCATION);   // slow
         
         // Solution 3: does not work
         // customCfg = (Configuration) BeanUtils.cloneBean(cfg);    // does a shallow copy
         // BeanUtils.copyProperties(customCfg, cfg);
         // does not copy Indexed and mapped properties without getters and setters
      
         customCfg.setProperty("hibernate.connection.url", URL);
         customCfg.setProperty("hibernate.connection.username", userName);
         customCfg.setProperty("hibernate.connection.password", password);
      
         String sessionFactoryJndiName = customCfg
               .getProperty(Environment.SESSION_FACTORY_NAME);
         if (sessionFactoryJndiName != null) {
            customCfg.buildSessionFactory();
            return (SessionFactory) (new InitialContext())
                  .lookup(sessionFactoryJndiName);

         } else {
            return customCfg.buildSessionFactory();
         }

      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
      finally
      {
                         //  trying to save time by re-using the first configuration as all schemas had the same structure and
                      // and hence the same configuration file configuration would suffice
         // cfg.setProperty("hibernate.connection.url", parentURL);
         // cfg.setProperty("hibernate.connection.username", parentUserName);
         // cfg.setProperty("hibernate.connection.password", parentPassword);
      }
   }


Trying to save time by re-using the first configuration as all schemas had the same structure and hence the same configuration file configuration would suffice.

Let me know if this is not what you are looking for!

Cheers
Krishna


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