« With transaction pattern (langage) » : différence entre les versions
(Page créée avec « Le pattern transaction permet d'encapsuler une transaction. ===with transaction do=== Ce pattern gère une transaction. Le code généré par ce pattern est le suivant : <… ») |
Aucun résumé des modifications |
||
| Ligne 28 : | Ligne 28 : | ||
</source> | </source> | ||
Exemple : | Exemple d'utilisation : | ||
<source lang="delphi"> | <source lang="delphi"> | ||
| Ligne 72 : | Ligne 72 : | ||
</source> | </source> | ||
Exemple : | Exemple d'utilisation : | ||
<source lang="delphi"> | <source lang="delphi"> | ||
| Ligne 122 : | Ligne 122 : | ||
</source> | </source> | ||
Exemple : | Exemple d'utilisation : | ||
<source lang="delphi"> | <source lang="delphi"> | ||
| Ligne 169 : | Ligne 169 : | ||
</source> | </source> | ||
Exemple : | Exemple d'utilisation : | ||
<source lang="delphi"> | <source lang="delphi"> | ||
Version du 20 avril 2010 à 11:04
Le pattern transaction permet d'encapsuler une transaction.
with 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
with transaction do
begin
inst := MyClass.Create;
inst.Caption := 'A new instance';
end;
end; </source>
with 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
// with 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
with 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>
with 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"> begin end; </source>
with private long transaction do
Ce pattern gère une transaction longue privé.
Le code généré par ce pattern est le suivant :
<source lang="delphi"> begin
// with private 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>
Exemple d'utilisation :
<source lang="delphi"> begin end; </source>