Connection and Threads ;)

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Thu 07 Apr 2005 09:37

ProjectFetch.dpr

Code: Select all

program ProjectFetch;

uses
  Forms,
  UnitFetch in 'UnitFetch.pas' {Form2},
  UnitThreadFetch in 'UnitThreadFetch.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
UnitFetch.dfm

Code: Select all

object Form2: TForm2
  Left = 246
  Top = 114
  Width = 870
  Height = 640
  Caption = 'Form2'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 862
    Height = 41
    Align = alTop
    TabOrder = 0
    object BitBtn1: TBitBtn
      Left = 8
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Start'
      TabOrder = 0
      OnClick = BitBtn1Click
    end
  end
  object DBGrid1: TDBGrid
    Left = 0
    Top = 41
    Width = 862
    Height = 565
    Align = alClient
    DataSource = DataSource1
    TabOrder = 1
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object MyConnection1: TMyConnection
    Database = 'mydactest'
    Username = 'root'
    Server = 'server'
    Connected = True
    LoginPrompt = False
    Left = 288
    Top = 8
  end
  object MyQuery1: TMyQuery
    Connection = MyConnection1
    SQL.Strings = (
      'SELECT * FROM detail a, detail b, detail c'
      '-- , detail d, detail e, detail f')
    FetchAll = True
    Left = 320
    Top = 8
  end
  object DataSource1: TDataSource
    Left = 352
    Top = 8
  end
end
UnitFetch.pas

Code: Select all

unit UnitFetch;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, MemDS, DBAccess, MyAccess, StdCtrls, Buttons, Grids,
  DBGrids, ExtCtrls, UnitThreadFetch;

type
  TForm2 = class(TForm)
    MyConnection1: TMyConnection;
    MyQuery1: TMyQuery;
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    BitBtn1: TBitBtn;
    DataSource1: TDataSource;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
    FMyQueryFetch: TMyQueryFetch;

    procedure OnFetchComplete(Sender: TObject);

  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.BitBtn1Click(Sender: TObject);
begin
  Caption := 'You can NOT use MyConnection1 and MyQuery1 now';
  BitBtn1.Enabled := False;
  FMyQueryFetch := TMyQueryFetch.Create(MyQuery1);
  FMyQueryFetch.OnTerminate := OnFetchComplete;
  FMyQueryFetch.Resume;
end;

procedure TForm2.OnFetchComplete(Sender: TObject);
begin
  FMyQueryFetch := nil;
  Caption := 'You can use MyConnection1 and MyQuery1 now';
  DataSource1.DataSet := MyQuery1;
end;

end.
UnitThreadFetch.pas

Code: Select all

unit UnitThreadFetch;

interface

uses
  Classes, DB;

type
  TMyQueryFetch = class(TThread)
  private
    FDataSet: TDataSet;
  protected
    procedure Execute; override;
  public
    constructor Create(DataSet: TDataSet);
  end;

implementation

{ TMyQueryFetch }

constructor TMyQueryFetch.Create(DataSet: TDataSet);
begin
  inherited Create(True);
  FDataSet := DataSet;
  FreeOnTerminate := True;
end;

procedure TMyQueryFetch.Execute;
begin
  FDataSet.Open;
end;

end.

GEswin
Posts: 186
Joined: Wed 03 Nov 2004 16:57
Location: Spain
Contact:

Post by GEswin » Thu 07 Apr 2005 12:32

Would be nice to include this in the example folders of mydac releases :)

Guest

Post by Guest » Thu 07 Apr 2005 12:46

Thank you Ikar for the time spent on this, but I'm encountering the same problem.

When fetching 1 query, it is working well.
When fetching 2 queries at the same time (I duplicated everything from component to events) I'm getting the same problem as before.

The purpose was to load in the background more the 1 query :)

It is perhaps impossible due to the component architecture ? Is this really impossible to make it run ?

Thank you anyways

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Thu 07 Apr 2005 12:49

> I duplicated everything from component to events

Even TMyConnection?

Guest

Post by Guest » Fri 08 Apr 2005 06:48

no:
FMyQueryFetch2: TMyQueryFetch;
procedure OnFetchComplete2(Sender: TObject);
TForm2.BitBtn1Click2(Sender: TObject);


procedure TForm2.BitBtn2Click(Sender: TObject);
begin
//Caption := 'You can NOT use MyConnection1 and MyQuery1 now';
BitBtn2.Enabled := False;
FMyQueryFetch2 := TMyQueryFetch2.Create(MyQuery2);
FMyQueryFetch2.OnTerminate := OnFetchComplete2;
FMyQueryFetch2.Resume;
end;

procedure TForm2.OnFetchComplete(Sender: TObject);
begin
FMyQueryFetch2 := nil;
//Caption := 'You can use MyConnection1 and MyQuery1 now';
DataSource2.DataSet := MyQuery2;
end;
I click at the same time the 2 button so that 2 queries are loaded in background. I4m getting then the error.

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Fri 08 Apr 2005 07:22

Until fetch of the first MyQuery1 isn't completed you couldn't use MyQuery1.Connection. For MyQuery2 you should create its own connection.

Guest

Post by Guest » Fri 08 Apr 2005 08:57

I know , THIS IS ACTUALLY MY PROBLEM.
I have to open 10 tables in background.
I want to have 1 MyConnection

Why 1 MyConnection ? : because I want to close all tables / connection in one quick way (MyConnection.disconnect)

That's all :)

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Fri 08 Apr 2005 11:02

You cannot do so. According to MySQL ideology through the single connection it is possible to execute only a single query simultaneously. To avoid this problem you have to create additional TMyConnection. MyDAC doesn't provide possibility to group connections by some criteria and then at once disconnect all of them.

Guest

Post by Guest » Mon 11 Apr 2005 07:03

Ok, I see.

Sorry for disturbing you again, but why not implemeting an MyConnectionManager component ?

Such a component will be really usefull !, it could, for exemple, handle all the created connections to mysql.

So, we will parse through the connection and close each others....

What do you think about this (and what other dveloppers thinks about that ?)

Thank you

GEswin
Posts: 186
Joined: Wed 03 Nov 2004 16:57
Location: Spain
Contact:

Post by GEswin » Mon 11 Apr 2005 07:51

I agree that it would be very usefull. I have many threaded applications where i create many threads based on remote connections, and it's a pain to always close etc application nicely.

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Mon 11 Apr 2005 10:59

> it's a pain to always close etc application nicely

Do you mean complexity in closing all MyConnection before close the application? It is not obligatory, on destroy MyConnection a connection will be broken automatically. Moreover, if to use ConnectionPooling it will not be a lot of real connections.

Guest

Post by Guest » Mon 11 Apr 2005 12:31

Dear Ikar,

You might know that "we" (developpers) like to handle every objects that we create :) so, please, let us the ability to close them by a proper way :).
Is this so difficult to create such component (myConnectionManager) ?

Thank you

Ps : do not want to offense you.

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Mon 11 Apr 2005 15:12

> You might know that "we" (developpers) like to handle every objects that we
> create :)

To create a component that will close all connections created at the project doesn't solve the problem - you have to destroy them. It also concerns all DataSets if they were created as well as TMyConnection, they also must be destroyed.

> Is this so difficult to create such component (myConnectionManager) ?

It is quite easy to create such component. But management of a method to choose a connection to be closed makes its using more complex that writing own code.

Post Reply