RTC Forums
April 20, 2024, 06:37:45 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Add object container into TRtcConnection  (Read 3232 times)
ISIGest
RTC Expired
*
Posts: 121


« on: April 19, 2016, 02:20:00 PM »

Can you add an object container field (or property) to TRtcConnection class that autofree when object is destroyed.
This can be useful to add and retrieve an object handled by connection or thared in MultiTheared application, like a DB pooled connection for example.
When you create an aplication that need db connection the majior problem is to handle connection in multithread, to solve this we can check if the DBConnection exist in the object list (field or property) of TRtcConnection and get it or create it if does not exist.
This connection must free (aumatically) when the connecction will be freed.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: April 20, 2016, 10:00:28 AM »

TRtcConnection component already has the "Info:TRtcInfo" property, which can be used to store any kind of connection-related information and data. TRtcInfo class extends the TRtcRecord class, which allows you to store any kind of data in a simple-to-use RTC record. To store pointers to persistent objects, which you do NOT want to have destroyed with the connection, simply use the "Info.Obj[]" property.

For objects which you DO WANT to be AUTO-FREED with the connection, you can implement your own custom object container by extending the "TRtcObject" class and implementing the "Kill" method, where you should free all the memory and objects your container "contains", as well as the container itself.

Here is a dummy "container" class extending the TRtcObject class, whose instances can be stored in the "Info.Obj[]" property on the "Sender:TRtcConnection" component and will be autofreed when the connection component is destroyed ...

interface
type
  TMyContainer=class(TRtcObject)
  public
    procedure Kill; override;
    end;
implementation
procedure TMyContainer.Kill;
  begin
  // Make sure the destructor for our container will be called NOW ...
  {$IFDEF NEXTGEN} DisposeOf; {$ELSE} Free; {$ENDIF}
  end;

Naturally, that is just a dummy shell class, making sure the container will be autofreed if assigned to the Info.Obj[] property. To store useful information in your container, you will also need a constructor (so you can create and initialize your container), a destructor (to free memory and objects stored), as well as methods and/or properties for accessing the objects and/or data it contains.

If you need more examples, check the "TRtcFileStream" and/or "TRtcObjectManager" classes in the rtcInfo unit, which extend the TRtcObject class to make sure their instances will be autofreed when assigned to the Info.Obj[] property.

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.022 seconds with 16 queries.