« With transaction pattern (langage) » : différence entre les versions
(4 versions intermédiaires par un autre utilisateur non affichées) | |||
Ligne 42 : | Ligne 42 : | ||
===withP private transaction do=== | ===withP private transaction do=== | ||
Ce pattern gère une transaction privée. | Ce pattern gère une [[Transaction privée|transaction privée]]. | ||
Le code généré par le pattern est le suivant : | Le code généré par le pattern est le suivant : | ||
Ligne 149 : | Ligne 149 : | ||
end; | end; | ||
</source> | </source> | ||
{{tip|Les patterns gérant des transactions longues utilisent le mode [[TranAutoBatch_(CM)|AutoBatch]]}} | |||
===withP private long transaction do=== | ===withP private long transaction do=== | ||
Ligne 190 : | Ligne 192 : | ||
</source> | </source> | ||
[[Category:Langage]] | [[Category:Langage]] | ||
[[Category:Transaction]] | |||
[[Category:Code pattern]] | [[Category:Code pattern]] |
Dernière version du 18 mars 2013 à 09:13
Le pattern transaction permet d'encapsuler une transaction.
withP transaction do
Ce pattern gère une transaction.
Le code généré par ce pattern est le suivant :
<source lang="delphi"> begin
// with transaction do // ClassManager.BeginTran; try // glbWorkerPool.BeginParallel; try h.RunAnonymous(f.sAEntry); finally glbWorkerPool.EndParallel; end; // ClassManager.Commit([]); except ClassManager.RollBack; raise; end;
end; </source>
Exemple d'utilisation :
<source lang="delphi"> begin
// Create an instance withP transaction do begin inst := MyClass.Create; inst.Caption := 'A new instance'; end;
end; </source>
withP private transaction do
Ce pattern gère une transaction privée.
Le code généré par le pattern est le suivant :
<source lang="delphi"> begin
// withP private transaction do // old := ClassManager.NewTransContext; try ClassManager.BeginTran; try glbWorkerPool.BeginParallel; try h.RunAnonymous(f.sAEntry); finally glbWorkerPool.EndParallel; end; // ClassManager.Commit([]); except ClassManager.RollBack; raise; end; finally ClassManager.ActivateContext(old); end;
end; </source>
Exemple d'utilisation :
<source lang="delphi"> var inst:WFClasseB; begin
withP private Transaction do for var idx:=1 to 100 do begin inst := WFClasseB.Create; inst.unCode := 'B'+inttostr(index); inst.Caption := 'Objet B'+inttostr(index); end;
end; </source>
withP long transaction do
Ce pattern gère une transaction longue.
Le code généré par ce pattern est le suivant :
<source lang="delphi"> begin
// with long(batchSize) transaction do // ClassManager.BeginLongTran(batchSize); if batchSize>0 then begin ClassManager.CurrentTran.AutoBatch := True; ClassManager.CurrentTran.AutoBatchOptions := [coDontShowEngineError]; end; try {$IFDEF PARALLEL} glbWorkerPool.BeginParallel; try {$ENDIF} h.RunAnonymous(f.sAEntry); {$IFDEF PARALLEL} finally if not glbWorkerPool.EndParallel(E) then raise E; end; {$ENDIF}
ClassManager.CommitLongTran([coDontShowEngineError,coNoConfirmDialog]); except ClassManager.RollBackLongTran; raise; end;
end; </source>
Exemple d'utilisation :
<source lang="delphi"> // Long transaction with default batch size var inst:WFClasseB; begin
withP long(100) Transaction do for var idx:=1 to 100 do begin inst := WFClasseB.Create; inst.unCode := 'B'+inttostr(index); inst.Caption := 'Objet B'+inttostr(index); end;
end;
// Long transaction with a batch size of 100 var inst:WFClasseB; begin
withP long(100) Transaction do for var idx:=1 to 100 do begin inst := WFClasseB.Create; inst.unCode := 'B'+inttostr(index); inst.Caption := 'Objet B'+inttostr(index); end;
end; </source>
![]() |
Tip : Les patterns gérant des transactions longues utilisent le mode AutoBatch |
withP private long transaction do
Ce pattern gère une transaction longue privée.
Le code généré par ce pattern est le suivant :
<source lang="delphi"> begin
// with privateP long(batchSize) transaction do // old := ClassManager.NewTransContext; try ClassManager.BeginLongTran(batchSize); if batchSize>0 then begin ClassManager.CurrentTran.AutoBatch := True; ClassManager.CurrentTran.AutoBatchOptions := [coDontShowEngineError]; end; try {$IFDEF PARALLEL} glbWorkerPool.BeginParallel; try {$ENDIF} h.RunAnonymous(f.sAEntry); {$IFDEF PARALLEL} finally if not glbWorkerPool.EndParallel(E) then raise E; end; {$ENDIF} // ClassManager.CommitLongTran([coDontShowEngineError,coNoConfirmDialog]); except ClassManager.RollBackLongTran; raise; end; finally ClassManager.ActivateContext(old); end;
end; </source>