Page 1 of 1

Transactions in when using more than 1 query component

Posted: Sun 01 Nov 2009 21:53
by boz
Hello

I have created a simple table:

CREATE TABLE "names" (
"id" int(10) NOT NULL AUTO_INCREMENT,
"name" varchar(10) DEFAULT NULL,
PRIMARY KEY ("id")
);

Please consider the following program that demonstrates a problem I am having, the form has only a TmyConnection component and a button.

Code: Select all

procedure Execute(sql:string);
var
  q:TMyQuery;
begin
  q:=nil;
  screen.cursor:=crSQLwait;
  try
    q:=TMyQuery.Create(nil);
    q.Connection:=Form1.MyConnection1;
    q.SQL.Text:=sql;
    q.Execute;
  finally
    q.Free;
    screen.cursor:=crdefault;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    execute('SAVEPOINT x');
    execute('INSERT INTO names (name) VALUES("Bill")');
    abort;
    execute('COMMIT');
  except
    execute('ROLLBACK TO x');
  end;
end;
When I execute the program and click the button I get the error "#42000 Savepoint x does not exist" and so it looks like each MyQuery is a separate transaction.

My Question is how do I force any query components I will use to be treated as the same transaction?

Or is there a document which explains transactions when using mysqldac as I also have another question when this is answered :-)

Thanks

Boz

The above code is not part of my real program I would not do something so simple this way it is only to reproduce and demonstrate my question.

Posted: Mon 02 Nov 2009 08:53
by Dimon
To solve the problem you should execute the 'BEGIN' SQL statement before executing the 'SAVEPOINT x' command, like this:

Code: Select all

execute('BEGIN');
execute('SAVEPOINT x');

Posted: Tue 03 Nov 2009 04:00
by boz
Thanks :)