TXDataAureliusModel.Get is not thread safe, what is the best workaround?

Hello Wagner,

I don't know, should TXDataAureliusModel.Get be thread safe?

I have this code:

procedure TdmSngModel.DoSetConnection( const AUrl, AToken, AModel: String);
  DoLog( loaTraceAll, '~~ DoSetConnection ~ Enter ~ ' + AModel);
  FToken := AToken;
  FConnectedModel := AModel;
  FXDataModel := TXDataAureliusModel.Get( FConnectedModel);
  xClient := TXDataClient.Create( FXDataModel);
  xClient.Uri := AUrl;
  xClient.HttpClient.OnSendingRequest := xClientOnSendingRequest;
//  xClient.ReturnedInstancesOwnership := TInstanceOwnership.None;
  xsngFunc := xClient.Service<IsngFunc>;
  FDatasetControl := TDatasetControl.Create( Self);
  DoLog( loaTraceAll, '~~ DoSetConnection ~ Exit ~ ' + AModel);

The line: FXDataModel := TXDataAureliusModel.Get( FConnectedModel); makes the problem.

It often happens that this method is called multiple times. Of course from different threads.
I have a log from a second "Enter" before the first "Enter" logs "Exit".
There is an exception at the second entry at the Model.Get position.
"Duplikate nicht zulässig" in german. I think in english version: "Duplicates not allowed".
This is triggered by a TDictionary.

I'm not sure if that's the trigger. Debugging is difficult.

Can I get the model in a thread-safe manner?


This solution seems to work.
But is this really good?

  TThread.Synchronize( nil, procedure
      FXDataModel := TXDataAureliusModel.Get( FConnectedModel);

No, ist not ok.
I can't terminate the program.

I think it's because of the different threads.

I keep trying...


I set up a dummy connection right when I start so that the model is entered once.

I hope for a better solution.

Thank you

That's actually what you should do, initialize the model before any thread use it.
Here is more reference about it:

OK thanks.
That won't be a problem.
Despite previous searches, I hadn't found the answers.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.