Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Discussion of open issues, suggestions and bugs regarding usage of dbExpress drivers for MySQL in Delphi and C++Builder
Post Reply
MaBrazil
Posts: 5
Joined: Tue 24 Jan 2012 12:35

Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by MaBrazil » Wed 13 Jun 2012 15:30

I'm porting an application from Delphi 2006 to Delphi XE2.

Everything has gone well until I tried to update a PC with the new version.
When application tries to connect to the Database server it reports an error
Can't connect to MySQL server on 'localhost' (10061)
After a few hours of searching both PC's, I found the work version was connecting to 127.0.0.1.

Is there a restriction that only IP addresses can be used or am I missing a setting?

Thank in advances.

AndreyZ

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by AndreyZ » Thu 14 Jun 2012 12:16

Hello,

Please check if connection can be established using telnet in the following way:

Code: Select all

telnet localhost 3306
If you cannot establish a connection using telnet, please try opening your Windows hosts file (for example, C:\Windows\System32\drivers\etc\hosts) and uncomment the following line:

Code: Select all

127.0.0.1 localhost

MaBrazil
Posts: 5
Joined: Tue 24 Jan 2012 12:35

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by MaBrazil » Thu 14 Jun 2012 16:04

Telnet works fine.

Before update the Application the old Delphi 2006 using dbexpmda30.dll v4.xx worked with the host set to localhost.

On the PC I've used the PC's name without any problem on the old version of the application.

AndreyZ

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by AndreyZ » Fri 15 Jun 2012 10:14

Please try using the standard dbExpress driver for MySQL and check this problem with it.

MaBrazil
Posts: 5
Joined: Tue 24 Jan 2012 12:35

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by MaBrazil » Wed 27 Jun 2012 16:10

I've created a simple VCL routine and set to connection using an onClick event of a button.

Code: Select all

procedure TForm1.BitBtn1Click(Sender: TObject);
var
 i :integer;
 Host,Port, Server : String;

begin
   SQLQuery1.Close;
   SQLQuery2.Close;
   if SQLConnection2.Connected then  SQLConnection2.Close;

   With SQLConnection2 do begin
    params.Clear;
    ConnectionName := '';
    DriverName     := '';

    ConnectionName := 'DEVART MYSQL DIRECT';
    DriverName := 'DevartMySQLDirect';
    GetDriverFunc := 'getSQLDriverMySQLDirect';
    LibraryName := 'dbexpmda40.dll';
    VendorLib := 'not used';
    Params.values['BlobSize']:='-1';
    Params.values['User_Name']:=edUser.Text;
    Params.values['Password']:=edPwd.Text;
    Host := edName.Text;
    Port := edPort.Text;

    if (Port <> '') and (Port <> '3306') then Host := Host +':'+Port;
    Params.Values['HostName'] := Host;

    if edDB.Text <> '' then
      Params.Values['Database'] := edDB.Text;

    Memo1.Text := 'Params';
    Memo1.Lines.Text := params.Text;

    Try
      open;
    Except
      on E:exception do begin
         Memo1.Lines.Add('============================================');
         Memo1.Lines.Add( e.Message);
      end;

    End;
  end;

  if SQLConnection2.Connected then begin
   with SQLQuery1 do begin
     SQL.Text := 'SELECT @@VERSION';
     try
     Open;
     lbVer.Caption := fields[0].asString;
     finally
       close;
     end;
     Close;
   end;
   SQLQuery2.Open;
  end;
end;
This code works OK.

The Program that fails is an ISAPI DLL and use a Custom class to make the connection.
Host detail are stored before calling the connect routine.

Code: Select all

