Recent

Author Topic: Smart Transaction Manager  (Read 2727 times)

DeBritto

  • Jr. Member
  • **
  • Posts: 68
Smart Transaction Manager
« on: June 06, 2016, 02:38:07 am »
Hi everyone,
I'm starting to deal with transactions in my app and I've found this code at this web site (From: http://delphi.xcjc.net//viewthread.php?tid=48818).

But unfortunately I still doesn't know how to change it to work in Free Pascal/Lazarus using the basic components. I'm using Firebird 2.5 as database. I thought that this code could be usefull not only for me. Could anyone help me translate it to work in Free Pascal/Lazarus?

Other question, do you think that this logic works well?


{Smart Transaction Management with Firebird/Interbase}

Add the following functions to your main datamodule (dmMain)

Code: Pascal  [Select][+][-]
  1. public
  2.     procedure OpenQuery(Q: TIBQuery; AForceRB: boolean = false);
  3.     procedure ExecQuery(Q: TIBQuery; AForceRB: boolean = false; FAutoCommit: boolean = false);
  4.  
  5. //and in the implementation:
  6.  
  7. procedure TdMain.ExecQuery(Q: TIBQuery; AForceRB, FAutoCommit: boolean);
  8. begin
  9.   if Q.Transaction.InTransaction then  // only rollback if active
  10.     if AForceRB then
  11.       Q.Transaction.Rollback;
  12.  
  13. if not Q.Transaction.InTransaction then
  14.     Q.Transaction.StartTransaction;
  15.  
  16. try
  17.  
  18. Q.ExecSQL;
  19.  
  20. except
  21.   on E:Exception do
  22.   begin
  23.     if Q.Transaction.InTransaction then
  24.       Q.Transaction.RollBack;
  25.  
  26. Assert(false, 'An error has occurred while executing the query '+Q.Name+'.'#13#13'Errormessage: '+E.Message);
  27.  
  28. end;
  29.  
  30. if FAutoCommit then
  31.     Q.Transaction.Commit;
  32. end;
  33.  
  34. procedure TdMain.OpenQuery(Q: TIBQuery; AForceRB: boolean);
  35. begin
  36.   if Q.Transaction.InTransaction then
  37.     if AForceRB then
  38.       Q.Transaction.Rollback;
  39.  
  40. if not Q.Transaction.InTransaction then
  41.     Q.Transaction.StartTransaction;
  42.  
  43. try
  44.  
  45. Q.Open;
  46.  
  47. except
  48.   on E:Exception do
  49.   begin
  50.  
  51. Assert(false, 'An error has occurred while opening the query '+Q.Name+'.'#13#13'Errormessage: '+E.Message);
  52.  
  53. end;
  54.  
  55. end;

In order to use these function think like this:
Some form, getting data to fill listbox:

Code: Pascal  [Select][+][-]
  1. procedure UpdateListbox;
  2. begin
  3.   qGet.SQL.Text:= 'SELECT NAME FROM CUSTOMERS ORDER BY CITY';
  4.  
  5. dMain.OpenQuery(qGet, true); // force rollback before opening - most recent data is wanted.
  6.  
  7. while not qGet.Eof do
  8.   begin
  9.     lbCustomers.Items.Add(qGet.Fields[0].AsString);
  10.     qGet.Next;
  11.   end;
  12.  
  13. qGet.Transaction.Commit;
  14. end;
« Last Edit: June 06, 2016, 02:51:43 am by DeBritto »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Smart Transaction Manager
« Reply #1 on: April 17, 2017, 01:44:34 am »
More than 10 months have passed, just as a curiosity, have you found a solution for you?

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Smart Transaction Manager
« Reply #2 on: April 17, 2017, 01:48:42 am »
Smart Transaction Management with Firebird/Interbase
http://delphi.xcjc.net//viewthread.php?tid=48818).

... do you think that this logic works well?

No.
If you are working with transactions on RAD, you'd better be explicit using try/except and commit/rollback whenever they are necessary.
That's make you code clear for you and for future maintain.

 

TinyPortal © 2005-2018