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;
}