(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
program bad;

uses SQLDB, MySQL55Conn, dbu, sysutils;

type BadDB = class (DB)
    public
      function get_connection : TSQLConnection; override;
      procedure test; override;
  end;

function BadDB.get_connection : TSQLConnection;

var
  con : TMySQL55Connection;

begin
  con := TMySQL55Connection.Create(nil);
  con.HostName := 'localhost';
  con.UserName := 'root';
  con.Password := 'hemmeligt';
  con.DatabaseName := 'Test';
  get_connection := con;
end;

procedure BadDB.test;

var
   con : TSQLConnection;
   tx : TSQLTransaction;
   q : TSQLQuery;
   i : integer;

begin
  for i := 1 to 10 do begin
    con := get_connection;
    tx := TSQLTransaction.Create(nil);
    con.Transaction := tx;
    try
      q := TSQLQuery.Create(nil);
      q.DataBase := con;
      q.SQL.Text := 'UPDATE tblsrc SET val = val - 1 WHERE id = 1';
      q.ExecSQL;
      if (i mod 2) <> 1 then begin
        raise Problem.Create('Problem');
      end;
      q.SQL.Text := 'UPDATE tbltrg SET val = val + 1 WHERE id = 1';
      q.ExecSQL;
      q.Close;
      q.Free;
    except
      writeln('Problem');
    end;
    tx.Commit;
    tx.Free;
    con.Close;
    con.Free;
  end;
end;

begin
  BadDB.demo(BadDB.Create);
end.

