Hi Folks,
I got two procedures that are connected to MariaDB 5.6,
My goal is if my application was launch for a long time, it must not lost the connection with my database and if the connection to the database was lost due to any reasons and restored (MariaDB running again), the procedure must still connect to the database without the application been restarted.

I have initialized the connection to the database using TSQLConnection to avoided the client library to trough away the connection, PLEASE see below.

procedure TdataModuleMySQL.createConnection;
begin
  SQLConnection1 := TMySQL55Connection.Create(nil);
  SQLConnection1.HostName:='127.0.0.1';
  SQLConnection1.DatabaseName:='first';
  SQLConnection1.UserName:='erickom';
  SQLConnection1.Password:='tux.';
end;
procedure TdataModuleMySQL.startDatabase;
begin
   //Start of connection
   createConnection;
   //Open the connection & transaction
   SQLConnection1.Connected:=True;
   SQLConnection1.Transaction:=SQLTransaction1;
   SQLTransaction1.DataBase:=SQLConnection1;
end;

I use the first procedure to INSERT data in the database via TLabeledEdit component and has been tested successfully , see below:

procedure TfrmMySQLTest.bitBtnInsertClick(Sender: TObject);
begin
try
  dataModuleMySQL.startDatabase;
  //Start of execution statment
  dataModuleMySQL.queryAtInsert;
dataModuleMySQL.SQLQuery1.SQL.Text:='INSERT INTO student (names, dob) VALUES (:getNAMES, :getDOB);';
dataModuleMySQL.SQLQuery1.Params.ParamByName('getNAMES').AsString:=lblEdtNames1.Text;
dataModuleMySQL.SQLQuery1.Params.ParamByName('getDOB').AsString:=lblEdtDOB1.Text;
  //This lets you know via the label if you are connected or not.
  if dataModuleMySQL.SQLConnection1.Connected then
  begin
    lblStatus1.Caption:='Connected';
    try
       //This starts the query which was created in the connection string
       dataModuleMySQL.SQLQuery1.ExecSQL;
       //Close the database
       dataModuleMySQL.stopDatabase;
    except
showMessage('Execution of your query with error; APP can not continued. ');
    end;
  end;
except
  lblStatus1.Caption:='Not Connected';
showMessage('Database Connection Error. The database may be shutdown or network issue.');
end;

The second procedure is used to SELECT and display in the TDBGrid (Successfully done) but lost the connect after the database been shutdown or when the application been running for long without activities, see below:

procedure TdataModuleMySQL.queryGrid;
begin
   SQLQueryGrid.DataBase:=SQLConnection1;
   SQLQueryGrid.Transaction:=SQLTransactionGrid;
   DataSource1.DataSet:=SQLQueryGrid;
   SQLQueryGrid.SQL.Text:='SELECT * FROM student;';
end;

procedure TfrmMySQLTest.btnGridClick(Sender: TObject);
begin
try
  dataModuleMySQL.startDatabase;
  //Start of execution statment
  dataModuleMySQL.queryGrid;

  DBGrid2.DataSource:=dataModuleMySQL.DataSource1;

  //This lets you know via the label if you are connected or not.

  if dataModuleMySQL.SQLConnection1.Connected then
  begin
          lblStatus3.Caption:='Connected';
               //This will also pass the inormation through to the GRID
//This starts the query which was created in the connection string
          try
             dataModuleMySQL.SQLQueryGrid.Open;
          except
showMessage('Execution of your query with error; APP can not continued.');
          end;
  end;
except
      lblStatus3.Caption:='Not Connected..';
showMessage('Database Connection Error. The database may be shutdown or network issue.');
      dataModuleMySQL.createConnection;
end;
end;

I don't know if the datasource can be the problem or not? or it is a normal behave?

Thank you in advance

--
_______________________________________________
Lazarus mailing list
[email protected]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to