Connection Pooling!!

P

parez

Hi ALL!

I have questions on connection pooling..

1) Is a connection pool for a DB, Application or a machine?

I am using .net 1.1 and sql server 2k if that matters..

Parez
 
D

David Browne

parez said:
Hi ALL!

I have questions on connection pooling..

1) Is a connection pool for a DB, Application or a machine?

I am using .net 1.1 and sql server 2k if that matters..

Each connection pool lives inside an App Domain, and in each App Domain
there is a different pool for each database (connection string).

So if you have 5 applications, each connecting to 3 different databases then
you would have 15 different connection pools.

David
 
S

Sahil Malik [MVP]

I want to add a bit more. If your code uses impersonation, I believe that
creates a new connection pool within the same appdomain .. (or am I wrong?)

- SM
 
M

Miha Markic [MVP C#]

Hi Sahil,

Sahil Malik said:
I want to add a bit more. If your code uses impersonation, I believe that
creates a new connection pool within the same appdomain .. (or am I wrong?)

I am not sure about this but it sounds logical.
Here is my addition :) : David mentioned that it is per database, acutally
it is per connection string (it has to be exact match, i.e. adding a space
at the end of conneciton string will result in another connection)
 
S

Sahil Malik [MVP]

Right .. it is per Connection String.

- SM

Miha Markic said:
Hi Sahil,

Sahil Malik said:
I want to add a bit more. If your code uses impersonation, I believe that
creates a new connection pool within the same appdomain .. (or am I
wrong?)

I am not sure about this but it sounds logical.
Here is my addition :) : David mentioned that it is per database,
acutally it is per connection string (it has to be exact match, i.e.
adding a space at the end of conneciton string will result in another
connection)

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
 
P

Paul Clement

¤ I want to add a bit more. If your code uses impersonation, I believe that
¤ creates a new connection pool within the same appdomain .. (or am I wrong?)
¤
¤ - SM

I'm assuming by appdomain you mean process?

Connection pools are created per unique connection string and are process specific. You can have
multiple connection pools per process, but these pools do not cross process boundaries.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
D

David Browne

Paul Clement said:
On Fri, 9 Sep 2005 00:46:55 -0400, "Sahil Malik [MVP]"

¤ I want to add a bit more. If your code uses impersonation, I believe
that
¤ creates a new connection pool within the same appdomain .. (or am I
wrong?)
¤
¤ - SM

I'm assuming by appdomain you mean process?

Connection pools are created per unique connection string and are process
specific. You can have
multiple connection pools per process, but these pools do not cross
process boundaries.

A Process is divided into one or more App Domain. Connection Pools are
scoped to the App Domain, just like any global/static variable.

David
 
W

William \(Bill\) Vaughn

Right and if you need more detail, you can dig into the performance counters
and see how many pools (and their process names) are created. These counters
work better in 2.0, but the 1.1 counters can help (assuming you're using SQL
Server).

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
P

Paul Clement

¤
¤ ¤ > On Fri, 9 Sep 2005 00:46:55 -0400, "Sahil Malik [MVP]"
¤ >
¤ > ¤ I want to add a bit more. If your code uses impersonation, I believe
¤ > that
¤ > ¤ creates a new connection pool within the same appdomain .. (or am I
¤ > wrong?)
¤ > ¤
¤ > ¤ - SM
¤ >
¤ > I'm assuming by appdomain you mean process?
¤ >
¤ > Connection pools are created per unique connection string and are process
¤ > specific. You can have
¤ > multiple connection pools per process, but these pools do not cross
¤ > process boundaries.
¤ >
¤
¤ A Process is divided into one or more App Domain. Connection Pools are
¤ scoped to the App Domain, just like any global/static variable.
¤
¤ David
¤

Interesting. I have to admit that I'm not familiar with AppDomains. Sounds more like a managed code
(.NET) extension rather than something that is specific to the connection pool implementation.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
M

Mark Ashton

Here is a bit of V2.0 C# code using PerformanceCounters that shows each
appdomain has its own connection pool for SqlClient. Pooling happens in
native code for Odbc & OleDb, so their pooling is per process.

using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

class CrossDomain : System.MarshalByRefObject {

[DllImport("kernel32.dll")]
private static extern int GetCurrentProcessId();

private static string GetAssemblyName() {
Assembly assembly = Assembly.GetEntryAssembly();
if (null != assembly) {
AssemblyName name = assembly.GetName();
return name.Name;
}
return null;
}

private static string GetInstanceName() {
string instanceName = GetAssemblyName();
if (String.IsNullOrEmpty(instanceName)) {
AppDomain appDomain = AppDomain.CurrentDomain;
if (null != appDomain) {
instanceName = appDomain.FriendlyName;
}
}
int pid = GetCurrentProcessId();
return String.Format("{0}[{1}]", instanceName, pid);
}

public string Start(string constr) {
using(SqlConnection c = new SqlConnection(constr)) {
c.Open();
}
return GetInstanceName();
}
}

class Program {

private static void NumberOfActiveConnectionPools(string name) {
PerformanceCounter instance = new PerformanceCounter();
instance.CategoryName = ".NET Data Provider for SqlServer";
instance.CounterName = "NumberOfActiveConnectionPools";
instance.InstanceName = name;
instance.ReadOnly = true;
Console.WriteLine("{0}: NumberOfActiveConnectionPools={1}",
instance.InstanceName, instance.RawValue);
}

public static void Main(string[] args) {
try {
CrossDomain c0 = new CrossDomain();

AppDomain ad =AppDomain.CreateDomain("Test");
CrossDomain c1 =
(CrossDomain)ad.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location,
typeof(CrossDomain).FullName);

string name1 = c0.Start("Data Source=.;integrated
security=sspi");
string name2 = c1.Start("Data Source=.;integrated
security=sspi");
string name3 = c1.Start("server=.;trusted_connection=sspi");

NumberOfActiveConnectionPools(name1);
NumberOfActiveConnectionPools(name2);
if (name2 != name3) {
NumberOfActiveConnectionPools(name3);
}
}
catch (Exception e) {
Console.WriteLine(e);
}
}
}
 
S

Sahil Malik [MVP]

Pools are per appdomain, per connection string. In case of NT Auth the pool
is further partitioned <-- That is the whole truth.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----------------------------------------------------------------------------



Mark Ashton said:
Here is a bit of V2.0 C# code using PerformanceCounters that shows each
appdomain has its own connection pool for SqlClient. Pooling happens in
native code for Odbc & OleDb, so their pooling is per process.

using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

class CrossDomain : System.MarshalByRefObject {

[DllImport("kernel32.dll")]
private static extern int GetCurrentProcessId();

private static string GetAssemblyName() {
Assembly assembly = Assembly.GetEntryAssembly();
if (null != assembly) {
AssemblyName name = assembly.GetName();
return name.Name;
}
return null;
}

private static string GetInstanceName() {
string instanceName = GetAssemblyName();
if (String.IsNullOrEmpty(instanceName)) {
AppDomain appDomain = AppDomain.CurrentDomain;
if (null != appDomain) {
instanceName = appDomain.FriendlyName;
}
}
int pid = GetCurrentProcessId();
return String.Format("{0}[{1}]", instanceName, pid);
}

public string Start(string constr) {
using(SqlConnection c = new SqlConnection(constr)) {
c.Open();
}
return GetInstanceName();
}
}

class Program {

private static void NumberOfActiveConnectionPools(string name) {
PerformanceCounter instance = new PerformanceCounter();
instance.CategoryName = ".NET Data Provider for SqlServer";
instance.CounterName = "NumberOfActiveConnectionPools";
instance.InstanceName = name;
instance.ReadOnly = true;
Console.WriteLine("{0}: NumberOfActiveConnectionPools={1}",
instance.InstanceName, instance.RawValue);
}

public static void Main(string[] args) {
try {
CrossDomain c0 = new CrossDomain();

AppDomain ad =AppDomain.CreateDomain("Test");
CrossDomain c1 =
(CrossDomain)ad.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location,
typeof(CrossDomain).FullName);

string name1 = c0.Start("Data Source=.;integrated
security=sspi");
string name2 = c1.Start("Data Source=.;integrated
security=sspi");
string name3 = c1.Start("server=.;trusted_connection=sspi");

NumberOfActiveConnectionPools(name1);
NumberOfActiveConnectionPools(name2);
if (name2 != name3) {
NumberOfActiveConnectionPools(name3);
}
}
catch (Exception e) {
Console.WriteLine(e);
}
}
}


--
This posting is provided "AS IS", with no warranties, and confers no
rights. Please do not send email directly to this alias. This alias is for
newsgroup purposes only.

David Browne said:
A Process is divided into one or more App Domain. Connection Pools are
scoped to the App Domain, just like any global/static variable.

David
 
P

Paul Clement

¤ Pools are per appdomain, per connection string. In case of NT Auth the pool
¤ is further partitioned <-- That is the whole truth.
¤

Sahil,

Are you also referring to unmanaged providers? Based upon what Mark stated it doesn't appear that
there is any knowledge of AppDomains by unmanaged providers/drivers.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
S

Sahil Malik [MVP]

Only SqlClient.

- SM

Paul Clement said:
¤ Pools are per appdomain, per connection string. In case of NT Auth the pool
¤ is further partitioned <-- That is the whole truth.
¤

Sahil,

Are you also referring to unmanaged providers? Based upon what Mark stated it doesn't appear that
there is any knowledge of AppDomains by unmanaged providers/drivers.


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top