您好,登錄后才能下訂單哦!
本篇內容主要講解“如何使用Zeos數據庫連接池”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用Zeos數據庫連接池”吧!
unit ZeosConPool; interface uses SysUtils, Windows, Classes, IniFiles, DB, ZAbstractRODataset, ZDataset, ZAbstractConnection, ZConnection, ZCompatibility; type TSQLConntionRecord = record HostName: string; Port: Integer; UserName: string; DBName: string; MyDataBase: string; Password: string; end; TSQLConnectionPool = class private fDataBase: string; fServer: string; fPort: integer; fUserName: string; // 數據庫用戶 fPassWord: string; // 密碼 FConList: TThreadList; // 池容器 function TestConnection(con: TzConnection): boolean; function CreateNewConnection: TZConnection; function GetConnectionRecord: TSQLConntionRecord; public function Pop: TZConnection; procedure Push(con: TZConnection); constructor Create; destructor Destroy; override; end; TQryPool = class private function GetQry: TZQuery; procedure con(qry: TZQuery); procedure discon(qry: TZQuery); public function Require: TZQuery; procedure Return(qry: TZQuery); end; function GetQueryJsonData(sql: string): string; var ConnectionPools: TSQLConnectionPool; QryPools: TQryPool; implementation function _DataSetToJson(ADataset: TDataSet): string; // [{“CityId”:”18”,”CityName”:”西安”},{“CityId”:”53”,”CityName”:”廣州”}] var LRecord: string; LField: TField; i: integer; begin Result := ''; if (not ADataset.Active) or (ADataset.IsEmpty) then Exit; Result := '['; ADataset.DisableControls; ADataset.First; while not ADataset.Eof do begin for i := 0 to ADataset.FieldCount - 1 do begin LField := ADataset.Fields[i]; // 取得當前字段 if LRecord = '' then LRecord := '{"' + LField.FieldName + '":"' + LField.Text + '"' else LRecord := LRecord + ',"' + LField.FieldName + '":"' + LField.Text + '"'; if i = ADataset.FieldCount - 1 then begin LRecord := LRecord + '}'; if Result = '[' then Result := Result + LRecord else Result := Result + ',' + LRecord; LRecord := ''; end; end; ADataset.Next; end; ADataset.EnableControls; Result := Result + ']'; end; { TSQLConnectionPool } constructor TSQLConnectionPool.Create; begin FConList := TThreadList.Create; end; destructor TSQLConnectionPool.Destroy; var i: Integer; begin with FConList.LockList do try for i := Count - 1 downto 0 do begin TZConnection(Items[i]).Disconnect; TZConnection(Items[i]).Free; end; finally FConList.UnlockList; end; FConList.Free; end; //獲取SQL連接對象 function TSQLConnectionPool.CreateNewConnection: TZConnection; var con: TZConnection; RecCon: TSQLConntionRecord; begin Result := nil; try con := TZConnection.Create(nil); RecCon := GetConnectionRecord; try with con do begin Protocol := 'MySQL'; // 數據庫驅動類型 LibraryLocation := 'libmysql.dll'; ClientCodepage := 'utf8'; LoginPrompt := False; HostName := RecCon.HostName; User := RecCon.UserName; Password := RecCon.Password; Database := RecCon.DBName; Port := RecCon.Port; Connect; end; Result := con; except on E: exception do begin Result := nil; con.Free; // 打印日志。。。。 end; end; except end; end; //獲取配置SQL連接參數 function TSQLConnectionPool.GetConnectionRecord: TSQLConntionRecord; var dbIni: TIniFile; begin dbIni := TIniFile.Create(ExpandFileName(ExtractFilePath(ParamStr(0)) + '\DataBase.ini')); try with Result do begin HostName := dbIni.ReadString('Database', 'Host', '127.0.0.1'); Port := dbIni.ReadInteger('Database', 'Port', 3306); UserName := dbIni.ReadString('Database', 'UID', 'root'); DBName := dbIni.ReadString('Database', 'Database', 'klys'); MyDataBase := UpperCase(dbIni.ReadString('Database', 'DataBaseType', 'MySql')); Password := dbIni.ReadString('Database', 'Password', '1234567'); end; finally dbIni.Free; end; end; //彈出SQL連接對象 function TSQLConnectionPool.Pop: TZConnection; begin with FConList.LockList do try if Count > 0 then begin Result := TZConnection(Items[Count-1]); Delete(Count-1); { if not TestConnection(Result) then begin Result.Free; Result := Pop; // 遞歸 end; } end else begin Result := CreateNewConnection; end finally FConList.UnlockList; end; end; //回收SQL連接對象 procedure TSQLConnectionPool.Push(con: TZConnection); begin if con <> nil then with FConList.LockList do try // Insert(0, con); Add(con); finally FConList.UnlockList; end; end; //測試連接池中的SQL對象是否存活 function TSQLConnectionPool.TestConnection(con: TZConnection): boolean; begin Result := false; try // con.ExecuteDirect('delete from zd_user where 1<>1'); Result := true; except on E: exception do begin // 實際應用,一定要打印日志 end; end; end; { TQryPool } //qry 關聯SQL Connection procedure TQryPool.con(qry: TZQuery); var sqlcon: TZConnection; begin sqlcon := ConnectionPools.Pop; qry.Connection := sqlcon; end; //回收SQL Connetion 對象 procedure TQryPool.discon(qry: TZQuery); begin ConnectionPools.Push(TZConnection(qry.Connection)); end; //獲取對象 function TQryPool.GetQry: TZQuery; var qry: TZQuery; begin qry := TZQuery.Create(nil); con(qry); Result := qry; end; //彈出Qry對象 function TQryPool.Require: TZQuery; begin Result := GetQry; end; //獲取Qry對象 procedure TQryPool.Return(qry: TZQuery); begin if qry <> nil then begin qry.Close; ConnectionPools.Push(TZConnection(qry.Connection)); qry.Free; end; end; function GetQueryJsonData(sql: string): string; var qry: TZQuery; uid: integer; susername, spw: string; str:String; begin qry := QryPools.Require; try qry.SQL.Text := sql; qry.Open; result := _DataSetToJson(qry); finally QryPools.Return(qry); end; end; initialization ConnectionPools := TSQLConnectionPool.Create(); QryPools := TQryPool.Create; finalization QryPools.Free; ConnectionPools.Free; end.
到此,相信大家對“如何使用Zeos數據庫連接池”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。