XDataserver getting DB connection via XDatapool for service operation

Hi Wagner,

I have created a standard XDataServer with automatic CRUD entities and would like to add some service operations which needs database access.

If I understood correct the following code should provide a database connection which could be used in a service operation.

var
Statement: IDBStatement;
ResultSet: IDBResultSet;

Statement := Manager.Connection.CreateStatement;
Statement.SetSQLCommand(TheSQLStatement);
ResultSet := Statement.ExecuteQuery;

  1. Must the Statement and ResultSet not be freed after use?

A new instance of this data module will be created by this code.

  1. Besides FDConnection there are of course other components on the datatamodule.

Doesn't this give a lot of overhead or other possible issues?

That is correct.

No, you don't need to destroy them. As they are reference-counted interfaces, they will be destroyed automatically when they get out of scope.

No.

No, that's the correct way to do so and there are no additional overheads compared to the way CRUD operations are also retrieving connections and executing statements.

Thank you for the explanation.

Statement := Manager.Connection.CreateStatement;

Where can I find Manager?

You can get manager this way:

Manager := TXDataOperationContext.Current.GetManager;

Or you can simply do it this way if you don't use manager:

var
Statement: IDBStatement;
ResultSet: IDBResultSet;

Statement := TXDataOperationContext.Current.Connection.CreateStatement;
Statement.SetSQLCommand(TheSQLStatement);
ResultSet := Statement.ExecuteQuery;

Hi Wagner, thanks again for the great support. That did the trick.

1 Like

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