Featured | Andreano Lanusse | Technology and Software Development Where Andreano Lanusse talk about technology, software development, programming techniques, databases, games and more through articles, tutorials and videos Sat, 17 Mar 2012 22:45:20 +0000 en hourly 1 https://wordpress.org/?v=6.3.4 Sharing DB connection between multiples DataSnap Server Modules http://www.andreanolanusse.com/en/sharing-db-connection-between-multiples-datasnap-server-modules/ http://www.andreanolanusse.com/en/sharing-db-connection-between-multiples-datasnap-server-modules/#comments Fri, 10 Dec 2010 19:12:54 +0000 http://www.andreanolanusse.com/en/?p=396 When developers starts creating Delphi DataSnap application it is very common to see the database connection defined per Data Module. Doing this will generate a lot of connections on the database, and depending on the situation it will became a problem. In Delphi XE, DataSnap introduce the Session Management that will make easy to implement […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
When developers starts creating Delphi DataSnap application it is very common to see the database connection defined per Data Module. Doing this will generate a lot of connections on the database, and depending on the situation it will became a problem.

In Delphi XE, DataSnap introduce the Session Management that will make easy to implement the control on the server side to manage the database connection for the clients. The client application won’t know anything about that, the server will do the magic.

When we create a DataSnap Server it is best practice to define a Server Container (Data Module), which contains the DataSnap Server component and registers all the server classes required by the application. In this container we will define the method responsible for dealing with the database connections for each Server Class.

As an example, I have implemented a GetConnection method on the Server Container. This method is responsible for assigning the connection looking in to the connection pool, which will a have list of connections per client.

  private
    { Private declarations }
    ListofConnection : TDictionary;
  public
    function GetConnection : TSQLConnection;

In case the server receives new requests from new clients the GetConnection will create a new connection and add to the pool list. If the client already have a connection associated, the GetConnection will just return a instance of SQLConnection. The pool uses the Thread ID to control the unique connection per client. If you use DataSnap 2010 you have to use the method GetThreadSession for this purpose.

function TServerContainer1.GetConnection: TSQLConnection;
var
  dbconn : TSQLConnection;
begin

  if ListofConnection.ContainsKey(TDSSessionManager.GetThreadSession.Id) then
     Result := ListofConnection[TDSSessionManager.GetThreadSession.Id]
  else
  begin
    dbconn := TSQLConnection.Create(nil);
    dbconn.Params.Clear;
    dbconn.LoadParamsOnConnect := true;
    dbconn.ConnectionName := 'DS Employee';

    ListofConnection.Add(TDSSessionManager.GetThreadSession.Id, dbconn);
    Result := dbconn;
  end;

end;

Since the connection is defined we need to update all datasets to use this connection, as well the server methods that create and execute SQL queries at run-time will have to invoke the GetConnection.

If you are using the VCL Data components (TSQLQuery, TSQLStoredProc, etc…) on your Server DataModules, the onCreate event is a good place to associate the DataSets with the connection, using the following code.

procedure TServerContainer1.SetConnection(Conn: TSqlConnection);
var
  i: integer;
begin
  if Conn = nil then
     Conn := GetConnection;
  else
    Conn := Sender;

  for i := 0 to ComponentCount - 1 do
    if Components[i] is TSQLQuery then
       TSQLQuery(Components[i]).SQLConnection := Conn;
end;

To avoid connection leaks on the database, we implement the onDisconnect event from DSServer. This will be executed when the client disconnects.

  if GetConnection <> nil then
     GetConnection.Close;

The source code is available for download at Code Central.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/sharing-db-connection-between-multiples-datasnap-server-modules/feed/ 6
White Paper: Reasons to Migrate to Delphi XE – What you might have missed since Delphi 7 http://www.andreanolanusse.com/en/white-paper-reasons-to-migrate-to-delphi-xe-what-you-might-have-missed-since-delphi-7/ http://www.andreanolanusse.com/en/white-paper-reasons-to-migrate-to-delphi-xe-what-you-might-have-missed-since-delphi-7/#comments Wed, 17 Nov 2010 05:45:45 +0000 http://www.andreanolanusse.com/en/?p=401 Delphi community, many of you have been looking for compelling reasons to migrate to Delphi XE, each developer in a old version of Delphi. This article gives a few good reasons to migrate, along with an overview of the most important features added to Delphi product releases since version 7. If you already read my […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
Delphi community, many of you have been looking for compelling reasons to migrate to Delphi XE, each developer in a old version of Delphi.

This article gives a few good reasons to migrate, along with an overview of the most important features added to Delphi product releases since version 7. If you already read my article “Reasons to migrate from Delphi 7 to Delphi 2009”, this article include updates related with Delphi 2010 and XE.

