Jump to content

UniThreadTimer doesn't work


emin

Recommended Posts

I need to delete some record on a database on every 30 minutes. I added a unithreadtimer on servermodule and used below code. But when I check the database it seems that unithreadtimer doesn't work.

My code:

procedure TUniServerModule.UniGUIServerModuleServerStartup(Sender: TObject);
begin

unithreadtimer1.Enabled:=true;
end;

procedure TUniServerModule.UniThreadTimer1Timer(Sender: TObject);
begin

UniThreadTimer1.Lock;
    try
     with unimainmodule.servermodulesql do
      begin
         close;
         sql.Text:='DELETE from kilitlipoliceler where ROUND(TIME_TO_SEC(timediff(CURRENT_TIME,kilitlemesaati))/60)>30'
         execsql;
      end;
    finally
      UniThreadTimer1.Release;
    end;
end;

Link to comment
Share on other sites

1 hour ago, emin said:

with unimainmodule.servermodulesql do

What i meant was that you can’t call anything in the mainmodule from the servermodule. The mainmodule is linked to a specific user, the servermodule is not (singleton). So the sql and the db connection in the servermodule usually needs to be created in the servermodule. 

Link to comment
Share on other sites

Ok.

 I add a TFDConnection, TFDQuery and TUniThreadTimer on server module. And I created another connection which is independant from mainmodule connection

But the result was the same. Nothing changed on the database.

Link to comment
Share on other sites

You are right. Actually there is no problem with running unithreadtimer. My problem is that I could not manage how to run a query on server to handle backup operation. I checked demos related to unithreadtimer, but non of them use query. I don't want to use unitimer instead of unithreadtimer and run the same query many times.

Link to comment
Share on other sites

Finally I've solved the problem. I set unithreadtimer interval as 600000 and started unithreadtimer when server module create. It worked.

On the other hand I'm not sure is it right to use two fdconnetcion one on mainmodule, onether on servermodule

 

 

Link to comment
Share on other sites

yes, the correct one and as already explained, 
and you create a new connection independent of your database, 
make the connection at the beginning, and at the end disconnect.

    UniThreadTimer1.enabled :=false;
    try
       db :=tfdconnection.create(self);
       db.... you parameters connection load ini ....
       db.open;
       db.execsql('your DELETE....');
    finally
       begin
        db.close; 
        Freeandnil(db);
        UniThreadTimer1.enabled :=true;
       end;
    end;

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...