indy多线程创建ado连接,创建多个连接后,比如10个,发现sqlserver中有10个该数据库的连接,然后我清除几个连接,但发现sqlserver还是10个,并没释放,但当我把10个都Connection都释放后,sqlserver的连接却都释放了,是什么问题??
也就是说sqlserver中保留了最大数量创建的连接,直到这些连接都释放后才全部释放?我使用的是1个udl连接sqlserver
indy控件名servertype
    TSimpleClient = record
        id: Cardinal; //系统编号
        IP: string; //IP
        Port: integer; //端口
        LoginTime: string; //登录时间
        UpdateTime: string; //更新时间
        DataBackTime: Integer; //监控时间, 超时则断开
        MYConn: TADOConnection;
        MYQuery: tadoquery;
    end;
    PMyClient = ^TSimpleClient;//有客户端连接
procedure TfrmShowMain.ServerConnect(AThread: TIdPeerThread);
var
    Client: PMyClient;
    list: TList;
    i: integer;
    Ret: DWord;
begin
    try
        CoInitialize(nil); //
        Client := new(PMyClient);
        Client.id := AThread.ThreadID; 
        Client.IP := AThread.Connection.Socket.Binding.PeerIP;
        Client.Port := AThread.Connection.Socket.Binding.PeerPort;
        Client.LoginTime := formatdatetime('yyyy-mm-dd HH:mm:ss', now);
        Client.UpdateTime := formatdatetime('yyyy-mm-dd HH:mm:ss', now);
        AThread.Data := Pointer(client);         Client.MYConn := TAdoconnection.create(nil);  //创建的连接
        Client.MYConn.LoginPrompt := False;
        Client.MYConn.KeepConnection := True;
        Client.MYConn.ConnectionString := 'FILE NAME=' + ExtractFileDir(application.ExeName) +
            '\bjyy.udl';
        Client.MYQuery := tadoquery.Create(nil);
        Client.MYQuery.Connection := Client.MYConn;
        Client.MYConn.Connected := true;
        AThread.Connection.WriteLn('Welcome');
    except
        on E: Exception do
        begin
        end;
    end;
end;//在某button clickprocedure TfrmShowMain.BitBtn3Click(Sender: TObject);
var
    Client: PMyClient;
    list: TList;
    i: integer;
begin
    if not server.Active then
        exit;
    List := Server.Threads.LockList;
    try
        for i := 0 to List.Count - 1 do
        begin
            try
                Client := Pointer(TIdPeerThread(List.Items[i]).Data);
                if Client = nil then
                    continue;
                if inttostr(Client.id) = listview1.Selected.Caption then
                begin   //发现我要断开的ID
                    Client.MYQuery.Close;
                    Client.MYQuery.Free;                    Client.MYConn.Connected := false;
                    freeandnil(Client.MYConn);                    TIdPeerThread(List.Items[i]).Connection.Disconnect;
                    TIdPeerThread(List.Items[i]).Data := nil;
                    FreeMem(Client);
                    CoUnInitialize();
                    exit;
                end;
            except
                on E: Exception do
                begin
                end;
            end;
        end;
    finally
        Server.Threads.UnlockList;
    end;
end;请高人 指点

解决方案 »

  1.   

    freeandnil(Client.MYConn);
    改成Client.MYConn.free;试一试
      

  2.   

    为什么不让多个线程去连接一个创建过的TAdoconnection呢,请高手分析一下这两种方法的区别!
      

  3.   

    没看出来在线程里做创建ADOConnection之后要做什么
      

  4.   

    创建connection后,在indy的excute中client.MYConn.BeginTrans;
                        client.MYQuery.Close;
                        client.MYQuery.SQL.Text := 'select * from BJYY_D_UpData where 
                        client.MYQuery.Open;
                        client.MYQuery.Append;
                        client.MYQuery.FieldByName('ID').AsString := 
    ....                    
    ....
                        client.MYQuery.Post;
                        client.MYConn.CommitTrans;------------------------------------
     ccdarkness(亲亲我的宝贝) ( ) 信誉:100  2007-7-30 23:33:55  得分: 0  
    freeandnil(Client.MYConn);
    改成Client.MYConn.free;试一试  ------------
    以前就是这样写的,也不行.-----------------------------------
      aniugee(阿牛) ( ) 信誉:100  2007-07-31 08:38:06  得分: 0  
     
     
       为什么不让多个线程去连接一个创建过的TAdoconnection呢,请高手分析一下这两种方法的区别!
      -------------------------
    这样多个连接去连接1个connection可能会有问题吧,具体什么问题高人解答吧:0
     
      

  5.   

    你根本就不用创建TAdoconnection
    Client.MYQuery通过TAdoconnection连接到数据库,可以直接写
    Client.MYQuery.ConnectionString := 'FILE NAME=' + ExtractFileDir(application.ExeName) +
    '\bjyy.udl';
      

  6.   

    ccdarkness(亲亲我的宝贝) ( ) 信誉:100 我试了,这样如果在excute时,SQLSERVER就创建了连接,但当我连接关闭后,sql的进程中还存在刚才的连接,只有程序退出了,这些连接才断开
      

  7.   

    刚测错了.不过不创建connection好象和创建了一样.就是我关闭某个连接后,sql的进程中并没有减少,一直保持着最大的连接,直到全部连接都断开后,sql进程才没有了.是不是我程序写的有问题
      

  8.   

    TIdPeerThread(List.Items[i]).Connection.Disconnect;
                        TIdPeerThread(List.Items[i]).Data := nil;这个就是结束的哦