We understand that developers are busy creating software, and that spending time to migrate to a new version is not always possible since you always have to deliver new projects.

Possibly the best reason for you to migrate to Delphi XE, C++Builder XE or RAD Studio XE, is that buying those versions gets you earlier versions at no extra cost. For example buying Delphi XE also gets you Delphi 7, Delphi 2007, Delphi 2009 and Delphi 2010.

I hope this article answer all of your question, any additional question let us know how.

Article download.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/white-paper-reasons-to-migrate-to-delphi-xe-what-you-might-have-missed-since-delphi-7/feed/ 13
“Linq to Directories/Files” – Enumerating Directories and Files with Delphi Prism and .NET 4 http://www.andreanolanusse.com/en/linq-to-directoriesfiles-enumerating-directories-and-files-with-delphi-prism-2011-and-net-4/ http://www.andreanolanusse.com/en/linq-to-directoriesfiles-enumerating-directories-and-files-with-delphi-prism-2011-and-net-4/#comments Tue, 01 Jun 2010 13:54:59 +0000 http://www.andreanolanusse.com/en/?p=307 .NET 4 allow you to enumerate directories and files by using methods that return an enumerable collection of strings of their names, also you can use methods that return an enumerable collection of DirectoryInfo, FileInfo, or FileSystemInfo objects, these objects are very powerfull and make easy the development. Before .NET 4 you could only obtain […]

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
.NET 4 allow you to enumerate directories and files by using methods that return an enumerable collection of strings of their names, also you can use methods that return an enumerable collection of DirectoryInfo, FileInfo, or FileSystemInfo objects, these objects are very powerfull and make easy the development. Before .NET 4 you could only obtain arrays of these collections. Enumerable collections provide better performance than arrays.

Delphi Prism XE provide full support for .NET 4. the follow examples shows how to enumerate Directories and Files.

Enumareting directory names – this samples list all directories under “c:\Program Files\”

class method ConsoleApp.Main(args: array of System.String);
begin
  try
    var dirPath: System.String := 'c:\Program Files';

    // LINQ query.
    var dirs :=  from dir in Directory.EnumerateDirectories(dirPath) select dir;

    // Show results.
    for each dir in dirs do begin
      // Remove path information from string.
      Console.WriteLine('{0}', dir.Substring(dir.LastIndexOf('\') + 1))
    end;
    Console.WriteLine('{0} directories found.', dirs.Count().ToString());

    // Optionally create a List collection.
    var workDirs: List := new List(dirs)
  except
    on UAEx: UnauthorizedAccessException do begin
      Console.WriteLine(UAEx.Message)
    end;
    on PathEx: PathTooLongException do begin
      Console.WriteLine(PathEx.Message)
    end;
  end;
  Console.ReadKey;

end;

Enumerating file names in all directories – this sample search all TXT files which contains the word “license”

class method ConsoleApp.Main(args: array of System.String);
begin

  try

    var files :=  from sfile in Directory.EnumerateFiles('c:\', '*.txt', SearchOption.AllDirectories)
                  from line in File.ReadLines(sfile)
                  where line.Contains('license')
                  select new class( File := sfile, Line := line);

    for each f in files do begin
      Console.WriteLine('{0}'#9'{1}', f.File, f.Line)
    end;

    Console.WriteLine('{0} files found.', files.Count().ToString());
  except
    on UAEx: UnauthorizedAccessException do begin
      Console.WriteLine(UAEx.Message);
    end;
    on PathEx: PathTooLongException do begin
      Console.WriteLine(PathEx.Message);
    end;
  end;

  Console.ReadKey;

end;

Enumerating a collection of DirectoryInfo objects – this sample list all directories created before April of 2010

class method ConsoleApp.Main(args: array of string);
begin

  var dirPrograms: DirectoryInfo := new DirectoryInfo('c:\program files');
  var StartOf2010: DateTime := new DateTime(2010, 4, 1);

  // LINQ query for all directories created before April of 2010.
  var dirs :=  from dir in dirPrograms.EnumerateDirectories()
               where dir.CreationTimeUtc < StartOf2010
               select new class (ProgDir := dir);
  // Show results.
  for each di in dirs do begin
    Console.WriteLine('{0}', di.ProgDir.Name)
  end;

  Console.ReadKey;
end;

You can download the Delphi Prism 2011 Trial here.

Andreano Lanusse | Technology and Software Development Follow me on Twitter: @andreanolanusse

]]>
http://www.andreanolanusse.com/en/linq-to-directoriesfiles-enumerating-directories-and-files-with-delphi-prism-2011-and-net-4/feed/ 4