A problem has plagued us for a long time:
The same program sometimes fails to succeed and is suspected to be related to the ASP thread.
Program:
Public int Deposit (Guid AccountID, string sn)
{
Decimal balance = 0;
Int useDay = 0;
String business;
Account account =
(Account) _ coreRepository. GetObjectById (typeof (Account), AccountID );
Guid snId;
GetSnID (sn, out snId); // call Hql
If (ValidateSn (snId, out balance, out useDay, out business) = 0) // use HQL of NH
{
// Omitted
_ CoreRepository. UpdateObject (account );
SetOperateLog ("DepositBySn", balance. ToString (), "", "", account); // Save is used
Return 0;
}
}
Else
{
Return ValidateSn (snId, out balance, out useDay, out business );
}
}
_ CoreRespository source code my Blog has encapsulated some simple CRUD operations
The above code suddenly failed to run yesterday and encountered an error when it ran to GetSnID. However, you can call GetSnID to run properly. If the problem is suspected to be a Session issue, check the Corespoitory encapsulation and find that he did not close the Session in each operation but asked HttpModuel to handle it.
The trackable program finds that the Session is Open, so it has nothing to do with the Session OPen and Close.
The Code is as follows: public int Deposit (Guid AccountID, string sn)
{
Decimal balance = 0;
Int useDay = 0;
String business;
Account account = (Account) _ coreRepository. GetObjectById (typeof (Account), AccountID );
Guid snId;
_ CoreRepository. CloseSession ();
_ CoreRepository. OpenSession ();
GetSnID (sn, out snId );
If (ValidateSn (snId, out balance, out useDay, out business) = 0)
{
_ CoreRepository. CloseSession ();
_ CoreRepository. OpenSession ();
ISession s = _ coreRepository. ActiveSession;
S. Update (account );
S. Close ();
If (_ coreRepository. ActiveSession. IsOpen = false)
{
_ CoreRepository. OpenSession ();
}
SetOperateLog ("DepositBySn", balance. ToString (), "", "", account );
// _ CoreRepository. CloseSession ();
Return 0;
}
}
Can run normally.
The problem is resolved.
However, when I run the previously problematic Code (the first section) in the evening, it actually works again.
Because the Session is not thread-safe, it is suspected that it is related to the Asp thread.
The second paragraph can solve the problem. It should be that a new Session is used in every function call, instead of the old one.
Can you advise me ??