Search code examples
delphibde

Shut down BDE from code


We have a legacy application uses the BDE. (The BDEcontinues to work surprisingly well, given its age).

There are times when our app needs to manipulate folders (Rename, move, etc.) but a .NET or .LCK file remains open in the folder, preventing that. We have been unable to find any table or query still open in our code.

Other than having our program shell to a non-BDE program and itself terminating, is there a programatic way for us to shut down the BDE, which would unlock these files.

Standard disclaimers : Yes, the BDE is dead. Yes, we should migrate to a more modern database. Yes, someday the BDE just won't work anymore. With almost 2 million lines of legacy code, migrating (even with a somewhat plug compatible platform like Sybase Advantage) isn't an inexpensive project, which is why we haven't done it yet...


Solution

  • You normally don't have anything specific to do for shutting down the BDE.
    All the BDE Sessions are freed in the Finalization section of DBTables. This will close everything and when the Default Session is destroyed as well it will call DbiDLLExit if needed, then DbiExit, both from the BDE unit.

    Now, if you want to shutdown the BDE before, I suggest you do mimic the finalization then initialization parts of DBTables (disclaimer: limited testing, use carefully...) like:

    procedure BDEKill;
    begin
    // from finalization
      Sessions.Free;
      Sessions := nil;
    end;
    
    procedure BDEReStart;
    begin
    // from initialization
      Sessions := TSessionList.Create;
      Session := TSession.Create(nil);
      Session.SessionName := 'Default'; { Do not localize }
    end;
    

    and use it like:

    BDEKill;
    try
      // move my folders
    finally
      BDEReStart;
    end;