Greetings Expert,
We are getting StaleObject Exception often but it goes away after IIS
Reset. We searched on the internet and not able to find the proper answer
:(.
private ISessionFactory GetFactory()
{
INetLog _log = ObjectFactory.GetInstance<INetLog>();
_log.ArchStart("NHSession.GetFactory", "");
// create factory if not already built
if (localFactory == null)
{
string[] nameParts = DBNameAndHasTran.Split(new char[] { '_' });
// StructureMap will cache this by singlton
INHFactory nHFactoryHdl =
ObjectFactory.GetNamedInstance<INHFactory>(nameParts[0]);
localFactory = nHFactoryHdl.GetFactory();
_log.ArchDebug("Got the factory from sington. Put it in
localFactory");
}
else
{
_log.ArchDebug("Used the localFactory");
}
_log.ArchEnd();
return localFactory;
}
public ISession GetSession()
{
INetLog _log = ObjectFactory.GetInstance<INetLog>();
_log.ArchStart("NHSession.GetSession", "");
if (localSession == null)
{
// error check DBNameHasTran
string[] nameParts = DBNameAndHasTran.Split(new char[] { '_' });
if (nameParts.Length < 2)
throw new Exception("Invalid value for DBNameAndHasTran:" +
DBNameAndHasTran + " it is missing a _");
if (nameParts.Length > 2)
throw new Exception("Invalid value for DBNameAndHasTran:" +
DBNameAndHasTran + " it has too many _");
if ((nameParts[1] != "HasTrans") && (nameParts[1] !=
"HasNoTrans"))
throw new Exception("Invalid value for DBNameAndHasTran:" +
DBNameAndHasTran + " missing _HasTrans or _HasNoTrans");
// get the factory
_log.ArchDebug("GetSession From Factory");
localSession = GetFactory().OpenSession();
// start transaction if InTrans == true
if (nameParts[1] == "HasTrans")
{
if (localSession.Transaction == null ||
!localSession.Transaction.IsActive)
{
_log.ArchDebug("Call to BeginTransaction");
localTransaction = localSession.BeginTransaction();
TransactionStarted = true;
}
}
}
else
{
_log.ArchDebug("Not Creating a Session Object using
localSession");
}
_log.ArchEnd();
return localSession;
}
public void CloseOutSession()
{
INetLog _log = ObjectFactory.GetInstance<INetLog>();
_log.ArchStart("NHSession.CloseOutSession", "");
// log if trying to close a closed session
if (localSession == null)
{
_log.ArchDebug("In CloseOutSession local session is null.");
// who called us
StackTrace stackTrace = new StackTrace(); // get call
stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // get
method calls (frames)
string msg = "";
// write call stack to determine why CloseOutSession is called
so frequently
foreach (StackFrame stackFrame in stackFrames)
{
if (msg == "")
{
msg = "I was called by " + stackFrame.GetMethod().Name;
}
else
{
msg += ", who was called by " +
stackFrame.GetMethod().Name;
}
}
_log.ArchDebug(msg);
_log.ArchEnd();
return;
}
// log error if session is not open
if (!localSession.IsOpen)
{
_log.ArchDebug("In CloseOutSession local session is not open.
");
_log.ArchEnd();
return;
}
_log.ArchDebug("In CloseOutSession Found session to kill");
if (localSession.Transaction != null &&
localSession.Transaction.IsActive)
{
_log.ArchDebug("In CloseOutSession Found Transaction to
rollback.");
localSession.Transaction.Rollback();
//localSession = null;
}
else
{
_log.ArchDebug("In CloseOutSession. Did not find any unprocessed
transactions so did a flush");
localSession.Flush();
}
// checked above so there is not a null reference possiblity
if (localSession.Transaction != null)
{
localSession.Transaction.Dispose();
}
localSession.Close();
localSession.Dispose();
localSession = null;
localTransaction = null;
_log.ArchEnd();
}
public void Commit()
{
INetLog _log = ObjectFactory.GetInstance<INetLog>();
_log.ArchStart("NHSession.Commit", "");
// exit out if there is no session.
if (localSession == null)
{
_log.ArchDebug("In Commit local session is null. This is a
indication that GetSession was not called before commit. This means no work to
commit");
_log.ArchEnd();
return;
}
// log error if session is not open
if (!localSession.IsOpen)
{
_log.ArchDebug("In Commit local session is not open. This is a
indication that NHSession was not called before commit. This means no work to
commit");
CloseOutSession();
_log.ArchEnd();
return;
}
// log error if not transaction mode.
if (localTransaction == null)
{
_log.ArchDebug("In Commit localTransaction is null. This is a
indication that NHSession was not was not open in transaction mode.");
CloseOutSession();
_log.ArchEnd();
return;
}
try
{
// do the commit
_log.ArchDebug("In Commit printing out Statistics");
_log.ArchDebug(localSession.Statistics.ToString());
localTransaction.Commit();
}
catch (Exception ex)
{
throw;
}
finally
{
CloseOutSession();
_log.ArchEnd();
}
}
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.