Domanda

For now i've done this:

/// <summary>
/// Disposing connection to database.
/// </summary>
public void Dispose()
{
    if (_sessionFactory != null)
        _sessionFactory.Dispose();

    if(_session != null)
    _session.Dispose();

    _sessionFactory = null;
    _session = null;
}

To dispose Connection to database, is this wrong? Should i just dispose the session and set it to null and let sessionfactory to be "alive"?

È stato utile?

Soluzione

The SessionFactory ist a very expensive object to create and should be created just once in the application life time.

So if you are done with the application or you know you will not need to connect to the data base in the lifetime of the application you can dispose the factory. Otherwise just leave it to be reused.

On the other hand the Session is a lightweight object that can be created easily by the SessionFacotry. Long living Sessions could lead to memory leaks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top