Hi,
We are facing some data inconsistency, when we have object(table) inhereted from another object(table), like TMember = class(TCustomer).
How transactions work on aurelius? i don't find it one documentation.
thanks
Use it like this:
Ok,
Manager.Flush;
By default aurelius Start transaction and commit/rollback with "Manager.Flush"?
we have always, start transaction? and in this particular case (inheritance)?
thanks Wagner
Yes, you have to create transactions.
Hi,
try
Trans := Manager.Connection.BeginTransaction;
Manager.Flush;
Trans.Commit;
except on E : EFDDBEngineException do
begin
Trans.Rollback;
end;
Is this correct? Why Trans.Rollback doesn't really Rollback?
Tanks, Wagner
Yes, the code seems correct, you could just raise the exception after the rollback in case you want the error to be propagated.
Hi, Wagner
I try again, and can't make transactions work. here is the code:
TUserGrp = class
private
[Column('Id', [TColumnProp.Required])]
FId: Int64;
[Column('Description', [TColumnProp.Required], 20)]
FDescription: string;
public
property Id: Int64 read FId write FId;
property Description: string read FDescription write FDescription;
end;
TUserX = class
private
[Column('Id', [TColumnProp.Required])]
FId: Int64;
[Column('UserName', [TColumnProp.Required], 20)]
FUserName: string;
[Column('RecVersion', [TColumnProp.Required])]
FRecVersion: TDateTime;
[Association([TAssociationProp.Lazy, TAssociationProp.Required], CascadeTypeAll - [TCascadeType.Remove])]
[JoinColumn('UserGrpId', [TColumnProp.Required], 'Id')]
FUserGrpId: Proxy<TUserGrp>;
function GetUserGrpId: TUserGrp;
procedure SetUserGrpId(const Value: TUserGrp);
public
property Id: Int64 read FId write FId;
property UserName: string read FUserName write FUserName;
property UserGrpId: TUserGrp read GetUserGrpId write SetUserGrpId;
end;
procedure TDBUserX.SaveUpdateT(NewDBObject: TUserX; const pSysResponse: TSysResponse);
var DBObject: TUserX;
Trans: IDBTransaction;
begin
if NewDBObject.Id = 0 then //INSERT
begin
pSysResponse.Done := pInserted;
end
else //UPDATE
begin
pSysResponse.Done := pUpdated;
DBObject := Manager.Find<TUserX>(NewDBObject.Id) as TUserX;// just to Check Record Version
if DBObject <> nil then
CheckRecVersion(NewDBObject.RecVersion, DBObject.recVersion, NewDBObject.Id, pSysResponse )
//Validate Data DBObject vs NewObject
else
NotFound(NewDBObject, NewDBObject.Id, pSysResponse);
end;
if pSysResponse.Success then
begin
NewDBObject.RecVersion := now;
NewDBObject := Manager.Merge(NewDBObject);
Trans := Manager.Connection.BeginTransaction;
try
Manager.Flush;
Trans.Commit;
except
on E : EFDDBEngineException do
begin
Trans.Rollback;
raise;
end;
end;
end;
end;
if you try, "SaveUpdateT" with UserGrp.Description:= nil, "Manager.Merge" raise an exception, Trans.Rollback is executed,
but a record is inserted in Tuser!!!
Do the merge after the transaction starts, if the object has no id, merge will save (insert) it.
Ok, works fine, but to save objects to database, should use "Manager.Merge" or ""Manager.Flush" or both?
Both