如何使用Zeos数据库连接池
发表于:2024-10-01 作者:千家信息网编辑
千家信息网最后更新 2024年10月01日,本篇内容主要讲解"如何使用Zeos数据库连接池",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用Zeos数据库连接池"吧!unit ZeosConPo
千家信息网最后更新 2024年10月01日如何使用Zeos数据库连接池
本篇内容主要讲解"如何使用Zeos数据库连接池",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用Zeos数据库连接池"吧!
unit ZeosConPool;interfaceuses 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;implementationfunction _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 Connectionprocedure 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;initializationConnectionPools := TSQLConnectionPool.Create();QryPools := TQryPool.Create;finalization QryPools.Free; ConnectionPools.Free; end.
到此,相信大家对"如何使用Zeos数据库连接池"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
对象
数据
数据库
内容
实际
日志
学习
实用
更深
兴趣
参数
字段
实用性
容器
密码
操作简单
方法
更多
朋友
用户
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网金融新晨科技有限公司
数据库的扩展名是什么
广泛宣传增强网络安全意识
php网站数据库文件
软件开发最速成的
协同软件开发怎么选择
g3笔记本网络安全系统
sr590 服务器管理口ip
潜渊症怎么保存服务器
数据库怎么设置交叉表
nba2k韩国服务器
富士康招聘软件开发吗
大学生网络安全教育建议
食堂支付宝点餐机软件开发
2kol是什么游戏服务器
国家信息业务编码维护数据库
太原火箭网络技术服务公司
adx新型网络安全
计算机国三网络技术必考题
美团招收网络安全人才
数据库管理系统崩溃和停电
青丘狐传说为什么连不上服务器
职业高中有软件开发吗
bs架构应用软件开发管理
诛仙新服务器
软件开发项目提成激励制度
软件开发权限
编辑数据库个人总结
网络技术的全面运用
流放 苦痛之捷 数据库