Database Connections

Connections to a database are an important resource to consider for application scalability. As recommended, for each session a connection object should be placed on MainModule. A new instance of the connection object will be created when a new session is initiated, so in a typical scenario each session will establish a new connection to the database server.

This can pose a scalability issue if the database server can only serve a limited number of simultaneous connections. Many database servers have default limits of a few hundred connections, while others can handle thousands. Database connections must be treated as limited resources and your application design should avoid reaching the upper limit.

You can configure the server to allow more simultaneous connections, but that is not the ideal way to solve the problem. The recommended approaches are:

1

Enabling Connection Pooling

Connection pooling is a method to re-use database connections with the same properties. Some database libraries implement pooling by default while others don't — check your particular connection library to see if pooling is available.

Examples:

  • The TADOConnection component combined with Microsoft SQL Server enables pooling by default.

  • FireDAC also supports pooling and can be enabled. See the FireDAC section for details: https://unigui.com/doc/online_help/firedac.htm

2

Using a Middleware Library

When the number of concurrent sessions exceeds the database server's upper limit, a middleware layer can help manage connections efficiently. Middleware creates a connection when needed and releases it when no longer used, allowing many sessions to be served with a smaller number of simultaneous connections. Some middleware libraries also implement pooling to improve performance.

Common middleware libraries in Delphi:

  • DataSnap — http://docwiki.embarcadero.com/RADStudio/Seattle/en/Developing_DataSnap_Applications

  • DataAbstract — http://www.dataabstract.com/da/

  • kbmMW — http://news.components4developers.com/products_kbmMW.html

Also see the Embarcadero middleware product page: http://www.embarcadero.com/products/rad-studio/middleware