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 6887185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:14+00:00 2026-05-27T05:51:14+00:00

I´m using NHibernate 3 on a new desktop project. I’ve read about serialize the

  • 0

I´m using NHibernate 3 on a new desktop project. I’ve read about serialize the Configuration Object. I did it but, sometimes it didn´t work. I’m started to think that it´s working only when i start up the app (serialize the configuration object), then close it, and startup again (few seconds later). But if i wait some minutes it didn´t work. I´m talking about 45+ seconds. Other Strange thing if that this just happen on a few machines not all. Some work as expected slow the first time, but faster the nexts startups.

Here is my configuration:

the class that create the configuration and session factory

class NHibernateUnitOfWorkFactory : IUnitOfWorkFactory
    {
        public NHibernateUnitOfWorkFactory(string nhibernateConfigPath)
        {
            String serializablefilePath = "configuration.serialized";
            try
            {
                if (IsConfigurationFileValid(serializablefilePath))
                {
                    Configuration = LoadConfigurationFromFile(serializablefilePath);
                }
                else
                {
                    // configuration is immutable, store last returned value
                    Configuration = new Configuration();
                    Configuration.Configure(nhibernateConfigPath);
                    Configuration.AddAssembly(typeof(ObjectEntity).Assembly);
                    SaveConfigurationToFile(serializablefilePath, Configuration);
                    new SchemaUpdate(Configuration).Execute(true, true);
                }
                //NHibernateSchemaExport();
            }
            catch (Exception ex)
            {
                throw new GenericRepositoryException(
                    string.Format("Error while configuring NHibernate: {0}.", ex.Message)
                    , ex
                    );
            }

            try
            {
                SessionFactory = Configuration.BuildSessionFactory();
            }
            catch (Exception ex)
            {
                throw new GenericRepositoryException(
                    string.Format("Error while building NH session factory: {0}.", ex.Message)
                    , ex
                    );
            }
        }
        private Boolean IsConfigurationFileValid(String ConfigFile)
        {
            //return File.Exists(ConfigFile);

            var ass = typeof(ObjectEntity).Assembly; //Assembly.GetCallingAssembly();
            if (ass.Location == null)
                return false;
            var configInfo = new FileInfo(ConfigFile);
            var assInfo = new FileInfo(ass.Location);
            if (configInfo.LastWriteTime < assInfo.LastWriteTime)
                return false;
            return true;
        }

        private static void SaveConfigurationToFile(String ConfigFile, Configuration configuration)
        {
            var file = File.Open(ConfigFile, FileMode.Create);
            var bf = new BinaryFormatter();
            bf.Serialize(file, configuration);
            file.Close();
        }

        private static Configuration LoadConfigurationFromFile(String ConfigFile)
        {
            try
            {
                var file = File.Open(ConfigFile, FileMode.Open);
                var bf = new BinaryFormatter();
                var config = bf.Deserialize(file) as Configuration;
                file.Close();
                return config;
            }
            catch (Exception)
            {
                return null;
            }

        }

        protected ISessionFactory SessionFactory { get; private set; }

        protected Configuration Configuration { get; private set; }

        /// <summary>
        /// Generates table structure inside specified database.
        /// </summary>
        public void NHibernateSchemaExport()
        {
            new SchemaExport(this.Configuration).Execute(false, true, false);
        }

        #region IUnitOfWorkFactory Members

        public IUnitOfWork BeginUnitOfWork()
        {
            return new NHibernateUnitOfWork(this.SessionFactory.OpenSession());
        }

        public void EndUnitOfWork(IUnitOfWork unitOfWork)
        {
            var nhUnitOfWork = unitOfWork as NHibernateUnitOfWork;
            if (unitOfWork != null)
            {
                unitOfWork.Dispose();
                unitOfWork = null;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (this.SessionFactory != null)
            {
                (this.SessionFactory as IDisposable).Dispose();
                this.SessionFactory = null;
                this.Configuration = null;
            }
        }

        #endregion
    }

xml configuration

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <reflection-optimizer use="true"/>
  <session-factory name="NHibernate.Test">
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <property name="connection.connection_string">
      User Id=xx;
      Password=xx;
      Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=
      (PROTOCOL=TCP)(HOST=192.168.100.xx)(PORT=1521)))
      (CONNECT_DATA=(SERVICE_NAME=XE)));
    </property>
    <property name="show_sql">false</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <property name="cache.use_query_cache">false</property>
    <property name="cache.use_second_level_cache">false</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  </session-factory>
</hibernate-configuration>

Any idea to improve the startup time?

  • 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-05-27T05:51:15+00:00Added an answer on May 27, 2026 at 5:51 am

    Serialization only speeds up building the Configuration (config = new Configuration().Add... vs config = bf.Deserialize(file)) not building the sessionfactory, which does a lot of other stuff like generating the proxies, connecting to the database and so on. It’s most likely a slow connection to the server or a slow server which is responsible for the delay.

    Also it would be better to use using to prevent leaking FileHandles when exceptions occure

    using (var file = File.Open(ConfigFile, FileMode.Open))
    {
        var bf = new BinaryFormatter();
        return bf.Deserialize(file) as Configuration;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm about to kick off a new project using NHibernate and ASP.Net MVC and
I am using NHibernate on a new ASP.NET project, and am running into what
I'm relatively new to NHibernate, but have been using it for the last few
I'm using nhibernate and it appears that changes to my new object are not
In this question I asked about NHibernate session lifetime. I'm using a desktop application,
I'm starting a new project and plan on using nhibernate. I'm struggling with whether
How do I add a new entity object using NHibernate whilst handling a PostUpdateEvent
I am relatively new to using Nhibernate but basic things already are working. Now
I am new to Nhibernate and I am using Nhibernate 2.1.0 RC1. In C#
I am new to nHibernate. I understand how to use mapping using Fluent nHibernate.

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.