Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Managing the Connection Lifetime


Managing the Connection Lifetime


Many developers need to look at the documentation when building a SQL Server connection string for a real-world application. Among the various attributes, one has always captured my attention until I correctly figured out its whys and wherefores. The attribute is “Connection Lifetime.”

The “Connection Lifetime” attribute indicates in seconds the time a connection object is considered valid. Elapse the time, the connection object is disposed of. Fine, but why on earth should you be getting rid of a perfectly working connection object? As it turns out, the attribute is expressly designed to address a well-known situation, and should never be used otherwise.

Imagine you have a cluster of servers sharing the workload. At some point, you realize the load is too high and turn on an additional server. What do you think will happen? You expect that the newly added server picks up most of the newly allocated work to mitigate impact on others. There might situations in which this just doesn’t happen and the new server remains idle.

This nasty effect occurs when middle-tier components cache the connection objects for performance reasons and never open new ones. By disposing of working connections, you force middle-tier components to create new connections. Needless to say, new connections will be assigned to the least loaded server. In the end, set “Connection Lifetime” only if you’re in a cluster scenario.

Partially related to this issue is the problem of clearing the pool of connection objects that ADO.NET creates when connection pooling is enabled. Until ADO.NET 2.0, there was no way to programmatically clear the pool of open connections. This is not an everyday operation, but it becomes essential when the database server goes down and is then restarted. As a result, all connection objects in the pool are invalid because they hold a reference to server connections that no longer exist. What happens when a new connection is requested?

The pooler returns an apparently valid connection object to the caller and the caller runs the command. Unfortunately, the connection object is not recognized by the database server, resulting in an exception. The connection object is removed from the pool and replaced. To make it short, shutting down the server without shutting down the application brings the connection pool to an inconsistent state.

This situation is common for applications that deal with server reboots, like a failover cluster. Only one solution is possible—flushing the connection pool. It is not as easy to implement as it may seem at first. Fortunately, in ADO.NET 2.0, the solution comes with the framework. ADO.NET 2.0 is smart enough to recognize when a command exception is due to a possible corruption of the pool. In this case, it walks down the pool and marks each connection as obsolete. When does a command exception indicate pool corruption? It has to be a fatal exception raised from the network layer on a previously opened connection. All other exceptions are ignored and bubble up as usual.

Connection classes define two new static methods for clearing the connection pool. They are ClearPool and ClearAllPools and are defined for both SqlConnection and OracleConnection. These methods can be used to programmatically clear the pool if you know that the server has been stopped and restarted. At the same time, these methods are used internally by ADO.NET 2.0 to clear the pool when a particular command exception is detected.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.