Hello,
i'm using CachedUpdate = true to perform more than a query in a single transaction. I'm having some issues about using it in a concurrency situation.
Here's my code
[Entity]
[Table('TG_NUMERATORI_CON')]
[Id('FANNO_DOCUMENTO', TIdGenerator.None)]
[Id('FSERIE_DOCUMENTO', TIdGenerator.None)]
TTG_NUMERATORI = class
private
[Column('ANNO_DOCUMENTO', [TColumnProp.Required], 4)]
[Description('')]
FANNO_DOCUMENTO: string;
[Column('SERIE_DOCUMENTO', [TColumnProp.Required], 4)]
[Description('')]
FSERIE_DOCUMENTO: string;
[Column('NUMERO_DOCUMENTO', [TColumnProp.Required])]
[Description('')]
FNUMERO_DOCUMENTO: Integer;
[Column('VERSIONE', [TColumnProp.Required])]
[Description('')]
[Version]
FVERSIONE: Integer;
public
property ANNO_DOCUMENTO: string read FANNO_DOCUMENTO write FANNO_DOCUMENTO;
property SERIE_DOCUMENTO: string read FSERIE_DOCUMENTO write FSERIE_DOCUMENTO;
property NUMERO_DOCUMENTO: Integer read FNUMERO_DOCUMENTO write FNUMERO_DOCUMENTO;
property VERSIONE: Integer read FVERSIONE write FVERSIONE;
end;
function TdmAurConnGS.getNUMERATORE(aAnno, aSerie: string): TTG_NUMERATORI;
begin
result:= FObjMan.Find<TTG_NUMERATORI>.add((Linq['ANNO_DOCUMENTO'] = aAnno) and
(Linq['SERIE_DOCUMENTO'] = aSerie)).refreshing.UniqueResult;
end;
var
LObj: T;
LNumeratore: TTG_NUMERATORI
LObjMan: TObjectManager;
Lobject: TObject;
begin
LObj:= adst.Current<T>;
LObjMan:= dmAurConnGS.ObjMan;
if getPropertyValue('ID',LObj).AsInteger = 0 then
begin
LObjMan.CachedUpdates:= true;
try
try
LNumeratore:=
dmAurConnGS.getNUMERATORE(getPropertyValue('ANNO_DOCUMENTO',LObj).AsString,
getPropertyValue('SERIE_DOCUMENTO',LObj).AsString);
SetNumeratore(LObj,LNumeratore);
LNumeratore.NUMERO_DOCUMENTO:= LNumeratore.NUMERO_DOCUMENTO + 1;
LObjMan.Update(LNumeratore);
LObjMan.Flush(LNumeratore);
LObjMan.SaveOrUpdate(LObj);
LObjMan.ApplyUpdates;
except
on E: EVersionedConcurrencyControl do
begin
LObjMan.CachedUpdates:= false;
getMethod('resetID',LObj).Invoke(LObj,[]); //reset ID to 0 to perform again insert
Put_or_Post<T>(adst);
end;
end;
finally
LObjMan.CachedUpdates:= false;
end;
If someone else updates in table TG_NUMERATORI_CON version field, ObjectManager obviously throws a EVersionedConcurrencyException, so I refresh LNumeratori cached in ObjectManager and I try again the operation.
When Put_Or_Post is invoked the second time, I can see the correct version of LNumeratori (same as the DB), but in LObjMan.CachedCount still remain on the previous value.
I've inspected TObjectManager.InternalApplyUpdates and it seems that if there's an exception FCachedActions isn't correctly cleared. How can I clear this list to perform correctly operations?
Thanks in advance.