nhibernateconnectionisession

NHibernate 2.1.2 connection open upon factory.OpenSession()?


When I open a session with

var session = factory.OpenSession();

and check session.Connection.State it is Open. The "Connection" is of type SqlConnection. This means that by creating the session the connection is automatically opened, which I thought that with NH isn't the case. Shouldn't this be closed until NH determines the optimal time for flush? What am I doing wrong?

The hibernate.cfg.xml:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=Test; Integrated Security=SSPI</property>
    <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  </session-factory>
</hibernate-configuration>

Solution

  • No, what actually happens is that NHibernate creates and opens a connection when you first say session.Connection (if the session didn't already have a connection, of course)