Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:47:59+00:00 2026-06-14T07:47:59+00:00

I have one difficult situation. As we know NH SessionFactory is a singleton in

  • 0

I have one difficult situation.

As we know NH SessionFactory is a singleton in our app.

“Heavyweight, thread-safe, and you only should ever have one per
database”.

And SessionFactory is created from Configuration().BuildSessionFactory() where Configuration() is my fluently configured database.

Here is the poblem.
I need to use different users for my connection string in an MVC app. This is needed to connect to the database by different users. When connection is ok, then I use NH to manipulate domain mapped objects.

Question here is how to pass different users to Configuration and use this SessionFactory in case when we need create Configuration first of all? Creating the SessionFactory again is a wrong way, right?

And this becomes more harder because I use Windsor container to register all objects before my MVC app’s run.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:48:00+00:00Added an answer on June 14, 2026 at 7:48 am

    It sounds like you are wanting to provide a different connection string for different users of your application.

    NHibernate has a feature in which you can provide your own DriverConnectionProvider which will allow you to ultimately be able to create custom connection strings on the fly by overriding the GetConnection() method.

    Example:

    class DynamicConnectionProvider : DriverConnectionProvider
    {
        public override IDbConnection GetConnection()
        {
            IDbConnection connection = Driver.CreateConnection();
    
            try
            {
                connection.ConnectionString = // Add your logic for dynamically creating a connection string
    
                connection.Open();
            }
            catch ( Exception e )
            {
                connection.Dispose();
    
                throw;
            }
    
            return connection;
        }
    }
    

    You can also take a look at the following links for examples:

    • https://stackoverflow.com/a/2927967/670028
    • http://nhforge.org/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx

    And yes, creating duplicate SessionFactories would be quite costly to your applications memory usage, would complicate your applications architecture, and is really unnecessary as there are other options such as the one I listed above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one user (only one, all the others are fine) trying to update
This is a difficult situation to explain, so bear with me. I have a
I'm having trouble figuring this one out. The situation is no difficult to imagine
I have run into a difficult situation. I do not want to do my
have one time consuming step that flattens a bunch of files. basically i'd like
I have one XML request which I need to modify (to XML) and then
I have one repo hosted at https://github.com/aikiframework/json . On my local copy, I added
I have one drop down list in my page, which contains two options. What
I have one problem with parsing *.docx document with OpenXML (C#). So, here's my
I have one model User and will be creating another one Category . Both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.