1

I'm using NHibernate with SQL Server 2005 in a WPF client application.

If I manually stop the SQL Server service and then restart it the session doesn't automatically reconnect.

So far I'm doing this witch seems to work :

try
{
    using (ITransaction transaction = this.Session.BeginTransaction())
    {
        // some select here
    }
}catch(Exception ex)
{                
    if(this.Session.Connection.State == ConnectionState.Closed)
    {
        try
        {
            this.Session.Connection.Open();
        }
        catch (Exception)
        {
        }
    }
}

Is there a better way ?

Catalin DICU
  • 4,610
  • 5
  • 34
  • 47

2 Answers2

1

NHibernate won't handle connection drops for you. For occasionally connected scenarios like this I'd look into using a local database as suggested in this question, and then use the Microsoft Sync Framework to synchronize the local and central databases. Also see this related question.

Community
  • 1
  • 1
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
-1

If the server just stops, I think there are other problems. I don't have any experience in WPF but there are a lot of questions related to nhibernate / WPF (SO query).

If the database connections drops sometimes you may take a look at the UnitOfWork pattern.

Community
  • 1
  • 1
bob
  • 6,465
  • 1
  • 30
  • 26
  • 2
    The connection doesn't drop, it's me manually stopping the SQL server service. I'd expect the session to automatically recover when starting the service again. – Catalin DICU May 26 '10 at 12:08