Few Things About Pooled Connections

Pooled Connections are not a new concept of SQL Client. It reuses pool of opened connections for any new T-SQL requests. It provides a logically fresh new connection to application as you physically open a new connection. The fact behind it is that when you close a pooled connection, the connection is merely returned back to the pool without physically closing the tunnel(SQL Server doesn’t know the connection is closed by the client), and, when you open a new connection, SQL Client will open a new connection if there is no available connections in the pool. Otherwise SQL Client will get the connection from the pool. In this case, SQL Server still does not know that the connection is assigned for new purposes. Whenever you run the first command through a connection which assigned from the pool, SQL Client will “inject” execution of sp_reset_connection before the execution of your SQL statement or procedure to reset resources and mimik a brand new connection. A lot of discussions regarding sp_reset_connection in the internet. I will excerpt them at the end of this blog post. Apart from that, you must know few other things about it.

  1. Duration: As explain above, the duration from connnection close to the first command issued after connection reacquired, SQL Server is aware of nothing of the connection, which means if you have any open transaction before connection closed, that transaction will be completed only when the connection get acquired and the first command of re-acquire connection run. The duration of the transaction might be extremely long when you have a huge connection pool. So, programming over pooled connection should be very defensive. Unclosed transactoin will not be carried over to the new commnad but it will last longer and potentially cause blocking issue. The transaction log will not be shrunk to expected size due to active long run transactions.
  2. Disconnection: Connections in the pool might be reset by any reason (network, DBA kill the session, etc). In those cases, SQL Server does close the connection, but SQL Client does not know anything about it. When a physically closed connection get re-assigned. You will not get error when you open the connection, this is different from opening a non-pooled connection. The error will arise when the first command is executed.
  3. Isolation Level: This is very important. The Isolation level will not be reset by sp_reset_connection procedure. If you accidentially set more restrictive isolation level before closing a connection without setting them back, that isolation level will be carried over for other purpose. For instance, if you use TransactionScope class to control the transaction at C# code level, it automatically escalates Isolation Level toΒ  Read Serializable by default. As a result, you may see a lot of deadlocks in your system. From deadlock graph, you will see the Serializable Isolation Level in place. Back to the code, you can’t find any SQL Statement sets the Isolation Level.
  4. Blocking: sp_reset_connection might be blocked or blocking! If you have a pooled connection that creates a global temp table, if this global temp table is locked by other sessions for very long time, when the Global Temp Table creator connection closed and get acquired by other code, when the first command run, sp_reset_connection will try to release the global temp table ( which is currently locked by other sessions). Procedure sp_reset_connection will WAIT for Sch-M lock on the Global temp table until all the blockers leave. In this case, the Sch-M wait might lead command timeout. Any commands accessing that Global Temp Table while Sch-M lock is waiting will be blocked.

At the end of this blog post, I would like to excerpt text form here for future reference.

sp_reset_connection resets the following aspects of a connection:

  1. It resets all error states and numbers (like @@error)
  2. It stops all EC’s (execution contexts) that are child threads of a parent EC executing a parallel query
  3. It will wait for any outstanding I/O operations that is outstanding
  4. It will free any held buffers on the server by the connection
  5. It will unlock any buffer resources that are used by the connection
  6. It will release all memory allocated owned by the connection
  7. It will clear any work or temporary tables that are created by the connection
  8. It will kill all global cursors owned by the connection
  9. It will close any open SQL-XML handles that are open
  10. It will delete any open SQL-XML related work tables
  11. It will close all system tables
  12. It will close all user tables
  13. It will drop all temporary objects
  14. It will abort open transactions
  15. It will defect from a distributed transaction when enlisted
  16. It will decrement the reference count for users in current database; which release shared database lock
  17. It will free acquired locks
  18. It will releases any handles that may have been acquired
  19. It will reset all SET options to the default values
  20. It will reset the @@rowcount value
  21. It will reset the @@identity value
  22. It will reset any session level trace options using dbcc traceon()

sp_reset_connection will NOT reset:

  1. Security context, which is why connection pooling matches connections based on the exact connection string
  2. If you entered an application role using sp_setapprole, since application roles can not be reverted

7 thoughts on “Few Things About Pooled Connections”

  1. Hi John,

    Thanks for this article, that’s really great info.
    Can you explain what you mean by closing a table ?

    Regards, Nicolas

    Reply
  2. i’m starting use of SQL 2003…how to manage the database….
    Then I encounter in SQL Server Profiler as my reference what is going on the database…then
    I encounter the Textdata the “Network Protocol :LPC set quoted identifier on set arithabort off set numeric_roundabort off set ansi_warning on set a”…what does it means…..and also on TEXTDATA under SQL Server PROFILER i encounter also sp_reset_connection ..can u please help me.tnx

    Reply

Leave a Comment

C# | HTML | Plain Text | SQL | XHTML | XML | XSLT |

This site uses Akismet to reduce spam. Learn how your comment data is processed.