How to use TSession Class of BDE in Delphi? -
i using mutiple connection using odbc. in whole project using same connection, create, use , destory tquery object. going use connection in threads , came know delphi bde provides tsession class that. want know how use tsession concurrent operation, please provide code sample if possible.
while agree bde old, possible create thread-safe access database using bde , tsessions.
consider this. when 2 copies of same application running @ same time, database engine or database server distinguishes between 2 instances purpose of record , table locking. distinction possible because each application uses separate connection, or in case of bde, session.
the session represented tsession instance. in single threaded projects tsession created you. if want connect bde 2 or more threads, each should have own tsession.
using multiple tsessions demonstrated here, in old code example dug (it old, , differently today, asked it). trick each session needs have same network directory , have unique private directory. here tthread descendant:
type twritedata = class(tthread) private fsql: string; ffilename: string; protected procedure execute; override; public constructor create(createsuspended: boolean; const sql: string; const filename: string); override; overload; end;
here overridden constructor:
constructor twritedata.create(createsuspended: boolean; const sql: string; const filename: string); begin inherited create(true); fsql := sql; ffilename := string; end;
and here execute method. importantly, tsession.privatedir set unique directory name (based on threadid). use guid, or other value, long unique. note session1 tsession component on data module, , query1 tquery uses tdatabase (database1), in turn uses session1. session variable declared in bde.dbtables unit. variable refers default tsession bde creates bde tdatasets active in primary thread of execution.
procedure twritedata.execute; var datamod: tdatamodule1; appdir: string; begin appdir := extractfilepath(application.exename); datamod := tdatamodule1.create(nil); try datamod begin //all sessions need unique private directory session1.privatedir := appdir + inttostr(self.threadid); //all sessions share common network control file session1.netfiledir := session.netfiledir; forcedirectories(session1.privatedir); try query1.sql.text := fsql; clientdataset1.open; clientdataset1.savetofile(appdir + ffilename); clientdataset1.close; sysutils.removedir(session1.privatedir); end; //try end; //begin datamod.free; end; end;
i hope helps.
Comments
Post a Comment