Essentially, you'll need to set up a collection in the web application that stores each configured session for NH. Then you'll instantiate a session for each database. Check out S#arp Archtiecture for a nice way to accomplish this. IN Billy's example, he uses a Dictionary of strings and ISession objects, then grabs the appropriate session object based on the key:
http://github.com/codai/Sharp-Architecture/tree/master/src/SharpArch/SharpArch.Data/NHibernate/ hth, -devon On Jan 19, 8:13 am, csetzkorn <[email protected]> wrote: > Dear all, > > I am a bit stuck and would like some help please. > > I am using DAOs in a web application (ASP.NET MVC within controllers) > served by two dao factories which access two separate databases. > > The DAOs use this class: > > public class NHibernateHelper > { > private static ISessionFactory sessionFactory; > > public static ISessionFactory SessionFactory > { > get > { > if (sessionFactory == null) > { > sessionFactory = new Configuration().Configure > ().AddAssembly("Bla").BuildSessionFactory(); > } > return sessionFactory; > } > } > > public static ISession OpenSession() > { > return SessionFactory.OpenSession(); > } > > public static ISession GetCurrentSession() > { > if (!CurrentSessionContext.HasBind(SessionFactory)) > { > CurrentSessionContext.Bind(SessionFactory.OpenSession > ()); > } > return SessionFactory.GetCurrentSession(); > } > > public static void DisposeSession() > { > var session = GetCurrentSession(); > session.Close(); > session.Dispose(); > } > > public static void BeginTransaction() > { > GetCurrentSession().BeginTransaction(); > } > > public static void CommitTransaction() > { > var session = GetCurrentSession(); > if (session.Transaction.IsActive) > session.Transaction.Commit(); > } > > public static void RollbackTransaction() > { > var session = GetCurrentSession(); > if (session.Transaction.IsActive) > session.Transaction.Rollback(); > } > } > > This is all no problem if I only use one database as I can just define > one hibernate-configuration section in my Web.config. I am just > wondering what I can do to define the connection strings for two or > more databases. I could use something like this when the singleton > sessionFactory is accessed: > > SessionFactory sf = new Configuration() > .Configure("/path/to/config.cfg.xml") > .BuildSessionFactory(); > > or this: > > cfg.SetProperty("connection.connection_string", > get_connection_string_from_other_web_config); > cfg.BuildSessionFactory(); > > However, this seems all a bit 'dirty' (I did not try it yet as well). > I would also like to use my DAO classes etc. in other applications > (e.g. console application). Any feedback would be very much > appreciated. Many thanks in advance. > > Best wishes, > > Christian
-- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
