Question

I'm developing an application (for Win32 and WinCE) in Lazarus using sqldb components for data access.

Remote database is PostgreSQL (but i have the same behaviour with local SQLite). Connection to PostgreSQL work perfectrly but when I open any Query (also a very simple select) database go in transaction: "idle in transaction".

var
  PGConnection:TPQConnection;
  PGTransaction:TSQLTransaction;
  myQuery:TSQLQuery;
begin
  PGConnection := TPQConnection.Create(self);
  PGTransaction := TSQLTransaction.Create(self);
  myQuery := TSQLQuery.Create(self);
  try
    PGConnection.HostName := '192.168.1.2';
    PGConnection.DatabaseName:='testdb';
    PGConnection.UserName:='test';
    PGConnection.Password:='test';
    PGConnection.Transaction := PGTransaction;
    PGConnection.Open;

    myQuery.DataBase := PGConnection;
    myQuery.SQL.Add('SELECT 1 AS value');
    myQuery.Open; // <- this start transaction
    ShowMessage(myQuery.FieldByName('value').AsString); // <- db: "idle in transaction"
    myQuery.Close; // <- db: "idle in transaction"
    PGConnction.Close; 
  finally
    myQuery.Free;
    PGConnection.Free;
    PGTransaction.Free;
  end;
end;  

Ok, maybe sqldb work in this way: every query on database start a transaction, so, developer must Commit or Rollback after interrogation. But there is another question: when I commit the Transaction, sqldb close the query and i can't access value retrieved:

var
  PGConnection:TPQConnection;
  PGTransaction:TSQLTransaction;
  myQuery:TSQLQuery;
begin
  PGConnection := TPQConnection.Create(self);
  PGTransaction := TSQLTransaction.Create(self);
  myQuery := TSQLQuery.Create(self);
  try
    PGConnection.HostName := '192.168.1.2';
    PGConnection.DatabaseName:='testdb';
    PGConnection.UserName:='test';
    PGConnection.Password:='test';
    PGConnection.Transaction := PGTransaction;
    PGConnection.Open;

    myQuery.DataBase := PGConnection;
    myQuery.SQL.Add('SELECT 1 AS value');
    myQuery.Open; // <- this start transaction
    PGConnection.Transaction.Active := False; // <- Close also myQuery
    ShowMessage(myQuery.FieldByName('value').AsString); // <- Error: Field "value" not found
    myQuery.Close; 
    PGConnction.Close; 
  finally
    myQuery.Free;
    PGConnection.Free;
    PGTransaction.Free;
  end;
end;  

This behavior is a bit boring: I can't use TSQLQuery dataset with dbgrid (since I do not want my database in transaction for too long) so I need to move selected data in Memory Tables.

Is this a bug, I made some mistakes or is a normal operation? There is a way to open a SELECT Query and use it without start a transaction?

Was it helpful?

Solution

This is currently normal behaviour. I have planned an 'offline' mode where the transaction is closed but the data is kept open. What you can currently do is save the data to file (using the savetofile method), disconnect, and load the data again from file (using the loadfromfile method)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top