VisualBuilder
  Home > Dotnet > Tutorials > Connection pooling in .Net Applications - .NET Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
.NET Tutorial Home
1 . Overview of .Net Framework
2 . .Net Components:
3 . CLR (Common Language Runtime)
4 . Steps to Run a program in CLR
5 . Assembly
6 . GAC (Global Assembly CACHE)
7 . Strong Name in .Net Assembly
8 . ILDASM: Intermediate Language Disassembler in .Net
9 . Custom Attributes in .Net
10 . Application Domain in .Net
11 . Code Access Security in .Net
12 . Type System in .Net -1
13 . Type System in .Net -2
14 . Reflection: Inspection of a Type\'s Metadata
15 . Introduction to Generics
16 . Generic Classes and Methods
17 . Generic Methods in .Net 2.0
18 . Overview of Inheritance in Generics classes
19 . Connection pooling in .Net Applications
20 . Deployment in .Net
 
Dotnet Group Home
Dotnet Discussion (9)
Dotnet Members (2825)
Dotnet Resources
Dotnet Source Code (0)
Dotnet Articles (0)
Dotnet Blogs
Dotnet Jobs
Dotnet Components (2)
Dotnet Books
Dotnet Websites (43)
Dotnet News (186)
Dotnet Q & A (3)
- Dotnet Ask Question
- Dotnet Questions
- Dotnet Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Dotnet Tutorial
 Connection pooling in .Net Applications
  << Prev: Overview of Inheritance in Generics classes Next: Deployment in .Net >>

A connection pool is a set of database connections that are available for an application to use. Connection Pooling is the concept of using a connection pool for enhanced performance. As we know every application requires connection to database to perform operations like insert, update and delete and to fetch records from database to display. This requires a connection to be specified between the applications so that user can transfer data to and fro from the database server. Connection pooling is the concept that helps in maintaining or reusing the already open connection and hence improves site performance.


 


Connection pooling is used to enhance the performance of executing commands on a database. Before executing a command a connection to that database needs to be established. Sometimes creating and tearing down the connection is more costly then executing the command.


 


Concept behind Connection Pooling:


 



  • When a user request a connection, it is returned from the pool rather than establishing new connection and, when a user releases a connection, it is returned to the pool rather than being released.

  • Be sure than your connections use the same connection string each time. Connection pooling only works if the connection string is the same. If the connection string is different, then a new connection will be opened, and connection pooling won't be used.

  • Connection pooling is extremely useful when used with applications that do not have a state. State is a presence between instances. Active Server pages are considered stateless since they do not share data between themselves. Stateless applications benefit from connection pooling since they can not hold a connection open by themselves.

  • The main benefit of pooling is performance. Making a connection to a database can be very time-consuming, depending on the speed of the network as well as on the proximity of the database server. When pooling is enabled, the request for a database connection can be satisfied from the pool rather than by (re)connecting to the server, (re)authenticating the connection information, and returning (again) a validated connection to the application.

  • Multiple connections, all of which may not be of use, to the database are open, this is the flaw in the connection pooling, and developer must address this first before implementation of connection pooling.


Managing Connection Pooling


Connection pooling allows you to reuse connections that are already opened, rather than creating a new one every time we need to send/retrieve the data from the database. Connection pooling behavior can be controlled by using connection string. For E.g.


In Ado.Net data providers, connection string options can defines:



  • Number of connections pools

  • Number of connections in a pool

  • Lifetime of pooled connections used by each process.


Note:- Connection pooling in Ado.Net is not provided by the core components of .Net framework, this must be implemented in the Ado.Net data provider itself.


 


 


Creating a Connection Pool


The connection pool is associated with a specific connection string. By default, the connection pool is created when the first connection with a unique connection string connects to the database. The pool is populated with connections up to the minimum pool size. Additional connections can be added until the pool reaches the maximum pool size.



  • Pool remains active as long as connection remains open

  • If a new connection is open, the connection string doesn't match with the one which is used earlier to connect to the database, then new connection pool will be created.

  • Connection pooling enhanced the performance and scalability of the applications.

  • Once created connection pools are not destroyed until the active process ends or the connection lifetime is exceeded.


Example - Demonstrate Connection Pooling


 


In below code snipplet Connection Pool “A” is created and filled with connections upto the minimum pool size .


 


SqlConnection con = null;


string conStr = "";


conStr = "Data Source=localhost; initial catalog=TestDB; uid=sa; pwd=sa; Min Pool Size=50";

con = new SqlConnection(conStr);


con.open();


 


Opening and Closing Connections


Open connections just before they are needed. Opening them earlier than necessary decreases the number of connections available to other users and can increase the demand for resources. One should explicitly close the connection as soon as it is no longer in use. If one waits for garbage collection to implicitly close connection that go out of scope, the connections are not returned to the connection pool immediately.


Always, close connection in the finally block, because code in the finally block always runs, regardless of whether an exception occurs. This guarantees explicitly closing of connection. For example:


Try {


    Con.open ();


} Catch (Exception ex) {


    // Handle Exception


} Finally {


    // Close the Connection


    If (Con! = null)


        Con.close ();


}


  << Prev: Overview of Inheritance in Generics classes Next: Deployment in .Net >>
Dotnet Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
Skype vs. sipcall
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising