Connection pooling and application domains

clock March 22, 2007 15:50 by author Galcho |

Yesterday I wrote about my Connection pooling tests and I had good comment from Marto Kulov about application domains.

It was easy so I wrote the following code to create new application domain and execute two forms from different assemblies in order to test if they share same connection pool

 

   1:  AppDomain domain = AppDomain.CreateDomain("myNewDomain");
   2:   
   3:  Form frm1 = domain.CreateInstanceFromAndUnwrap("WindowsApplication1.exe", "WindowsApplication1.Form1") as Form;
   4:  Form frm2 = domain.CreateInstanceFromAndUnwrap("WindowsApplication2.exe", "WindowsApplication2.Form1") as Form;
   5:   
   6:  frm1.Show();
   7:  frm2.Show();

And the answer is Yes, They share same connection pool. Both forms executed same for loop and opened 10 connections each to the MS SQL Server. And on the SQL Server side was only one connection alive.

So this is the way to share a connection pool between two processes - put them in one process and in one application domain.

 

 



Connection pooling

clock March 21, 2007 08:05 by author Galcho |

Two days ago I decided to dive into connection pooling secrets. What a coincidence with Sahil Malik as he wrote blog article about his research same day J.

In addtition to his article I can describe what I tested. As I have job to do I will miss screenshotsL.

We were considering option to pass data to SQL SPs usign connection string and we wanted to know how this affects connection pooling. So I created simple winform application with a button with the following.

   1:  string connString = @"data source=.\SQLExpress;
   2:                      user id=gu; password= 123; 
   3:                      Initial catalog = master;
   4:                      Workstation ID=test_host{0};
   5:                      Pooling=true;
   6:                      Connect Timeout = 60";
   7:   
   8:  for (int i = 0; i < 10; i++)
   9:  {
  10:      SqlDataReader dr = null;
  11:      string cmdText = "SELECT date=getdate()";
  12:      SqlConnection cn = new SqlConnection(string.Format(connString, 1));
  13:      SqlCommand cmd = new SqlCommand(cmdText, cn);
  14:      try
  15:      {
  16:          cn.Open();
  17:          dr = cmd.ExecuteReader();
  18:          if (dr != null)
  19:          {
  20:              while (dr.Read())
  21:              {
  22:                  Console.WriteLine(dr["date"].ToString());
  23:              }
  24:          }
  25:      }
  26:      finally
  27:      {
  28:          if (dr != null) dr.Close();
  29:          cn.Close();
  30:      }
  31:  }

I used same SQL statement as Sahil to determine number of physical connection to database.

After executed the following code I got only one connection opened. Note that connection string is always same as I pass 1 as parameter in string.Format(connString, 1) on line 12.

Let’s change this to string.Format(connString, i). As expected I ended with 10 opened connections as connection string is different for every SqlConnection object.

Let’s undo change and use connection pooling again. We have for loop with 10 connection objects opened but only one physical connection is opened on SQL side.

Let’s start two instances of our test application and check what happens by clicking on buttons. We executed for loop once in each instance. By executing Sahil’s SQL statement we see two connections  although all 20 connection has same connection string.

I opened Activity Monitor from SQL Management Studio and I was able to see the connections – everything was same except Net Address. According the documentation:

Net Address

Assigned unique identifier for the network interface card on each user's workstation. When the user logs in, this identifier is inserted in the Network Address column.

Strang… both instances are on same machine…

So as conclusion the statement from Wikipedia’s Connection Pooling page – “A Connection Pool is a cache of database connections maintained in the database's memory so that the connections can be reused when the database receives future requests for data.

Is wrong… the pool is only on client side

As I missed the screenshots I will add another example in case I lost you somewhere above J

Connection pooling is pool of connection within instance of application! If you have two instances of same application that creates one connection to SQL  Server you will have 2 conenction to the server.

If these 2 instances created 20 connections each (with same connection string within the instance) there will be 2 connections on the server because connection pooling.

If these 2 instances created 20 connections each (different connection string each) there will be 2x20=40 connections on the server because connection pooling is done only on same connection string. 



Count to 31 using one hand.

clock March 13, 2007 14:40 by author Galcho |

Probably this is not big deal but it is interesting so I decided to share with you.

All of us are supposed to know how to count to 5 using one hand. But can we count above  5? How?!

Think about this for a while :) 

(if you want to give up and just to see solution check here)



LINQ Article - Lambda expressions section done

clock March 10, 2007 18:04 by author Galcho |

I was able to complete the Lambda expressions section in the LINQ Tutorial.

Last week I felt a bit tired and I got half of this week off. And as typical tech geek I used some of the time to play with VS Orcas March CTP and to grow LINQ Tutorial a bit.

It is nice to get some days off :)



LINQ Tutorial - two more features described

clock March 9, 2007 13:31 by author Galcho |

I've just published the update of LINQ tutorial I started in Bulgarian. I cover two new features in C# - Auto-implement properties (I bloged about this too) and Extension methods.

