Multithreading (Access Violation)

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
exchange
Posts: 3
Joined: Mon 21 Nov 2016 11:34

Multithreading (Access Violation)

Post by exchange » Tue 22 Nov 2016 12:25

Hello,
maybe anybody can help me with the following problem:

This code has no exception:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
  sleep(1000);
  Thread1.Resume;
  sleep(1000);
  Thread2.Resume;
end; 
This ist make a exception:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
  Thread1.Resume;
  Thread2.Resume;
end; 
Complete Code:

Code: Select all

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Uni, MySQLUniProvider;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

  TThread1 = class(TThread)
    protected
    procedure Execute; override;
  end;

  TThread2 = class(TThread)
    protected
    procedure Execute; override;
  end;

var
  Form1: TForm1;
  MySQL: TMySQLUniProvider;
  Thread1: TThread1;
  Thread2: TThread2;

implementation

{$R *.dfm}

procedure TThread1.Execute;
var DBConnection: TUniConnection; DBQuery: TUniQuery;
begin
  while not Terminated do
  begin
    DBConnection:=TUniConnection.Create(nil);
    DBConnection.Server:='172.16.6.50';
    DBConnection.Port:=3306;
    DBConnection.Username:='root_copy';
    DBConnection.Password:='password';
    DBConnection.ProviderName:='MySQL';
    DBConnection.Database:='testdb';

    DBConnection.Connect;

    if DBConnection.Connected then
    begin
        DBQuery:=TUniQuery.Create(nil);
        DBQuery.Connection:=DBConnection;
        DBQuery.SQL.Text:='INSERT into test SET datum=NOW(), thread=1;';
        DBQuery.Execute;
        DBQuery.Free;
      DBConnection.Close;
    end;

    DBConnection.Free;

    sleep(2000);
  end;
end;

procedure TThread2.Execute;
var DBConnection: TUniConnection; DBQuery: TUniQuery;
begin
  while not Terminated do
  begin
  DBConnection:=TUniConnection.Create(nil);
    DBConnection.Server:='172.16.6.50';
    DBConnection.Port:=3306;
    DBConnection.Username:='root_copy';
    DBConnection.Password:='password';
    DBConnection.Database:='testdb';
    DBConnection.ProviderName:='MySQL';

    DBConnection.Connect;

    if DBConnection.Connected then
    begin
        DBQuery:=TUniQuery.Create(nil);
        DBQuery.Connection:=DBConnection;
        DBQuery.SQL.Text:='INSERT into test SET datum=NOW(), thread=2;';
        DBQuery.Execute;
        DBQuery.Free;
      DBConnection.Close;
    end;

    DBConnection.Free;

    sleep(2000);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  sleep(1000);
  Thread1.Resume;
  sleep(1000);
  Thread2.Resume;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MySQL.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MySQL:=TMySQLUniProvider.Create(nil);

  Thread1 := TThread1.Create(True);
  Thread1.FreeOnTerminate := True;

  Thread2 := TThread2.Create(True);
  Thread2.FreeOnTerminate := True;
end;

end.
This is my first multithreading application. The error ist in the line
Thanks

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Multithreading (Access Violation)

Post by ViktorV » Wed 23 Nov 2016 12:37

Thank you for the information. We have reproduced the issue and investigation is in progress. We will inform you when we have any results.

exchange
Posts: 3
Joined: Mon 21 Nov 2016 11:34

Re: Multithreading (Access Violation)

Post by exchange » Wed 30 Nov 2016 07:49

Do you have news?

Thanks

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Multithreading (Access Violation)

Post by ViktorV » Wed 30 Nov 2016 09:39

We will continue investigation of the UniDAC behavior and inform you as soon as we get any results, but we can't tell any time frame at the moment.

hq872036709
Posts: 2
Joined: Fri 02 Dec 2016 10:18

Re: Multithreading (Access Violation)

Post by hq872036709 » Fri 02 Dec 2016 10:33

TUniProvider = class(TComponent)
protected
cri: TCriticalSection;//add



function TMySQLUniProvider.GetConnectionOptions: TOptionsList;
begin
cri.Acquire; //add
try
if FConnectionOptions = nil then begin
FConnectionOptions := TOptionsList.Create(GetProviderName);
FConnectionOptions.Add(TIntegerOption.Create('ConnectionTimeout', prConnectionTimeout, [TMySQLConnection, TMyConnectionParameters], 15));
FConnectionOptions.Add(TStringOption.Create('Charset', prCharset, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TBooleanOption.Create('UseUnicode', prUseUnicode, [TMySQLConnection, TMyConnectionParameters], False));
FConnectionOptions.Add(TBooleanOption.Create('OptimizedBigint', prOptimizedBigint, [TMySQLConnection], False));
FConnectionOptions.Add(TBooleanOption.Create('NullForZeroDelphiDate', prNullForZeroDelphiDate, [TMySQLConnection], False));

FConnectionOptions.Add(TBooleanOption.Create('Compress', prCompress, [TMySQLConnection, TMyConnectionParameters], False));
FConnectionOptions.Add(TEnumeratorOption.Create('Protocol', prProtocol, [TMySQLConnection, TMyConnectionParameters], Variant(mpDefault), TypeInfo(TMyProtocol)));
FConnectionOptions.Add(TBooleanOption.Create('Embedded', prEmbedded, [TMySQLConnection, TMyConnectionParameters, TMyConnectDialogService], False));
FConnectionOptions.Add(TStringOption.Create('EmbeddedParams', prEmbParams, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TBooleanOption.Create('Interactive', prInteractive, [TMySQLConnection, TMyConnectionParameters], False));
FConnectionOptions.Add(TEnumeratorOption.Create('IPVersion', prIPVersion, [TMySQLConnection, TMyConnectionParameters], Variant(ivIPv4), TypeInfo(TIPVersion)));

// SSL
FConnectionOptions.Add(TStringOption.Create('SSLChipherList', prSSLCipher, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TStringOption.Create('SSLCACert', prSSLCA, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TStringOption.Create('SSLKey', prSSLKey, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TStringOption.Create('SSLCert', prSSLCert, [TMySQLConnection, TMyConnectionParameters], ''));

// HTTP
FConnectionOptions.Add(TStringOption.Create('HttpUrl', prHttpUrl, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TStringOption.Create('HttpUsername', prHttpUsername, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TStringOption.Create('HttpPassword', prHttpPassword, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TStringOption.Create('ProxyHostname', prProxyHostname, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TIntegerOption.Create('ProxyPort', prProxyPort, [TMySQLConnection, TMyConnectionParameters], 0));
FConnectionOptions.Add(TStringOption.Create('ProxyUsername', prProxyUsername, [TMySQLConnection, TMyConnectionParameters], ''));
FConnectionOptions.Add(TStringOption.Create('ProxyPassword', prProxyPassword, [TMySQLConnection, TMyConnectionParameters], ''));
end;

finally
cri.Release;
end;
Result := FConnectionOptions;
end;

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Multithreading (Access Violation)

Post by ViktorV » Fri 02 Dec 2016 12:25

We will continue investigation of the UniDAC behavior and inform you as soon as we get any results, but we can't tell any time frame at the moment.

hq872036709
Posts: 2
Joined: Fri 02 Dec 2016 10:18

Re: Multithreading (Access Violation)

Post by hq872036709 » Fri 02 Dec 2016 13:17

GetConnectionOptions is thread unsafe

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Multithreading (Access Violation)

Post by ViktorV » Fri 02 Dec 2016 13:56

Yes, you are right. We are working on multithreading support improvement.

Post Reply