Hi,

As part of my system initialization i have to connect to a large
number of databases (over 20).
I created a class, factory of session factory, that according to a
unique key will return a session factory.

I use a default NHibernate config file, load it, and change all the
things according to each key in that collection (change the connection
string, change the mapping assemblies, cache region name etc.)

This initialization takes a huge amount of time (about 3.5 minutes to
connect to 10 databases).
After a small research, i found out that what's taking so long, is
when i add the mapping assemblies to the configuration (i have about
10 asseblies to each database, but they are the same for each
database... so maybe there's something i can do in here?!), and the
cfg.BuildSessionFactory() takes only 2-3 seconds.

Is there a way i can significantly reduce the time for adding the
assemblies to the configuration (cfg.AddAssembly(assembly.Name)) ?

BTW,

It doesn't matter if i dynamicly add the assemblies or add them as
part of the default configuration.

Thanks in advance,

Here is the code that creates one unit of work factory according to
the configuration it recieves as a parameter:

private ISessionFactory createUnitOfWorkFactory(UnitOfWorkKey
sessionFactoryKey, UnitOfWorkFactoryElement factoryElement, bool
isFromConfigurationFile)
        {
            try
            {
                ISessionFactory sessionFactory;

                NHibernateConfiguration cfg = new
NHibernateConfiguration();

                // Add the connection string according to the database
type specified in the key
                Dictionary<string, string> properties = new
Dictionary<string, string>();

                if (!File.Exists(factoryElement.FactoryConfigPath))
                {
                    throw new FileNotFoundException(
                         formatException("The config file at '" +
factoryElement.FactoryConfigPath + "' could not be found",
sessionFactoryKey));
                }

                string connectionString =
factoryElement.ConnectionString;

                // The configuration file doesn't contain concrete
connection but a connection name that need to be fetched
                // from configuration
                if (isFromConfigurationFile)
                {
                    // Fetch the connection string according to it's
name from the configuration settings
                    ConnectionStringSettings connectionSettings =
 
ConfigurationManager.ConnectionStrings[factoryElement.ConnectionString];

                    if (connectionSettings == null)
                    {
                        throw new HibernateException(formatException
                            (string.Format("Could not find named
connection string {0}", factoryElement.ConnectionString),
                             sessionFactoryKey));
                    }

                    connectionString =
connectionSettings.ConnectionString;
                }
                properties.Add(NHibernateEnvironment.ConnectionString,
connectionString);
 
properties.Add(NHibernateEnvironment.CacheRegionPrefix,
factoryElement.CacheRegionName);

                cfg.AddProperties(properties);

                // Add the assemblies of the entities according to the
configuration file
                cfg.Configure(factoryElement.FactoryConfigPath);
                addMappingAssemblies(ref cfg);
                //  Now that we have our key object, create a new
SessionFactory
                sessionFactory = cfg.BuildSessionFactory();

                if (sessionFactory == null)
                {
                    throw new
InvalidOperationException(formatException("cfg.BuildSessionFactory()
returned null.", sessionFactoryKey));
                }

                return sessionFactory;
            }
            catch (Exception exp)
            {
                Logger.Logger.WriteLog(Severity.Fatal, this, "Error
while trying to build unit of work factory", exp);
                return null;
            }
        }


Maorino.

-- 
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.

Reply via email to