RTC Forums
November 24, 2024, 03:39:06 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Sharing DB Connection with firedac on Server Application  (Read 5145 times)
mkurnia
RTC Expired
*
Posts: 21


« on: November 17, 2015, 01:16:10 AM »

Hello Anyone,

I was new RTC SDK users to upgrade my previous application using DataSnap. but I find obstacles to implement the database connection. where before I use it the way there like on the blog Andreano lanuse(http://www.andreanolanusse.com/en/sharing-db-connection-between-multiples-datasnap-server-modules/).
In RTC I have not gotten TDSSessionManager.GetThreadSession.Id like in DataSnap.

so this time I made the connection at runtime just like the following:
Code:
procedure TdmServer.InsertMemberFuncExecute(Sender: TRtcConnection;
  Param: TRtcFunctionInfo; Result: TRtcValue);
var
  conn: TFDConnection;
  Member: TMEMBERS;
begin
  conn := TFDConnection.Create(nil);
  Member := TMEMBERS.Create;
  try
    with conn do
    begin
      Params.Values['DriverID'] := 'FB';
      Params.Values['Server'] := 'localhost';
      Params.Values['Database'] := sLocationDb;
      Params.Values['User_Name'] := sUser_Name;
      Params.Values['Password'] := sPassword;
      LoginPrompt := False;
      Connected := True;
    end;
    Member.ConDB := conn;
    ..................................
    ................................

    Member.ConDB := nil;
    conn.Close;
  finally
    FreeAndNil(Member);
    FreeAndNil(conn);
  end;
end;

there anything you can share the right way.
Thank you.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: November 17, 2015, 08:00:19 AM »

Here is a FAQ topic about writing a Database Connection Pool.

Best Regards,
Danijel Tkalcec
Logged
mkurnia
RTC Expired
*
Posts: 21


« Reply #2 on: November 17, 2015, 09:15:05 AM »

how to use it properly,
whether the class at the runtime when datamodule create a server in the create?

Code:
procedure TdmServer.DataModuleCreate(Sender: TObject);
begin
  DBPool := TDBPool.Create;
  with DBPool do
  begin
    db_server :=  'localhost';
    db_path := sLocationDb;
    db_username := sUser_Name;
    db_password := sPassword;
    AddDBConn;
  end;
  
end;

then use
Code:
procedure TdmServer.InsertMemberFuncExecute(Sender: TRtcConnection;
  Param: TRtcFunctionInfo; Result: TRtcValue);
var
  Member: TMEMBERS;//class my table
begin
  Member := TMEMBERS.Create;
  try
    Member.ConDB := DBPool.GetDBConn;
    ..................................
    ................................

    Member.ConDB := nil;
  finally
    FreeAndNil(Member);
  end;
end;
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: November 17, 2015, 09:21:00 AM »

Read the FAQ Article. It is all explained there. In a nutshell, you create the Pool at Application start, then use the GetDBConn method to get a connection from the pool and PutDBConn to place the connection back into the pool after use. Do NOT forget to place the connection back into the pool after use, or you will soon end up with a lot of open connections floating around, resulting in memory leaks and eventually crashing your Application Server!

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.024 seconds with 16 queries.