-->
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.  [ 8 posts ] 
Author Message
 Post subject: Using nHibernate with two databases.
PostPosted: Fri Mar 10, 2006 12:22 pm 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
I need to get nHibernate configured so that it works tables in two different databases on two different servers.

From searching these forums i think the best way to do this is to have two session factories one for each database? Is this correct?

I am just a little unsure how i correctly configure and then use NHibernate to do this?


How do i correctly configure NHibernate, is it just a matter of putting some this like this in my web.config?

Code:
   <configSections>
      <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <section name="nhibernate2" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </configSections>
   <nhibernate>
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="hibernate.connection.connection_string" value="CONNECTION STRING ONE" />
   </nhibernate>
   
   <nhibernate2>
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
      <add key="hibernate.connection.connection_string" value="CONNECTION STRING TWO" />
   </nhibernate2>







I am currently getting the session factory like this, how but how do i adapt this to indicate which database i want the session factory for?

Code:
Configuration cfg = new Configuration();
cfg.AddAssembly("vcf");
instance = cfg.BuildSessionFactory();



And once this is sorted how do i indicate which entity classes belong to which database?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 1:18 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
I'm not sure if this will work, but it's an idea.

I don't use config files to setup NHibernate, I do it all from code. So, perhaps you can do the same thing, except setup two SessionFactories, one for each database.

As far as choosing the database for your classes, you need to put them in different assemblies. So if class1, 2 and 3 belong to databaseA, then put their hbm.xml files in MapAssemblyA. and if class 4, 5 and 6 belong to databaseB, then put them in MapAssemblyB. When you create your session factory for databaseA, only load MapAssemblyA, and do the same for assemblyB.

I think this might give you a start in the right direction. Let me know how it turns out, I might have a simliar need in the future.

Code:
  _nHConfiguration = new Configuration();

  _nHConfiguration.Properties.Add("hibernate.connection.provider",
      "NHibernate.Connection.DriverConnectionProvider");

  _nHConfiguration.Properties.Add("hibernate.dialect",
      "NHibernate.Dialect.Oracle9Dialect");

  _nHConfiguration.Properties.Add("hibernate.connection.driver_class",
      "NHibernate.Driver.OracleClientDriver");

  _nHConfiguration.Properties.Add("hibernate.connection.connection_string",
      string.Format(
      Core.Configuration.Database.ConnectionStringTemplate,
      Core.Configuration.Database.Name,
      Core.Configuration.Database.ConnectionUsername,
      Core.Configuration.Database.ConnectionPassword));

  _nHConfiguration.Properties.Add("hibernate.show_sql", "true");


  _nHConfiguration.AddAssembly("ChemRex.Application");
  _nHConfiguration.AddAssembly("ChemRex.Security");
  _nHConfiguration.AddAssembly("ChemRex.Core");

  _hNSessionFactory = _nHConfiguration.BuildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 3:56 pm 
Newbie

Joined: Thu Jan 26, 2006 4:22 pm
Posts: 6
If you need distributed transaction support then check out my reply to this post:

http://forum.hibernate.org/viewtopic.ph ... highlight=

/Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 7:31 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Pelton, Thanks i guess this make sence to have two assembly, but i dont fully understand your coded config, or how to apply that to two databases.


csstolle, i dont think so

I just want to simply talk to two databases in one application, nothing fancy, i dont want to do joins between them.....the entities on the different databases will not be related or associated to one another, and i dont want to update the two databases in anyone transaction.

Can anyone help with this, i need some noddy exmaples, ie a xml fragment for setting up the web config and an example of how to talk to the two different databases?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 10:46 am 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
If the schemas are exactly the same, you can use one SessionFactory and just change the connection string. Otherwise, you need to set up a SessionFactory for each database. So for each SF, you would have a different connection string, I'm guessing different classes and mappings, and retrieve a session for the DB you want by calling the related SF.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 10:54 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
The schemas are different, i all ready know i need two session factories with different connection strings im just not sure how to config this in the web.config, or how to then access the different session factories?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 3:38 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
I find it easier to use different config files (nhibernate config, not web.config) to build different configurations, which I then build my sessionfactory from. It's also possible to set up your Configuration completely in code with no XML, if you like. I don't have the docs or source handy right now, but there are methods on the Configuration class to do what you need.


Top
 Profile  
 
 Post subject: NHibernate Facility from Castle Open Source Project
PostPosted: Tue Mar 14, 2006 2:32 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
You can either use NHibernate facility from the Castle project (http://www.castleproject.org)or you can see the code and write something similar.
It lets you define multiple session factories and lets you assign aliases to them. You can then specify this alias when making subsequent method calls.


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