0

I'm developing a MVC4 application with NHibernate. In my computer it works great, but when I deploy my application on my shared hosting provider I noticed that NHibernate starts all over again every 40 minutes. NHibernate must start just once, when the App starts, it takes about 10 minutes to initialize.

After trying to solve the problem, I discovered that the problem is with the host server because its Idle Time-out is 40 minutes, and I can't change this time! How can I fix it ? I already tryed to use a Timer or another thread to perform some action and tryed this website too http://www.uptimerobot.com to ping my site every 5 minutes. But none of this seems to work..

Raiiy
  • 320
  • 2
  • 12
  • 1
    10 minutes? Ugh... Maybe save the raw NHibernate mappings that come out of Fluent NHibernate to a file as a cache and then load them on start if the file is available, instead of re-creating everything from scratch? – Patryk Ćwiek Sep 02 '13 at 20:18
  • Huum, How can I do that ? I didn't even know it was possible – Raiiy Sep 02 '13 at 20:20
  • 1
    Check out [this link](http://entron.wordpress.com/2010/01/06/optimizing-application-startup-time-with-fluent-nhibernate-and-unhaddins/) or [this SO question](http://stackoverflow.com/questions/2036270/generate-xml-mappings-from-fluent-nhibernate), unfortunately I don't have my own code available at this very moment. After you export the mappings, should be fairly easy to check if the file exists and load them. – Patryk Ćwiek Sep 02 '13 at 20:23
  • I serialized the Configuration object, but its still slow because the delay is in BuildSessionFactory method. Can I serialize somehow my Configuration so BuildSessionFactory loads faster ? Obs: I'm using XML mapping files – Raiiy Sep 03 '13 at 13:32

1 Answers1

2

I guess the time goes in initializing SessionFactory class, I would suggest you serialize it into a file when you first initialize it, then keep loading it and Deserialize it back on application start.

You'll have to clear the file, if you modify your db though.

Tamim Al Manaseer
  • 3,554
  • 3
  • 24
  • 33
  • I serialized the Configuration object, but its still slow because the delay is in BuildSessionFactory method. Can I serialize somehow my Configuration so BuildSessionFactory loads faster ? – Raiiy Sep 03 '13 at 13:27
  • Obs: I'm using XML mapping files – Raiiy Sep 03 '13 at 13:34
  • You can check this blog post for details: http://lucisferre.net/2009/06/18/speed-up-nhibernate-startup-with-object-serialization/ – Tamim Al Manaseer Sep 03 '13 at 14:40
  • It worked, Thank you!!! I was tryed this article, but it didnt worked because I was using a XML configuration file, not fluent config. Thank you! But do you why it is so much faster than config with XML file ? It was taking about 10 minutes, now its taking about 4 seconds! – Raiiy Sep 03 '13 at 16:38
  • 1
    I found the problem. I was using `update` in my XML configuration file. Thats why it was so slow to build section factory! I removed this property and it is so fast as fluent! – Raiiy Sep 03 '13 at 17:40