Tuesday, June 17, 2014

Managing Zombie (Broken) Connections in Connection Pooling (Microsoft.NET)

What is it?

 

Recently we had came across an issue with a WCF application. After a period of normal operation, any SQL operations through SQLConnection objects returned weird errors in production environment.The error text varies and below are the typical ones that get displayed.

 

·         NB: Format of the initialization string does not confirm to specification starting at index 0

·         A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

·         Procedure or function has too many arguments specified

·         Input string was not in correct format

·         Internal connection fatal error.

·         Connection property has not been initialized

·         A transport-level error has occurred when sending the request to the server.

·         The connection is closed

 

This is a typical scenario of a Zombie (Broken) SQL connection get reused over and over again from the connection pool. Broken connections can happen in certain scenarios. Say we’ve a valid SQLConnection in ‘Open’ state to the database server. Suppose the ‘Database’ server got restarted or went down, then the ‘SQLConnection’ at our hand becomes a broken connection. Now any ‘SQL Operations’ requested through that connection will end with errors similar to the above listed ones. Even if the database server comes online, we cannot re-use this ‘SQLConnection’ as it is in broken state. It should be discarded forever. Luckily Zombie connections are automatically removed from connection pools by the connection pooling framework.

 

So how does the above issue happens in the first place? This will happen, due to the below ways.

 

Why it is happening?

 

1. Programmer forgot to release (dispose) any SQL related objected after use.

 

This may usually happen during an exception, where programmer forgot to release the object inside the ‘catch’ section of the ‘Try’ block.

If we forgot to release an SQL connection, it is said to be a leaky connection, which will not be properly tracked by the connection pooling mechanism. These connections are get reused over the course of time, without any recycling and if in-turn become a broken connection caused the above failures.

 

2. App-Pool corresponding to your application in IIS, not get recycled properly due to some configuration settings

 

This wont typically happen for web applications as App-Pool get recycled automatically.

But for certain applications like WCF, and under certain configurations (eg. Instance Context Mode configured as ‘Singleton’) , Automatic recycling will not happen and over a period of time your resources get leaked until you manually recycle the pool.

So what could be the solution? The full proof solution consists of two parts

 

What is the resolution?

 

a. Clear and recreate the connection pool, once a broken connection has encountered.

 

While creating a connection, always validate it’s state after the ‘Open’ call. If the state is ‘Broken’ or ‘Not-Open’, Clear the ‘Connection Pools’ and re-create the connection in the new pool.

 

b. Properly release all SQL related objects after use, in every scenarios.

 

We can use ‘Using’ blocks to properly release the objects, no matter whether an exception has occurred or not.

 

Sample Code

 

The sample code is given below.

Note: This example is related SQLConnection object. If you’re using other frameworks like LINQToSQL, EntityFramework e.t.c, Check the state of the ‘Connection’ property of the ‘DataContext’ or somewhat similar object.

 

a. Sample, recreating the connection pool, once a broken connection has encountered.

 

//This is the helper function to get the connection properly from the connection pool.

//It clears and recreates the pool, if necessory (eg. Broken connection)

public SqlConnection GetPooledConnection()

{

SqlConnection con = new SqlConnection(connectionString);

 

if (con.State == ConnectionState.Broken)

{

SqlConnection.ClearAllPools();

con = new SqlConnection(connectionString);

}

 

con.Open();

 

if (con.State != ConnectionState.Open)

{

SqlConnection.ClearAllPools();

con = new SqlConnection(connectionString);

con.Open();

}

 

return con;

}

 

b. Sample. Properly release all SQL related objects after use, in every scenarios.

 

//This is a sample function that uses, the connection and SQL objects.

//This also demonstrates, the proper disposal of SQL objects

public void Usage()

{

try

{

using (SqlConnection con = GetPooledConnection())

{

using (SqlTransaction tran = con.BeginTransaction())

{

try

{

using (SqlCommand cmd = new SqlCommand())

{

cmd.Connection = con;

cmd.Transaction = tran;

 

 

using (SqlDataReader rdr = cmd.ExecuteReader())

{

//Read your data here

}

 

//Do your data operations using cmd object

}

//Commit updates on graceful exit

tran.Commit();

}

catch

{

//Rollback on errors

tran.Rollback();

//Log your errors

throw;

}

}

}

}

catch

{

//Log your errors

throw;

}

}

 

Wednesday, June 11, 2014

‘New Mail’ Notifications for Secondary ‘Additional Outlook Mailbox Folders’ – Custom Application

Most of the IT-Support teams have a dedicated ‘Secondary Mailbox Folder’ configured in their MS-Outlook, to track all the mails related to their divisions/applications/operations. All of the members of the support team have the ‘Mailbox Folder’ configured in their respective desktop.

If you don’t know, what’s a secondary mailbox is, the below figure will help! These are secondary mail box folders added to your primary email account. This may be a mail box shared among a group of people.

 

image

 

But one problem with the ‘Secondary Mailbox’ with MS-Outlook is that, ‘New Mail’ notification will not be triggered for it. This means, if any new mail arrives in your ‘Secondary Mail Box’, The traditional ‘Outlook New Mail’ notification will not pop up. So support team have a hard time to regularly check their ‘Secondary Mailboxes’ for any new mails. Failure to do so, may result in an SLA miss.

 

Here it is, where Microsoft says about this:

Outlook will not support native ‘New E-Mail’ notifications, for ‘Additional Secondary mail box folders’ added to your primary outlook account, unless you are the owner of your mailbox.

You can find more Details here:

www.msoutlook.info

MSDN

 

Yes there are dirty workarounds. For e.g. Setting up a custom outlook rule to auto forward all mails arrived at the ‘Secondary Mailbox’ to your Inbox. As Inbox mails will give you ‘New Mail’ notifications this will work, but with setting up the custom rule hack.

 

We were searching for a more straightforward way to handle this shortcoming.

 

The result is a ‘.NET windows application’, that will trigger ‘New Mail’ notifications for any configured under ‘Outlook Mailbox’ folders. No matter whether it’s a regular Inbox or an additional secondary mailbox, the application will pop-up ‘new mail’ notifications, if you prefer.

 

The tool targeted towards various IT Support teams (Maintenance), who have a designated shared ‘Outlook Mailbox Folder’ for their division/Applications and have to respond to those group mails in a timely manner.

 

1. Download

Download Application (EXE) with C# Source

Prerequisites:

• .NET Framework 2.0 Installed

• Microsoft Office Outlook installed and started 

 

image

 

 

2. Using the custom tool

 

Note:  This tool is a generic one, so you can enlist any of your mail boxes (Primary and any number of additional secondary mail boxes) for new mail notifications.

 

2.1 Download (Source and EXE) from the above link and Unzip it

 

2.2 Double click OutlookMailboxFolderNotify.exe

 

2.3 Select the mail boxes from the list, for which you would like to receive ‘New Mail’ Notifications

 

      Note: Here you  can select your Additional ‘Secondary Mailboxes’, along with Primary Mailbox if needed.

 

2.4 Click button ‘Start Notifications on New Mails’

 

 

image

 

2.5 Minimize the form to the system Try (using ‘Minimize To System Tray’ button)

 

Note: From now onwards you will get ‘New Mail’ notifications for the selected mailboxes.

 

 

image 

2.6 To stop receiving notifications/Exiting the application.

                       

                        • Double click on the Notify icon in the ‘System Tray’

• Click the button ‘Stop Notifications’ – To stop notifications.

• Click the button ‘Exit Application’ – To stop notifications and exit the application.

 

image

 

3. Known Limitations

 

If network connection is lost or ‘Microsoft Exchange Server’ has got disconnected and back online, you have to restart the application once again. (Using the ‘Reset!’ button) 

 

image

Monday, June 9, 2014

Improving Remote Desktop Performance using GPO settings on Remote PC(Applicable for RDP, PCOIP Or VMWare View Client)

To achieve a relatively good Remote Desktop performance,  tweak “Remote Desktop Services” GPO settings as below.

 

1. Open Group Policy Editor on the ‘Remote PC’ [Type ‘gpedit.msc’ at ‘Run’ prompt’]

 

clip_image002

 

2. Navigate to ‘Computer Configuration->Administrative Templates->Windows Components->Remote Desktop Services’

 

clip_image004

 

3. Now tweak the settings as given below [Sub GPO setting has been underlined]

 

Remote Desktop Session Host/Connections:

 

a.       Limit Number Of Connections = 1

 

Remote Desktop Session Host/Remote Session Environment:

 

a.       Limit maximum color depth = 15bit

b.      Enforce Removal of Desktop wallpaper = true

c.       Configure RemoteFX = disabled

d.      Limit maximum number of monitors = 1

e.      Optimize Visual Experience when using RemoteFx = (Screen Capture Rate: Lowest, Image Quality: Lowest)

f.        Set Compression Algorithm for RDP data = … use less network bandwidth

g.       Optimize Visual Experience for Remote Desktop Service Sessions = (Visual Experience = Text)

h.      Enable Remote Desktop 8.0 Protocol = Enabled

i.         Configure Image Quality For RemoteFx Adaptive Graphics = Medium

j.        Configure RemoteFx Adaptive Graphics = …minimum bandwidth usage 

Classic ASP Debugging with Visual Studio

Recently we’ve found an interesting discovery and hope this will be very helpful to you all, who are struggling with “Classic ASP” debugging through “Response.Write()” methods.

 

The good news is, you can debug your “Classic ASP” applications through Visual Studio (2003,2005 and all the way up to 2012) and support breakpoints.

 

Steps are given below: (We’ve used Windows7 and IIS7)

 

1.       Ensure Prerequisites

 

1.1   IIS7 has to be installed [Control Panel->Programs and Features->Turn Windows Features On/Off] 

clip_image002

 

1.2   Machine Debug Manger Service should be automatic and started [start->run->services.msc] 

clip_image004

 

2.       Host your “Classic ASP” website in IIS [start->run->inetmgr]

 

2.1   Create a application pool in “Classic Pipeline Mode” [Say GF]

 

2.2   In advanced settings set “Identity” as “Local System” 

1

image

 

2.3   Create your “Classic ASP” website [Virtual directory will be your asp application folder] in IIS and select the Above Pool [in this ex: GF]

image

2.4   Enable server script debugging and error to browser option 

image

 

2.5   Enable windows authentication [as per your need. You can set whatever authentication scheme you want] 

clip_image014 

2.6   Ensure *.asp is mapped to ISAPI filter and enabled. 

clip_image016

 

2.7   Now restart your website and browse to your start page (say default.asp) – This will kick-in the worker process 

 

3.        Debug with VS2010 [You can use 2003/2005/2012 e.t.c]

 

You can use 2 approaches, to debug the asp application. The most simple way is, put ‘Stop’ keyword in your asp source code, at place you want to start debugging. Then simply browsing your application from IIS, will hit your ‘Stop’ keyword, and you will be prompted with ‘Visual Studio’ debug dialog. Just click on the ‘new instance’ to launch. From there on you can debug using Visual Studio using standard ‘F10/F11’ keys.

 

Or else, you can use the below approach to do the debugging, especially you are using ‘JScript’ as your script language. 

 

3.1   Start VS2010 in administrator mode [Open as administrator menu item]

 

3.2   Attach visual studio to your classis ASP website worker process [Menu->Debug->Attach To Process…]

 

Carefully note the items shown in red rectangles. 

image

 

Note: You can always find your ‘Classic ASP’ worker process ID, by looking at the ‘Worker Process’ feature in IIS as below 

image

 

3.3   Now open the required classic ‘asp’ pages and put the break-points in required locations.

image

 

3.4   Now browse to your required pages and do the necessary actions, So that the break points will be hit

 

3.5   And there you are!!!, You have arrived at your break point. Add watch or quick watch your variables and values. Step in/Step out or just  F10 to debug line by line.

image

 

3.6   Say good  bye to “Response.Write()” and welcome VS2010 debugger for “Classic ASP”

 

Notes: 

a.       You cannot debug your remote servers (like staging/production). You’ve to setup your local IIS. But you can point any network share where your code resides

b.      Sometimes you may need to add yourself to the debug group as below [Start->Run->Gpedit.msc]

image

 

Happy Debugging!