Session and Asp.net thread of nhib.pdf

Source: Internet
Author: User

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 ??

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.