Zeos connection library caching
Posted: 02.02.2024, 18:38
I know we already talked about this but this time I'm bringing a solution proposal too.
The thing is, Zeos is automatically caching driver libraries, leaving the handles open even after a connection to a database is closed. You can reproduce the "issue" quite easily: drop a TZConnection on a form, set it to a database of your choice, finally call ZConnection1.Connect and ZConnection1.Disconnect right after it. Once the application is running, try to delete the driver .dll - it won't work as your program is still using it.
Caching is done in the TZAbstractDriver level and the cache can manually be cleared (therefore handles released) like this:
To be honest I don't know why this was implemented at the first place as loading the library will only be a fraction of the time spent compared to actually establisthing the connection but as I'm not questioning the viability of such cache. As I can not imagine a scenario when switching between cached and non-cached mode is actually needed I suggest to create a conditional directive like ZEOS_DISABLE_DRIVER_CACHE. When defined, I'd not even create the FCachedPlainDrivers instances to save some more resources.
What do you guys think about this solution? If agreed I can create a pull request even tonight with the changes.
The thing is, Zeos is automatically caching driver libraries, leaving the handles open even after a connection to a database is closed. You can reproduce the "issue" quite easily: drop a TZConnection on a form, set it to a database of your choice, finally call ZConnection1.Connect and ZConnection1.Disconnect right after it. Once the application is running, try to delete the driver .dll - it won't work as your program is still using it.
Caching is done in the TZAbstractDriver level and the cache can manually be cleared (therefore handles released) like this:
Code: Select all
Type
THackAbstractDriver = Class(TZAbstractDriver);
Var
drivers: IZCollection;
driver: TZAbstractDriver;
a: Integer;
Begin
drivers := DriverManager.GetDrivers;
For a := 0 To drivers.Count - 1 Do
Begin
driver := drivers[a] As TZAbstractDriver;
THackAbstractDriver(driver).FCachedPlainDrivers.Clear;
End;
End;
What do you guys think about this solution? If agreed I can create a pull request even tonight with the changes.