11.38 how does castleactiverecord ensure the security and success of multi-thread concurrent operations?

Source: Internet
Author: User

In MultithreadingConcurrencyIn the operating environment, database operations must also be preventedConcurrencyImproper control leads to data confusion. Currently, we have encountered the following situations:

Thread 1 Save Class A Data, not used Transactional session , At the same timeThread 2 In operation Class B Data, used Transactional session , The result is:Thread 1 All operations are lost,Thread 2 The operation results are saved to the database correctly. Debugging errors are detected,Thread 1 OfProgramDuring the test, because it was a single-threaded environment, there was no problem with running, and the developers felt inexplicable and had no way to start.

In order to avoid the above situations, we should adhere to the following rules to use sessions:

AThe pure query uses the refreshing session, for example:

Private VoidDataportal_fetch (PartylistcriteriaCriteria)

{

Raiselistchangedevents =False;

Using (New Castle. activerecord. sessionscope (Castle. activerecord. flushaction. Never ))

{

//Add QueryCodeAs follows:

Dalparty[] PS = Ps =Dalparty. Find (criteria. ownerid, criteria. Name,True, Criteria. maxresult );

}

Raiselistchangedevents =True;

}

A session exists. When accessed through a data object Delayed loading of data in a one-to-multiple relationship does not throw an exception.

 

B, AnyCud(Add, delete, modify, and delete) an event session is used. An example is as follows:

Protected Override VoidDataportal_update ()

{

This. Raiselistchangedevents =False;

 

Using (Castle. activerecord. transactionscope trans = New Castle. activerecord. transactionscope ())

{

Try

{

Foreach(VaRItemInDeletedlist)

{

Item. child_deleteself (This);

}

Deletedlist. Clear ();

 

Foreach(VaRItemIn This)

{

If(Item. isnew & item. isvalid)

{

Item. child_insert (This);

}

Else If(Item. isdirty & item. issavable)

{

Item. child_update (This);

}

}

Trans. votecommit ();

}

Catch(ExceptionEe)

{

Trans. voterollback ();

IlogLog =Logmanager. Getlogger (This. GetType ());

Log. Error (EE. tostring ());

ThrowEE;

}

}

 This. Raiselistchangedevents =True;

}

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.