Recent

Author Topic: Difference between StartTransaction and Active:= True;  (Read 320 times)

egsuh

  • Sr. Member
  • ****
  • Posts: 381
Difference between StartTransaction and Active:= True;
« on: April 27, 2020, 11:08:46 am »
Hi,

I used to use  syntax as following, because this was the way Delphi books explained.

   if not SQLTransaction1.Active then SQLTransaction1.StartTransaction;

But I'm just curious following statement does what the previous one does.

    SQLTransaction1.Active := True;

Does this start a new transaction if the transaction was not active , and do nothing if it was active?

eljo

  • Sr. Member
  • ****
  • Posts: 407
Re: Difference between StartTransaction and Active:= True;
« Reply #1 on: April 27, 2020, 11:24:46 am »
Code: Pascal  [Select][+][-]
  1. procedure TDBTransaction.SetActive(Value : boolean);
  2. begin
  3.   if FActive and (not Value) then
  4.     EndTransaction
  5.   else if (not FActive) and Value then
  6.     if csLoading in ComponentState then // if the properties are not finished loading yet
  7.       begin
  8.       FOpenAfterRead := true; // call start transaction at a later time.
  9.       exit;
  10.       end
  11.     else
  12.       StartTransaction;
  13. end;
  14.  
So as you can see setting active true at runtime will call startTransaction so yeah its the same.

rvk

  • Hero Member
  • *****
  • Posts: 4158
Re: Difference between StartTransaction and Active:= True;
« Reply #2 on: April 27, 2020, 11:29:26 am »
Code: Pascal  [Select][+][-]
  1. procedure TDBTransaction.SetActive(Value : boolean);
  2. begin
  3.   if FActive and (not Value) then
  4.     EndTransaction
  5.   else if (not FActive) and Value then
  6.     if csLoading in ComponentState then // if the properties are not finished loading yet
  7.       begin
  8.       FOpenAfterRead := true; // call start transaction at a later time.
  9.       exit;
  10.       end
  11.     else
  12.       StartTransaction;
  13. end;
  14.  
So as you can see setting active true at runtime will call startTransaction so yeah its the same.
You missed something in the code  ;D You see a "not FActive" in the second if.
So setting Active to true will ONLY call starttransaction if the transaction isn't already started.
That's different from just calling starttransaction.

eljo

  • Sr. Member
  • ****
  • Posts: 407
Re: Difference between StartTransaction and Active:= True;
« Reply #3 on: April 27, 2020, 11:31:57 am »
Nope you missed the code compared to and I quote.

I used to use  syntax as following, because this was the way Delphi books explained.

Code: Pascal  [Select][+][-]
  1.    if not SQLTransaction1.Active then SQLTransaction1.StartTransaction;
  2.  

So yes setting active to true is equivalent with the code above.

egsuh

  • Sr. Member
  • ****
  • Posts: 381
Re: Difference between StartTransaction and Active:= True;
« Reply #4 on: April 27, 2020, 11:34:56 am »
Code: Pascal  [Select][+][-]
  1. if csLoading in ComponentState then // if the properties are not finished loading yet
  2.       begin
  3.       FOpenAfterRead := true; // call start transaction at a later time.
  4.       exit;
  5.       end

I'm not sure about the above codes, but except that it seems to act as I want.
For example,

   Transaction.Active := True;   // Starts transaction if not active, and do nothing if already active
   Transaction.Active := False;   // Closes transaction if active, possibly using default action


Many thanks to you guys for kind comments. 
 

eljo

  • Sr. Member
  • ****
  • Posts: 407
Re: Difference between StartTransaction and Active:= True;
« Reply #5 on: April 27, 2020, 11:38:44 am »
Code: Pascal  [Select][+][-]
  1. if csLoading in ComponentState then // if the properties are not finished loading yet
  2.       begin
  3.       FOpenAfterRead := true; // call start transaction at a later time.
  4.       exit;
  5.       end

I'm not sure about the above codes, but except that it seems to act as I want.
For example,

   Transaction.Active := True;   // Starts transaction if not active, and do nothing if already active
   Transaction.Active := False;   // Closes transaction if active, possibly using default action
Your assumption is correct it uses the default Action to determinate what to do when endtransaction is called.

rvk

  • Hero Member
  • *****
  • Posts: 4158
Re: Difference between StartTransaction and Active:= True;
« Reply #6 on: April 27, 2020, 11:48:45 am »
Nope you missed the code compared to and I quote.
I used to use  syntax as following, because this was the way Delphi books explained.
Code: Pascal  [Select][+][-]
  1.    if not SQLTransaction1.Active then SQLTransaction1.StartTransaction;
So yes setting active to true is equivalent with the code above.

Ah, Ok, Sorry. I only reacted to your statement (especially the words marked in bold):
So as you can see setting active true at runtime will call startTransaction so yeah its the same.
Didn't realize you responded to the snippet gives.


 

TinyPortal © 2005-2018