简体   繁体   中英

Keep connection of ADOConnection in Delphi

I have a data module called MainModule that contain an ADOConnection and another data module called DatabaseModule that contain ADOQuery , ADOStoredProcedure and etc that subclass from the MainModule :

TDatabaseModule= class(TMainModule)
  • This modules are auto create .

in my application all of the classes subclass from DatabaseModule , now think the 2 class like :

TMyClass1= class(TDatabaseModule)

TMyClass2= class(TDatabaseModule)

in my application i create an instance from TMyClass1 and TMyClass2 , both of class finally connect to MainModule's ADOConnection , now when i free one of the classes the connection of MainModule is droped and other class can not access database because my ADOQuery and etc are connected to MainModule's ADOConnection and the connection of it not alive , how can i keep the connection alive ?

Note : I know that i can put the connection in my DatabaseModule and each time classes are create it create an specific instance of connection for that class but it has overload for database , i check it in SQL Server Profiler .

Thanks .

AutoCreation of MainModule and DatabaseModule will create 2 instances of ADOConnection, plus each instance of TDatabaseModule subclass will create one more.

How do yo connect your to TMyClass1 instance (let's say MyObj1) to MainModule.ADOConnection? Like MyObj1.ADOConnection := MainModule.ADOConnection; ?

If you now destroy MyObj1, all it's components get destroyed also, but as you changed MyObj1.ADOConnection reference to MainModule.ADOConnection, MainModule.ADOConnection is the one that you'll loose.

You could move ADOConnection component to another DataModule, set ADOConnection as DatabaseModule (or MainModule) porperty and assign Connection properties of used datasets in code. And you should not auto-create DataModules you do not intend to use at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM