有没有实现在.net中的专有连接池的标准方法? [英] Is there a standard way of implementing a proprietary connection pool in .net?

查看:121
本文介绍了有没有实现在.net中的专有连接池的标准方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个标准连接池模式(或API),类似于用在数据提供.NET ,我可以用它来实现自己的连接池?

我问,因为我有一个要求,实现自己的连接池,我们在Web服务中使用专有的TCP / IP设备。当前的问题是,有大量的连接(读太多)到装置的因IIS下运行的web服务的螺纹性质。我想用我自己的连接池,以限制这些连接的数量,似乎愚蠢推倒重来,如果有一个标准的模式,我可以用它来做到这一点。

解决方案
  

有没有一个标准的连接池模型

除ADO.NET,没有。但是,ADO.NET模型是很好的一个简单的。构造一个对象来从池中的连接,或重新创建,它是返回到关闭/处置/最后确定的游泳池。

从这个可以立即确定实现模式:

  • 在客户端类型是真正的类型的代理,并拥有从创建一生的时间去 关闭/&hellip ;.它是对真实对象的代理。提供方法和属性这 着有真正的联系。
  • 在真正的连接是一个长期居住的实例,在游泳池下创建一个代理给出了 然后返回在代理的末端。

目前在执行的选择。当一个对象已经交了,请问池也需要保持一个参考?如果它池需要跟踪哪些对象是活动的,哪些是汇集;否则可以使用可用对象的简单集合

是这样的:

 内部类MyObjectImpl {
  //保存资源的实物
}

内部静态类MyObjectPool {
  私有静态对象SyncRoot上=新的对象();
  私有静态队列< MyObjectImpl>池=新问答LT;为MyObject>();
  私有静态诠释totalObjects = 0;
  私人只读INT maxObjects = 10;

  内部MyObjectImplGet(){
    锁定(SyncRoot上){
      如果(pool.Count大于0){
        返回pool.Dequeue();
      }
      如果(totalOjects> = maxObjects){
        抛出新PoolException(无可用对象);
      }
      变种O =新MyObjectImpl();
      totalObjects ++;
      返回O;
    }
  }

  内部空隙返程(MyObjectImpl OBJ){
    锁定(SyncRoot上){
      pool.Enqueue(OBJ);
    }
  }
}

公共类为MyObject:IDisposable的{
  私人MyObjectImpl实现了一套;

  公众为MyObject(){
    实现了一套= MyObjectPool.Get();
  }

  公共无效关闭(){
    的Dispose();
  }

  公共无效的Dispose(){
    MyIObjectPool.Return(implement执行);
    // prevent继续使用,作为实施对象实例
    //现在可以给予的。
    实现了一套= NULL;
  }

  //转发API来小鬼

}
 

这不切合为MyObject 的实例被破坏。例如。坚持弱引用的集合分配为MyObject 的,如果池是处置情况空白支票。这也将是如果你不能依赖客户端的关闭或处置的实例,或落实 MyObjectImpl一个finaliser需要 1 (及报告为在调试错误生成)。

1 上的MyObject因为myObject的定稿MyObjectImpl实例可能已经被敲定的时候这不能做。

Is there a standard connection pooling model (or API) similar to that used by the data providers in .net that I could use to implement my own connection pool?

I ask because I have a requirement to implement my own connection pool to a proprietary TCP/IP device that we use in a web service. The current problem is that there are lot of connections (read too many) to the device due to the threaded nature of web services running under IIS. I want to limit the number of these connections using my own connection pool and it seems stupid to reinvent the wheel if there is a standard model I could use to do this.

解决方案

Is there a standard connection pooling model

Other than ADO.NET, no. But the ADO.NET model is nice an simple. Construct an object to get a connection from the pool, or created anew, and it is returned to the pool on Close/Dispose/Finalise.

From this one can immediately determine an implementation pattern:

  • The client type is a proxy to the real type, and has a lifetime from creation to Close/…. It is a proxy to the real object. Provides methods and properties which forward to the real connection.
  • The real connection is a long lived instance, created by the pool, given out under a proxy and then returned at the end of the proxy.

There is a choice in the implementation. When an object has been handed out, does the pool need to also keep a reference? If it does the pool needs to track which objects are active and which are pooled; otherwise a simple collection of available objects can be used.

Something like:

internal class MyObjectImpl {
  // The real object that holds the resource
}

internal static class MyObjectPool {
  private static object syncRoot = new object();
  private static Queue<MyObjectImpl> pool = new Queue<MyObject>();
  private static int totalObjects = 0;
  private readonly int maxObjects = 10;

  internal MyObjectImplGet() {
    lock (syncRoot) {
      if (pool.Count > 0) {
        return pool.Dequeue();
      }
      if (totalOjects >= maxObjects) {
        throw new PoolException("No objects available");
      }
      var o = new MyObjectImpl();
      totalObjects++;
      return o;
    }
  }

  internal void Return(MyObjectImpl obj) {
    lock (syncRoot) {
      pool.Enqueue(obj);
    }
  }
}

public class MyObject : IDisposable {
  private MyObjectImpl impl;

  public MyObject() {
    impl = MyObjectPool.Get();
  }

  public void Close() {
    Dispose();
  }

  public void Dispose() {
    MyIObjectPool.Return(impl);
    // Prevent continuing use, as the implementation object instance
    // could now be given out.
    impl = null;
  }

  // Forward API to imp

}

This doesn't cater for instances of MyObject being destroyed. E.g. hold a collection of weak references to allocated MyObject's, and if the pool is empty check for disposed instances. This would also be needed if you cannot rely on client's to Close or dispose of instances, or implement a finaliser on MyObjectImpl1 (and report this as an error in debug builds).

1 This cannot be done on MyObject because by the time MyObject was finalised the MyObjectImpl instance could already have been finalised.

这篇关于有没有实现在.net中的专有连接池的标准方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