MySQL Forums
Forum List  »  Delphi

Problem with MySQL connection while inserting data
Posted by: Stéphane Monteiro
Date: November 25, 2009 03:08AM

Hi all,

I am working on a Windows service in Delphi/Pascal language and I am using a MySQL database.
I am using (mysql.pas + libmysql.dll) files to work with my database and it works fine.
But, when the service runs and makes hundreds of UPDATE / INSERT, I see the following error in my log file:
"Lost connection to MySQL server at 'reading initial communication packet', system error: 0".
I try to reconnect automatically just after this problem, and the connection fail again, and so on....

I am using WampServer2.0i (DELPHI 6 / MySQL 5.1.36), and the connection with MySQL server at start-up is OK. I can make hundreds of SELECT queries without any problem.

Does anybody have an idea?
Now, I am trying to modifying some parameters in "my.ini" file, that could solve my problem...

Thanks.


------- Here is a my code:
Function to connect to MySQL database:

function DBConnect : boolean;
begin
Result := false;

if mySQL_Res <> nil then
mysql_free_result(mySQL_Res);

mySQL_Res := nil;
if mySQLhandle <> nil then
begin
mysql_close(mySQLhandle);
mySQLhandle := nil;
end;

mySQLhandle := mysql_init(nil);
if mySQLhandle = nil then
begin
Verbose(VRB_ERROR, '*** mysql_init failed !!! ***', []);
Result := false;
Exit;
end;

if mysql_real_connect(mySQLhandle, pChar(CfgCampi.GetMgrServerHost), pChar(CfgCampi.GetMgrDbUsername),
pChar(CfgCampi.GetMgrDbUserpass), pChar(CfgCampi.GetMgrDbPath), 0, nil, CLIENT_MULTI_STATEMENTS) = nil then
begin
Verbose(VRB_ERROR, '*** Connexion to database failed !!! ***', []);
Verbose(VRB_ERROR, 'MySQL Error: %s', [UnicodeString(mysql_error(mySQLhandle))]);
Result := false;
Exit;
end
else
begin
Verbose(VRB_INFO, 'Connected to MySQL server: %s', [UnicodeString(mysql_get_server_info(mySQLhandle))]);
end;

if (mysql_set_character_set(mySQLhandle, pChar('UTF8')) <> 0) then
begin
Verbose(VRB_ERROR, 'MySQL Error: %s', [UnicodeString(mysql_error(mySQLhandle))]);
end;

Result := not (Result);
end;


Function to do INSERT/UPDATEs:

procedure mysql_insertdata(query : string);
begin

if not DBConnected then // Uses 'mysql_ping' to test the connection
Exit;

try
if mysql_real_query(mySQLhandle, PAnsiChar(query), Length(query)) <> 0 then
begin
Verbose(VRB_ERROR, 'MySQL Error: %s', [UnicodeString(mysql_error(mySQLhandle))]);
end;

except
on e: Exception do
begin
Verbose(VRB_ERROR, '---- Exception inserting qry : %s', [query]);
Verbose(VRB_ERROR, 'MySQL Error: %s', [UnicodeString(mysql_error(mySQLhandle))]);
Verbose(VRB_ERROR, '%s', [e.Message]);
end;
end;
end;

--------- Another point, user limits:
I already verified these parameters (max_connections, max_updates, max_user_connections, max_questions) and there are all at 0. This means that there are unlimited, based on MySQL documentation.

Options: ReplyQuote


Subject
Written By
Posted
Problem with MySQL connection while inserting data
November 25, 2009 03:08AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.