Work units and transactions of the Abp, and work unit transactions of the abp
Environment: Abp1.2
Question: the timing of transaction commit caused by the failure to call the workunit's SaveChanges method.
For example, an Application Service Code is as follows:
public void CreatePhrase(PhraseCreateDto input) {var phrase = Mapper.Map<Phrase>(input); phrase.Id = Guid.NewGuid(); _phraseRepository.Insert(phrase); }
Insert a record based on the data submitted by the user, but the SaveChanges method is not explicitly called at the end of the Method
The code for calling the above method in the Controller of Mvc is as follows:
[AbpAuthorize] public ActionResult Create () {ViewBag. count = _ phraseAppService. getCount (); return View ();} [AbpAuthorize] [HttpPost] [ValidateInput (false)] public ActionResult Create (FormCollection fc) {CheckModelState (); if (fc. get ("editorValue ")! = Null) & (fc. Get ("ChineseMean ")! = Null) {// ueditor sometimes has an extra br line break at the end, which needs to be removed. var sentenceHtml = fc. get ("editorValue"); var phrase = new PhraseCreateDto {ChineseMean = fc. get ("ChineseMean"), SentenceHtml = sentenceHtml, // 1. remove Html tags 2. returns escape characters such as single quotes and double quotes. sentence = Server. htmlDecode (Common. replaceHtmlMark (sentenceHtml)}; _ phraseAppService. createPhrase (phrase);} return Create ();}
In the _ phraseAppService. createPhrase (phrase). After the record is inserted, call the Create method without parameters. In the Create method, ViewBag. count = _ phraseAppService. the number of records obtained by GetCount () is still the number of original records (not + 1). That is to say, after obtaining the number of records, if the SaveChanges method of the current work unit is explicitly called at the end of the CreatePhrase method, the latest number of records can be obtained each time:
public void CreatePhrase(PhraseCreateDto input) {var phrase = Mapper.Map<Phrase>(input); phrase.Id = Guid.NewGuid(); _phraseRepository.Insert(phrase); CurrentUnitOfWork.SaveChanges(); }
Note that the relationship between the work unit and the transaction is as follows:
public void CreatePhrase(PhraseCreateDto input) { using (var uow=UnitOfWorkManager.Begin()) { var phrase = Mapper.Map<Phrase>(input); phrase.Id = Guid.NewGuid(); _phraseRepository.Insert(phrase); uow.Complete(); } throw new Exception($"the exception inner {nameof(CreatePhrase)}"); }
If an exception is thrown after the Complete of UnitOfWorkHanle is called, is there any data insertion? The answer is not necessarily, because in the application service method, the default is a unit of work, and then create a smaller scope of work unit in the method, not necessarily create a transaction, however, it is possible to use existing transactions, but some transactions are managed by the outer unit of work, So calling the Complete method does not commit the transaction, so after an exception is thrown, the outer unit will roll back the transaction.
However, Begin has several reloads, such:
Required: default value. It is created if the transaction does not exist. If yes, it is used.
RequiresNew: always create a transaction.Therefore, if var uow = UnitOfWorkManager. Begin (transactionscospontion. RequiresNew) is used, the transaction is committed before an exception is thrown.
Suppress: No surrounding transaction context is required. all operations in the work unit domain are committed.