感谢竹子!

整体思路,是不用kbmMWUNIDACQuery,而是直接用uniQuery做数据查询,利用kbmMWUNIDACConnectioPool取得数据库联接,自己再建一个uniQuery对象池,从中取uniQuery.
下面是一个具体查询数据库的方法:
function TSchoolSrv.PerformGetStuAttendanceState(
  ClientIdent: TkbmMWClientIdentity;
  const Args: array of Variant): Variant;
var
  aUniQuery: TUniQuery;
  aconn: TkbmMWUNIDACConnection;
  json: string;
  code: Integer;
  msg: string;
  student_name,class_name: string;
  memTable: TkbmMemTable;
  aStreamFormat: TkbmMWBinaryStreamFormat;
begin
  code := -1;
  memTable := nil;
  aUniQuery := nil;
  aStreamFormat := nil;  
  aconn := FDM.getkbmMWUNIDACConnection;
  if aconn = nil then  kbmMWRaiseException(101,'无可用的数据库连接.');

try
      aUniQuery := UniQueryPool.Lock;
      if aUniQuery = nil then kbmMWRaiseServerException('无可用的数据集对象.');

aUniQuery.Connection := aconn.Database;
      aUniQuery.SQL.Text := 'select attendance_state_code,attendance_state from attendance.stu_attendance_state';
      aUniQuery.sql.Text := fdm.getFinalSql(aUniQuery.sql.Text);//多库合一库时重组SQL语句
      aUniQuery.Open;
      memTable := kbmMemTablePool.Lock;
      if memTable = nil then kbmMWRaiseServerException('无可用的内存表对象.');
      memTable.LoadFromDataSet(aUniQuery,[mtcpoStructure,mtcpoProperties]);
      aStreamFormat := kbmMWBinaryStreamFormatPool.Lock;
      if aStreamFormat = nil then  kbmMWRaiseServerException('无可用的StreamFormat对象.');
      memTable.SaveToStreamViaFormat(ResultStream,aStreamFormat);
    except
      on e: Exception do
      begin
         kbmMWRaiseException(999,e.Message);
      end;  
    end;

finally
    if aUniQuery <> nil then UniQueryPool.UnLock(aUniQuery);
    if memTable <> nil then kbmMemTablePool.UnLock(memTable);
    if aStreamFormat <> nil then kbmMWBinaryStreamFormatPool.UnLock(aStreamFormat);
    aconn.UnlockConnection;
  end;
end;

下面这个方法,从kbmMWUNIDACConnectionPool取得一个可用的数据库联接.function TFDM.getkbmMWUNIDACConnection: TkbmMWUNIDACConnection;

begin
  Result := TkbmMWUNIDACConnection(kbmMWUNIDACConnectionPool1.GetBestConnection(True,0,nil,3000));
end;

最后,再看看uniQuery池管理对象的实现,整个单元,不多解释.

unit UUniQueryPool;

interface

uses
  Classes, Windows, SysUtils, Uni, forms;

type
  TUniQueryPool = class(TObject)
  private
    FObjList:TThreadList;
    FTimeout: Integer; //单位:毫秒
    FMaxCount: Integer;
    FSemaphore: Cardinal;
    function CreateNewInstance(List:TList): TUniQuery;
    function GetLock(List:TList;Index: Integer): Boolean;
    function getUsedCount: Integer;
  public
    property Timeout:Integer read FTimeout write FTimeout;
    property MaxCount:Integer read FMaxCount;
    property usedCount: integer read getUsedCount;

constructor Create(ACapicity:Integer);overload;
    destructor Destroy;override;
    function Lock: TUniQuery;
    procedure UnLock(var Value: TUniQuery);
  end;

var
  UniQueryPool: TUniQueryPool;

implementation

constructor TUniQueryPool.Create(ACapicity:Integer);
begin
  FObjList:=TThreadList.Create;
  FTimeout := 3000;
  FMaxCount := ACapicity;
  FSemaphore := CreateSemaphore(nil, FMaxCount, FMaxCount, nil);   
end;

function TUniQueryPool.CreateNewInstance(List:TList): TUniQuery;
var
  p: TUniQuery;
