-->
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.  [ 2 posts ] 
Author Message
 Post subject: Change database programmatically
PostPosted: Mon Jun 30, 2008 5:26 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
I'm writing an application were the user's data are stored in a database. One important function of the application is the ability to work with different databases, and hence change database inside the application.

How can I change the database NHibernate access inside my application?

There is no problem in updating the app.config file with the new NHibernate settings (i.e. the hibernate.connection.connection_string_name parameter). The new databases is loaded correctly if the application i restarted.

When I debug the process of closing sessions and factories, and the open them again I see that the variable Environment.Properties (in the Configuration class) is only read once when NHibernate is accessed for the first time. And the connection string information is located in this hash table.

Does anyone have any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 5:52 am 
Beginner
Beginner

Joined: Wed Nov 08, 2006 8:24 am
Posts: 26
Location: Gothenburg, Sweden
This seems to work:

Code:
public ISession Session
        {
            get
            {
                if (session == null)
                {
                    cfg = new NHibernate.Cfg.Configuration();

                    // Read mapping classes from assembly
                    cfg.AddAssembly(mappingFile);

                    // Create session factory and open session
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    AppSettingsSection nhibernateSection = config.GetSection("nhibernate") as AppSettingsSection;
                    cfg.SetProperty("hibernate.connection.connection_string_name", nhibernateSection.Settings["hibernate.connection.connection_string_name"].Value);

                    factory = cfg.BuildSessionFactory();
                    session = factory.OpenSession();
                    session.FlushMode = FlushMode.Commit;
                }
                else if (session.IsOpen == false)
                {
                    session = factory.OpenSession();
                }
                return session;
            }
            set
            {
                session = value;
            }
        }


I.e. setting the property explicitly. Doesn't look very nice though. Cannot NHibernate re-read the settings on its own?


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