procedure TMyDB.Connect;
var Server :String;
begin
  Close;
  Params.Clear;
  ConnectionName := '';
  DriverName     := '';

  Server := FHost;
  if FProtocol='mssql' then
  begin
    DriverName    := 'DevartSQLServer';
    GetDriverFunc := 'getSQLDriverSQLServer';
    LibraryName   := 'dbexpsda40.dll';
    VendorLib     := 'sqloledb.dll';
  end
  else
  begin
    ConnectionName := 'DEVART MYSQL DIRECT';
    DriverName     := 'DevartMySQLDirect';
    GetDriverFunc  := 'getSQLDriverMySQLDirect'; 
    LibraryName    := 'dbexpmda40.dll';
    VendorLib      := 'not used';

    if (FPort > 0) then Server := Server + ':'+IntToStr(FPort);
  end;

  // Update Params
  Params.values['BlobSize']:='-1';
  Params.values['HostName']:=Server;
  Params.values['DataBase']:=FDatabase;
  Params.values['User_Name']:=FUser;
  Params.values['Password']:=FPassword;
  Open;
end;
Is the problem due to the procedure being called within an ISAPI dll?

AndreyZ

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by AndreyZ » Mon 02 Jul 2012 12:59

Please try creating a small sample to demonstrate the problem and send it to andreyz*devart*com .

MaBrazil
Posts: 5
Joined: Tue 24 Jan 2012 12:35

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by MaBrazil » Mon 09 Jul 2012 14:49

On further examination of the code, I've found we have two call the our db Connection class.

One Unit create the connection from within a TComponent Class calling

Code: Select all

InitDBase := TTempusDB.Create(self);
and work correctly.
The other within a global unit, without an Owner,

Code: Select all

 UniqueDataBase := TTempusDB.Create(nil);
fails to connect when using DNS names!

I've changed the first unit to use NIL as the owner and it still connects OK.

This failing unit uses another module to get Host and MacAddress of the PC. Is there a chance that the function used by DBExpress to relsolve the host name is been overwriten by on in one of the other units being called?

AndreyZ

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by AndreyZ » Tue 10 Jul 2012 09:25

dbExpress driver for MySQL uses the gethostbyname Windows system function to get the information about the host using its name. I have used the following code:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  con1, con2: TSQLConnection;
begin
  con1 := TSQLConnection.Create(self);
  con1.DriverName := 'Devart MySQL Direct';
  con1.ConnectionName := 'DevartMySQLDirect';
  con1.GetDriverFunc := 'getSQLDriverMySQLDirect';
  con1.LibraryName := 'dbexpmda40.dll';
  con1.VendorLib := 'not used';
  con1.LoginPrompt := False;
  con1.Params.Values['HostName'] := 'DNS:Port';
  con1.Params.Values['Database'] := 'database';
  con1.Params.Values['User_Name'] := 'username';
  con1.Params.Values['Password'] := 'password';
  con1.Open;

  con2 := TSQLConnection.Create(nil);
  try
    con2.DriverName := 'Devart MySQL Direct';
    con2.ConnectionName := 'DevartMySQLDirect';
    con2.GetDriverFunc := 'getSQLDriverMySQLDirect';
    con2.LibraryName := 'dbexpmda40.dll';
    con2.VendorLib := 'not used';
    con2.LoginPrompt := False;
    con2.Params.Values['HostName'] := 'DNS:Port';
    con2.Params.Values['Database'] := 'database';
    con2.Params.Values['User_Name'] := 'username';
    con2.Params.Values['Password'] := 'password';
    con2.Open;
  finally
    con2.Free;
  end;
end;
, and there were no problems with connecting to the MySQL server using DNS names. It seems that this problem is caused by the implementation of your TSQLConnection class. Please try creating a small sample to demonstrate the problem and send it to andreyz*devart*com .

MaBrazil
Posts: 5
Joined: Tue 24 Jan 2012 12:35

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by MaBrazil » Mon 16 Jul 2012 08:12

Found my Problem. :)

Moving the Database setup in the onCreate routine of the ISAPI filter fixed the issue.

Code: Select all

begin
  CoInitFlags := COINIT_MULTITHREADED;  
  // Move within Form OnCreate.
  // initapp.Run;
  Application.CacheConnections := true;
  Application.Initialize;
  Application.WebModuleClass := WebModule1;
  Application.Run;
end.
Odd that allow IP addressing to work. :!:

AndreyZ

Re: Problem using DNS Host name on Delphi XE2 and dbe 5.1.2

Post by AndreyZ » Mon 16 Jul 2012 10:54

It's good to see that you've found a solution. If any other questions come up, please contact us.

Post Reply