Closing Database Connections

From RAD Studio
Jump to: navigation, search

Go Up to Managing database sessions Index

Attention: The Borland Database Engine (BDE) has been deprecated, so it will not be enhanced. For instance, BDE will never have Unicode support. You should not undertake new development with BDE. Consider migrating your existing database applications from BDE to dbExpress.

To close an individual database connection, call the CloseDatabase method. When you call CloseDatabase, the reference count for the database, which is incremented when you call OpenDatabase, is decremented by 1. When the reference count for a database is 0, the database is closed. CloseDatabase takes one parameter, the database to close. If you opened the database using the OpenDatabase method, this parameter can be set to the return value of OpenDatabase.

Session.CloseDatabase(DBDemosDatabase);
Session->CloseDatabase(BCDemosDatabase);

If the specified database name is associated with a temporary (implicit) database component, and the session's KeepConnections property is False, the database component is freed, effectively closing the connection.

Note: If KeepConnections is False, temporary database components are closed and freed automatically when the last dataset associated with the database component is closed. An application can always call CloseDatabase prior to that time to force closure. To free temporary database components when KeepConnections is True, call the database component's Close method, and then call the session's DropConnections method.
Note: Calling CloseDatabase for a persistent database component does not actually close the connection. To close the connection, call the database component's Close method directly.

There are two ways to close all database connections within the session:

  • Set the Active property for the session to False.
  • Call the Close method for the session.

When you set Active to False, Delphi automatically calls the Close method. Close disconnects from all active databases by freeing temporary database components and calling each persistent database component's Close method. Finally, Close sets the session's BDE handle to nil.

See Also