I have an automation system and I would like to record user logins and logouts. Therefore, I wrote a code which works nearly perfect. First of all, I record the user login with this code piece:
if (UserLog.LoadByUserAndSessionAndLogoutTime(NHibernateHTTPModule.CurrentSession, user, Session.SessionID, null) == null)
{
UserLog userlog = new UserLog();
userlog.LoginTime = DateTime.Now;
userlog.LogoutTime = null;
userlog.User = (Business.Classes.User)Session["User"];
userlog.Session = Session.SessionID;
userlog.IPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
UserLog.Insert(NHibernateHTTPModule.CurrentSession, userlog);
}
Then, in the Session_End event in Global.asax, I wrote this code piece;
IList<UserLog> userlogs = UserLog.LoadBySessionAndLogoutTime(NHibernateHTTPModule.CurrentSession, Session.SessionID, null);
foreach (UserLog userlog in userlogs)
{
userlog.LogoutTime = DateTime.Now;
UserLog.Update(NHibernateHTTPModule.CurrentSession, userlog);
}
Now, this trick works well but when I check the database at the end of the day, I have some zombie records which have login time but no logout time. It is obvious that for some users, either Session_End does not fire or something different happening which prevents updating. What I am missing? Why this is not working properly? Thanks in advance. NOTE: I am using NHibernate.
Ok, allright, after couple days of analyzing, I finally have found the problem. It is the timeout amount of NHibernate. Timeout of both NHibernate and Application are set to fifteen (15) minutes. If a user do not close the application via the right way (using logout button I mean), that user will be logged on to the system for fifteen minutes after a succesful get – set call. After fifteen minutes passed, Session_End event has been fired to update the user log in database but as NHibernate timeout is also set to fifteen minutes, sometimes NHibernate session closed due to a time out before database update line in Session_End executed. Therefore, setting timeout of NHibernate to sixteen (16) minutes solved the problem.