Access datamodule in service

Hello,

is it possible to access the datamodule in a service procedure?

I'd like to have other pooled ressources on the datamodule like a sender to RabbitMQ or a soap connection to another webservice. These connections are expensive to create in every service operation similar to create and establish a database connection and therefore I'd like to access them pooled like IDBConnection.

Is there a simple way to access the datamodule and map it to the original Tdatamodule?

Regards
Harald

Yes, you can access any TDataModule object from a service operation, it's just Delphi code.

All you need to be sure is that it's thread-safe because you might have several service operations accessing the same global data module at the same time.

I'm not sure what do you mean by map a data module to an original TDataModule?

that was, what I had in mind, when asking my question: thread-safety 


I thought that thconcept is like this: you have a pool of max. tdatamodules and if I need an IDBConnection in the service I get one connection of one TDataModule out of this pool. Your answer seems to tell that there is only one TDatamodule and then a pool of Connections. So my question is more like this: How can I make sure to get a unique object? Is there a pooling-concept I can use for other objects to make it thread-safe?
Regards
Harald

The IDBConnection pool is just for the database connections.

But TMS Sparkle itself has internal classes that implement generic object pool (which are in turn used to implement the IDBConnection pool).
Those are undocumented but you can check the source code at Sparkle.Sys.ObjectPool unit.
There you have a TObjectPool<T> class which you can use for that. Constructing such class requires a size parameter and a function that creates an instance of T. You can use such object to use a object pool of your data modules.
Hello,

thank You that is exactly what I was looking for. Just one more question: Is there a built-in way to do the cleanup, to delete the objects after a spezified amount of time. Or do I have to run an extra thread to check the time?

Regards
Harald

Yes:



Pool.CleanUp(60);


will remove every item from the pool that has not been used in the last minute (60 seconds).
thanks, yes, but I have to call this somewhere. And therefore I need a thread that calls it periodically - or where would You call it?

Regards
Harald

Yes, it's up to you when and how to call it.