Hello Svein
I found a solution after reading this link:
http://forum.hibernate.org/viewtopic.ph ... connection
As you can see by reading this link Session.IsConnected will not tell you if there is an active connection to the database. But you can force it to do it in this way:
When you try to fetch data from the database with NHibernate then do it with a try-catch.
If you catch an exception use Session.Disconnect() to be sure that the session is disconnected.
Set a DateTime variable with DateTime.Now to mark the time.
Every time your service/timer runs use a TimeSpan variable where you calculate the difference between DateTime.Now and the variable set when the Exception occurred and wait to run your query until the delay you want has passed.
When you try to query again use Session.Reconnect() to open the connection again.
I have a DataAccess class where I have this property:
private ISession _session;
private ISession Session
{
set
{
_session = value;
}
get
{
if (!_session.IsConnected)
_session.Reconnect();
return _session;
}
}
In all my methods for fetching and saving data I call this property. Ex.:
public IList GetListByPropertyValue(Type type, string propertyName, object propertyValue)
{
try
{
ICriteria crit=Session.CreateCriteria(type);
crit.Add(Expression.Eq(propertyName, propertyValue));
return crit.List();
}
catch (Exception ex)
{
throw ex;
}
}
I use NHibenate in a web context and in my HHTPModule I have this method:
public static void DisconnectCurrentSession()
{
ISession _session = CurrentSession;
if (_session != null)
{
try
{
_session.Disconnect();
}
catch (Exception ex)
{
string _errmsg = ex.Message;
}
}
}
I hope this will help you.
jorgen