begin
  try
    p := TUniQuery.Create(nil);
    p.Tag := 1;
    List.Add(p);
    Result := p;
  except
    Result := nil;
    Exit;
  end;
end;

destructor TUniQueryPool.Destroy;
var
  i: Integer;
  List:TList;
begin
  List:=FObjList.LockList;
  try
    for i := List.Count - 1 downto 0 do
    begin
      TUniQuery(List[i]).Free;
    end;
  finally
    FObjList.UnlockList;
  end;
  FObjList.Free;
  FObjList := nil;
  CloseHandle(FSemaphore);
  inherited Destroy;
end;

function TUniQueryPool.GetLock(List:TList;Index: Integer): Boolean;
begin
  try
    Result := TUniQuery(List[Index]).Tag = 0;
    if Result then
      TUniQuery(List[Index]).Tag := 1;
  except
    Result :=False;
    Exit;
  end;
end;

function TUniQueryPool.getUsedCount: Integer;
var
  i: Integer;
  List:TList;
begin
  try
    Result := 0;
    list := FObjList.LockList;
    Result := list.Count;
  finally
    FObjList.UnlockList;
  end;  
end;

function TUniQueryPool.Lock: TUniQuery;
var
  i: Integer;
  List:TList;
begin
  try
    Result := nil;
    if WaitForSingleObject(FSemaphore, Timeout) = WAIT_FAILED then Exit;
    List:=FObjList.LockList;
    try
      for i := 0 to List.Count - 1 do
      begin
        if GetLock(List,i) then
        begin
          Result := TUniQuery(List[i]);
          //PostMessage(Application.MainForm.Handle, 8888,23,0);
          Exit;
        end;
      end;
      if List.Count < MaxCount then
      begin
        Result := CreateNewInstance(List);
        //PostMessage(Application.MainForm.Handle, 8888,21,0);
      end;
    finally
      FObjList.UnlockList;
    end;
  except
    Result :=nil;
    Exit;
  end;
end;

procedure TUniQueryPool.Unlock(var Value: TUniQuery);
var
  List:TList;
begin
  try
    List:=FObjList.LockList;
    try
      TUniQuery(List[List.IndexOf(Value)]).Tag :=0;
      ReleaseSemaphore(FSemaphore, 1, nil);
    finally
      FObjList.UnlockList;
    end;
    //PostMessage(Application.MainForm.Handle, 8888, 22, 0);
  except
    Exit;
  end;
end;

initialization
  UniQueryPool := TUniQueryPool.Create(100);
finalization
  FreeAndNil(UniQueryPool);

end.

最新文章

  1. hadoop 2.6配置记录
  2. Lambda演算 - 简述Y组合子的作用
  3. Mysql的一些常用方法
  4. Java for LeetCode 044 Wildcard Matching
  5. C#计算时间差值
  6. JDBC概述以及几种驱动程序
  7. NGUI 3.5教程(六)Font字体
  8. FineUI登入的例子中遇到的一些问题
  9. InnoDB引擎Myslq数据库数据恢复
  10. block,inline,inline-block的区别
  11. Mysql数据库报错:Cannot add or update a child row: a foreign key constraint fails(添加多对多关系)
  12. 面试题 -AR VR MR以及CR的简单介绍
  13. 【UWP】手动实现 WebAuthenticationBroker
  14. Latex常用数学符号(转)
  15. [转CSDN多篇文章]WEB 3D SVG CAD 矢量 几种实现方案
  16. UMI标签学习【转载】
  17. wordpress模板里加keywords和description
  18. MySQL之练习题5
  19. Qt多线程同步总结
  20. Android中的 style 和 theme

热门文章

  1. OC内存管理 @property的增强
  2. 一点简单的关于ASP.NET下载
  3. hibernate中增加annotation @后不提示信息【转】
  4. 基于密度的聚类之Dbscan算法
  5. uva 11181
  6. String 内在分配解析
  7. Java中super的用法并与this的区别(转载)
  8. What is the difference between Views and Materialized Views in Oracle?
  9. POJ 1459 Power Network(网络最大流,dinic算法模板题)
  10. 【转载】Dom篇