I hope bulgarian readers will find them interesitng.



C# 3.0 Partial Method Definitions

clock March 8, 2007 17:39 by author Galcho |

While I dug into Orcas March CTP Documentation I found another new C# feature (at least for me). It is called Partial Method Definitions and allows you to separate method definition and method implementation in partial classes.

//Definition

partial void partial void onNameChanged();

 

//Implementation

partial void onNameChanged()

{

  //method body

}

 

As I have C++ background this sounds familiar to me and reminded me times when I had to declare methods in .h (header) files and put implementation in .c/.cpp files.

To be honest I am not able to find the advantages of this new feature but I suppose there will be more blog post soon that will discuss this.

As I emphasize above you can separate method definition from implementation only in partial classes. There are number of restrictions stated in Orcas documentation:

  • Partial method declarations must begin with the contextual keyword partial and the method must return void.
  • Partial methods can have ref but not out parameters.
  • Partial methods are implicitly private, and therefore they cannot be virtual.
  • Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.
  • Partial methods can have static and unsafe modifiers.
  • Partial methods can be generic. Constraints are placed on the defining partial method declaration, and may optionally be repeated on the implementing one. Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.
  • You can not make a delegate to a partial method.

 



JScript IntelliSense in Visual Studio "Orcas"

clock March 5, 2007 15:38 by author Galcho |

JavaScript IntelliSense in Visual Studio "Orcas" March CTP - WOW That's awesome!!!

The guys from VS Web tools team did a great job and employ xml comments to make JS Intellisense possible.

Take a look at the screenshots below

ASP.NET AJAX Concepts in Completion List

IntelliSense from Script Libraries for ASPX Pages

There are much more...

Did I get your interest? See the full post at Web Development Tools Team blog.



C# 3.0 Auto-Implemented Properties

clock March 5, 2007 11:13 by author Galcho |

I have downloaded VS Orcas March CTP and I am playing with its features now.

I found one very cool feature called Auto-Implemented Properties. This will allow you to create private field and public property accessor with single C# 3.0 line like this:

public string FullName { get; set; }

instead like in c# 2.0:

private string fullName;

 

public string FullName

{

    get { return fullName; }

    set { fullName = value; }

}

And as with the others cool language sugars the compiler will do the rest :). Actually it doesn't look that good but still it works. For the single line property above I got the following code in Reflector:

public string FullName

{

    [CompilerGenerated]

    get

    {

        return this.<>k__AutomaticallyGeneratedPropertyField0;

    }

    [CompilerGenerated]

    set

    {

        this.<>k__AutomaticallyGeneratedPropertyField0 = value;

    }

}

Nice huh?

Of course this won't work if you have complex getters and setters but still will speed up code writing and leave more time for thinking :)



LINQ to ASCII..... art :)

clock March 2, 2007 10:24 by author Galcho |

Most of have seen such images and probably tools to create ascii pictures

 

There is good explanation on how to convert image to ascii on Wes Dyer's blog. And even cooler - this is done with new c# 3.0 language features.

Go and read yet another image2acsii converter from Yet Another Language Geek's blog :)



Prevent multiple clicks on Submit button in ASP.NET

clock March 1, 2007 11:05 by author Galcho |

Recently I had to solve very common problem: When users what to submit the page by clicking on Submit button very often they become inpatient and click the button again which results in another button OnClick event firing (and if you save db record) duplicating records in database.

The most obvious solution is to use JavaScript to disable submit button. Unfortunately this easy solution is not the best because of number of reasons - see Andy Smit's blog entry you don't want to disable buttons after they are clicked here for more details.

So I decided to use another approach : I won't disable button with I will hide it and I show div element with text that would calm user down :). Thus buttons are visisble according ASP.NET but parent HTML element hides them :)

this is the aspx code that declares buttons


<div id="divButtons" style="display:inline;">
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="buttons" OnClientClick="SubmitButtonClicked();return true;" OnClick="btnSubmit_Click" style="padding-left: 10; padding-right: 10;"></asp:Button>

                   

   <input type="button" value="Cancel" onclick="javascript:top.hidePopWin(false);" style="padding-left: 10; padding-right: 10;"/>

</div>

 

<div id="divWait" style="display:none;">Note is being saved. Please wait respond from server...</div>                    

               


thus asp:Buttons controls fires SubmitButtonClicked(); javascript function before post the form.

Here is the js code:



function SubmitButtonClicked()

{

    if (typeof(ValidatorOnSubmit) == "function"){

        if (Page_ClientValidate() == false) {

            return false; }}

   

    $get("divButtons").style.display = "none";

    $get("divWait").style.display = "";

}


UPDATE: The function above is edited according the link below and all validators works as expected. :) Thanks John!

Although the bug I think it is good starting point.

You can see here for alternative solution - http://aspzone.com/blogs/john/articles/207.aspx