最近研究了下数据库连接池,虽然知道了数据库连接池的作用,但是不是很明白。  难道对于一次数据库连接,只能做一次操作?不能在一个应用程序里面,最开始做一次连接,然后多次操作,最后再撤销连接?  如果是这样的话,数据库连接池的优势体现在哪?

解决方案 »

  1.   

    不能在一个应用程序里面,最开始做一次连接,然后多次操作,最后再撤销连接?可以的,连接上后,你可以做多次操作.
    使用连接池后,你操作完后,连接会被放到池里,而不是把连接销毁.
    你下次再用的时候,会直接从池里拿出空闲的连接使用.而不是去新建一个(new)
    新建连接是代价是很大的.
      

  2.   

    连接池记录的是连接,不是一个语句。比如你本次连接执行了一个事务,事务内多条sql语句。当再次需要该连接时,如果连接池里有该连接,会直接用的。不会再次创建该连接,可以直接执行其中的事务。
      

  3.   

    连接池的主要优势在于连接(connection)是可复用的,
    而非每次操作都需要实际的去建立->查询->断开连接,这样代价比较大.
    程序使用完连接后,把连接丢回连接池暂存,以供其他程序'连接'时使用.
    连接池也是B/S结构系统优于C/S结构系统的主要优势之一.
      

  4.   


    你的WEB应用程序只是一个连接,因此用不用连接池无所谓.如果是C/S结构的应用,有多个用户同时访问数据库(你这个实际上只是WEB服务器在连接数据库),那时连接池才有作用.
      

  5.   

    A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default). Connections are released back into the pool when they are closed or disposed.When a connection is first opened, a connection pool is created based on an exact matching algorithm that associates the pool with the connection string in the connection. Each connection pool is associated with a distinct connection string. When a new connection is opened, if the connection string is not an exact match to an existing pool, a new pool is created. Connections are pooled per process, per application domain, per connection string and when integrated security is used, per Windows identity. Connection strings must also be an exact match; keywords supplied in a different order for the same connection will be pooled separately.所谓的连接池,就是一个与连接对象Connection相关的集合,这不只是简单的集合,而是有一定的机制在内部。我们做开发时,可能建立Connection连接对象,关闭连接对象,有时候还调用Dispose来释放连接。下次再用时,便重新实例化一个连接。但在池中的连接不随连接对象的Close或Dispose而释放。如果下次重新建立连接,连接字符串与前一次完全一模一样,则连接池就会把上次可用的连接对象赋给连接去用。如果两个连接字符串有一点不一样,即使在某一个地方多一个空格,连接池也不会以为是相同的连接,这点微软可能在内部只直接去比较两个字符串了,而不是比较连接数据库字符串的键值互相匹配。连接池的好处就是保留连接对象,防止下次重头再来实例化一个连接对象。