Click here to Skip to main content
15,893,588 members
Articles / Web Development / ASP.NET

Connection Pooling in multithreaded applications

Rate me:
Please Sign up or sign in to vote.
3.29/5 (8 votes)
1 Mar 2010CPOL3 min read 89.8K   3.3K   24  
Approaches describing a design to handle connection objects and pools in C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using ConnLibrary;

namespace ProjectBusiness
{
    public class Project
    {
        public List<Employee> Employees { get; set; }
        public string Name { get; set; }

        private object updateStatusLock = new object();

        /// <summary>
        /// Updates the status of Project
        /// </summary>
        /// <exception cref="System.Data.SqlClient.SqlException">System.Data.SqlClient.SqlException</exception>
        /// <exception cref="System.InvalidOperationException">System.InvalidOperationException</exception>
        public void UpdateStatus()
        {
            lock (updateStatusLock)
            {
                DBConnectionManager connManager = new DBConnectionManager();
                DbTransaction transaction = null;          
                try
                {
                    connManager.Connection.Open();
                    //It is Project objects responsibility to commit multiple Database updates in one transaction. 
                    transaction = connManager.Connection.BeginTransaction();
                    //some checks for project status..
                    string cmdString = "INSERT INTO PROJECTS VALUES('" + Name + "','Hello')";
                    connManager.Connection.ExecuteScalar(cmdString, transaction);
                    if (Employees != null)
                        foreach (Employee employee in Employees)
                            employee.UpdateStatus(this, transaction,connManager);
                    transaction.Commit();
                    transaction = null;
                }
                catch (System.InvalidOperationException invalidOperatioExcpetion)
                {
                    throw;
                }
                catch (System.Data.SqlClient.SqlException sqlException)
                {
                    throw;
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (connManager.Connection.IsOpen())
                    {
                       connManager.Connection.Close();
                    }
                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }
                    transaction = null;
                    connManager = null;
                }
            }

        }


        /// <summary>
        /// Updates the status of Project
        /// </summary>
        /// <exception cref="System.Data.SqlClient.SqlException">System.Data.SqlClient.SqlException</exception>
        /// <exception cref="System.InvalidOperationException">System.InvalidOperationException</exception>
        public void UpdateStatusSingleton()
        {
            lock (updateStatusLock)
            {
                DbTransaction transaction = null;
                try
                {
                    SharedConnectionManager.Connection.Open();
                    //It is Project objects responsibility to commit multiple Database updates in one transaction. 
                    transaction = SharedConnectionManager.Connection.BeginTransaction();
                    //some checks for project status..
                    string cmdString = "INSERT INTO PROJECTS VALUES('" + Name + "','Hello')";
                    SharedConnectionManager.Connection.ExecuteScalar(cmdString, transaction);
                    if (Employees != null)
                        foreach (Employee employee in Employees)
                            employee.UpdateStatus(this, transaction);
                    transaction.Commit();
                    transaction = null;
                    SharedConnectionManager.Connection.Close();
                }
                catch (System.InvalidOperationException invalidOperatioExcpetion)
                {
                    throw;
                }
                catch (System.Data.SqlClient.SqlException sqlException)
                {
                    throw;
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (SharedConnectionManager.Connection.IsOpen())
                    {
                        SharedConnectionManager.Connection.Close();
                    }
                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }
                    transaction = null;
                }
            }

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions