Results 1 to 2 of 2

Thread: NHibernate connection strings in Shared hosted environments

  1. #1
    Join Date
    Feb 2008
    Posts
    1

    Default NHibernate connection strings in Shared hosted environments

    Hi,

    I am very new to Spring and NHibernate and have been tasked to investigate how to implement connection string management in a shared hosted environment. We have a ASP.NET 2.0 application that has a "system" database that stores user authentication/authorization data along with their system database connection strings. I am able to get and store this data in the current session using straight SQL against the "system" database. The application itself is written to utilize NHibernate so the "user_data" databases have the same schema for each client. The current web.config and Spring.DAO.config files have are setup as follows.

    web.config

    Code:
    <configSections>
    
    <section name="databaseSettings" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowLocation="false"/> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/> <section name="parsers" type="Spring.Context.Support.ConfigParsersSectionHandler, Spring.Core"/> <section name="typeAliases" type="Spring.Context.Support.TypeAliasesSectionHandler, Spring.Core"/> </sectionGroup>
    <databaseSettings> <add key="db.datasource" value="server"/> <add key="db.user" value="user"/> <add key="db.password" value="pass"/> <add key="db.database" value="database"/> </databaseSettings> <spring> <context> <resource uri="assembly://MyAssembly.Data.Config/Spring.DAO.xml"/> <resource uri="assembly://MyAssembly.Service.Config/Spring.Service.xml"/> <resource uri="assembly://MyAssembly.Presenter.Config/Spring.Presenter.xml"/> <resource uri="~/Config/Spring.Web.xml"/> </context> <parsers> <parser type="Spring.Data.DatabaseConfigParser, Spring.Data"/> <parser type="Spring.Transaction.Config.TxNamespaceParser"/> </parsers> <typeAliases> <alias name="ListPresenter" type="MyAssembly.NET.Presenter.ListPresenter&lt;&gt;, MyAssembly.NET"/> <alias name="FormPresenter" type="MyAssembly.NET.Presenter.FormPresenter&lt;&gt;, MyAssembly.NET"/> </typeAliases> </spring>
    Spring.DAO.xml

    Code:
    .
    .
    .
    <!-- This requires the containing .config file to have a datbaseSettings section -->
    <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
    	<property name="ConfigSections" value="databaseSettings"/>
    </object>
    	<db:dbProvider id="DbProvider" provider="SqlServer-2.0" 
        connectionString="Data Source=${db.datasource};Database=${db.database};User ID=${db.user};Password=${db.password};"/>
    	<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
    	<property name="DbProvider" ref="DbProvider"/>
    	<property name="MappingAssemblies">
    		<list>
    			<value>Intuit.IRES.Data</value>
    		</list>
    	</property>
    	<property name="HibernateProperties">
    		<dictionary>
    			<entry key="expiration" value="120"/>
    			<entry key="hibernate.adonet.batch_size" value="10"/>
    			<entry key="hibernate.cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache"/>
    			<entry key="hibernate.cache.use_query_cache" value="true"/>
    			<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
    			<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
    			<entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
    			<entry key="hibernate.current_session_context_class" value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate12"/>
    			<entry key="hibernate.show_sql" value="false"/>
    		</dictionary>
    	</property>
    </object>
    .
    .
    .

    I have tried to follow the thread of "How to change a connection string at runtime?" on this forum but not sure if I totally understand it in terms of how I could implement it in my project. I have also looked at the MultiDelegatingDbProvider documentation but there seems to be only one para on it in the documentation.

    Judging by how these two config files are set up, any tips would be appreciated. In the meantime, I'll keep messing around with the code samples I find to make sense of it all.

    Thank you very much in advance.

  2. #2
    Mark Pollack is offline Spring.NET Co-Lead Spring TeamSpring User
    Join Date
    Sep 2004
    Location
    New York, NY
    Posts
    1,683

    Default

    Hi,

    The approach in the the thread you mention does not take into account that you will need to have multiple NHibernate SessionFactories created, one for each of the databases, so that they can have independent NHibernate caches. To do this one would have to write a subclass of ISessionFactory that would return/create a NHibernate SessionFactory based on the value of some thread local information that identifies the user request before any data access operations take place. If you set the thread local information to some user id, then your custom ISessionFactory implementation will retrieve the appropriate ISessionFactory from a cache. If the instance is not present in the cache create a new ISessionFactory instance and then cache it for future use.

    You might find it useful to use LocalSessionFactoryObject as a starting point, adding a cache, and having the GetObject implementation do this caching logic. Note that LocalSessionFactoryObject's AfterPropertiesSet method is called when the object is first created.

    A better solution, if at all possible, would be to have these applications for different users run in different asp.net applications.

    Hope this helps,
    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •