nhibernatedatabase-connectionsessionfactory

NHibernate: does building the session factory involve connecting to the database?


Following a migration of a database to a new server, my working copy cannot connect to the database anymore. But I'm experiencing something I didn't expect. Whenever a page in the application attempts to connect to this database, an error occurs during building of the session factory, i.e. before any attempt by the ISession to explicitly connect (e.g. to load an entity).

Is this right? Why would building the session factory require connecting to the database?


Solution

  • Building the session factory does not necessarily connect to the database. I wrote an application which instantiates the session factory to create an sql file from mapping files when there isn't a database at all.

    But you can advise the session factory to create the database by using a configuration like the following:

    <property name="hbm2ddl.auto">create</property>
    

    I wouldn't do it in production, but it may be useful for unit tests. Check your project for such a configuration.

    If there isn't such a configuration, check the error message if there is any useful information and take a look at the stack trace to see where it happens.