Click here to Skip to main content
15,893,668 members
Articles / Database Development / SQL Server

Database Concurrency Patterns - SIP and SUP

Rate me:
Please Sign up or sign in to vote.
3.94/5 (6 votes)
29 Jul 2008CPOL4 min read 28.1K   151   26  
Take a look at two new patterns to help with database concurrency: SIP and SUP.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace DatabaseConcurrency
{
    public static class Dal
    {
        private static string dbconnectstring = ConfigurationManager.ConnectionStrings["DbConnectString"].ConnectionString;
        /// <summary>
        /// Make a call to get the latest unique identifier
        /// </summary>
        /// <param name="NumberClaimed"></param>
        /// <param name="ThreadId"></param>
        /// <returns></returns>
        public static DataTable ThreadCall(int NumberClaimed, int ThreadId)
        {
            OleDbConnection sc = null;
            OleDbDataAdapter sda = null;
            try
            {
                sc = new OleDbConnection(dbconnectstring);
                sc.Open();
                sda = new OleDbDataAdapter("SingleUpdateTableCall", sc);
                sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                OleDbCommandBuilder.DeriveParameters(sda.SelectCommand);
                sda.SelectCommand.Parameters["ThreadId"].Value = ThreadId;
                sda.SelectCommand.Parameters["NumberClaimed"].Value = NumberClaimed;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
            finally
            {
                if (sda != null) sda.Dispose();
                sda = null;
                if (sc != null)
                {
                    if (sc.State == ConnectionState.Open)
                        sc.Close();
                    sc.Dispose();
                }
                sc = null;
            }
        }
        /// <summary>
        /// Get a history of the Single Update Table so we can see what activity
        /// is going on when generating unique identifiers
        /// </summary>
        /// <returns>DataTable</returns>
        public static DataTable GetDbResults()
        {
            OleDbConnection sc = null;
            OleDbDataAdapter sda = null;
            try
            {
                sc = new OleDbConnection(dbconnectstring);
                sc.Open();
                sda = new OleDbDataAdapter("SingleUpdateTableHistory", sc);
                sda.SelectCommand.CommandType = CommandType.TableDirect; ;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
            finally
            {
                if (sda != null) sda.Dispose();
                sda = null;
                if (sc != null)
                {
                    if (sc.State == ConnectionState.Open)
                        sc.Close();
                    sc.Dispose();
                }
                sc = null;
            }
        }
        /// <summary>
        /// Get a history of all the identifiers we have generated
        /// </summary>
        /// <returns></returns>
        public static DataTable GetActionResults()
        {
            OleDbConnection sc = null;
            OleDbDataAdapter sda = null;
            try
            {
                sc = new OleDbConnection(dbconnectstring);
                sc.Open();
                sda = new OleDbDataAdapter("SingleUpdateAction", sc);
                sda.SelectCommand.CommandType = CommandType.TableDirect; ;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
            finally
            {
                if (sda != null) sda.Dispose();
                sda = null;
                if (sc != null)
                {
                    if (sc.State == ConnectionState.Open)
                        sc.Close();
                    sc.Dispose();
                }
                sc = null;
            }
        }
        /// <summary>
        /// Clean up the tables and create an initial value
        /// </summary>
        public static void Setup()
        {
            OleDbConnection sc = null;
            OleDbCommand sda = null;
            try
            {
                sc = new OleDbConnection(dbconnectstring);
                sc.Open();
                sda = new OleDbCommand("SingleUpdateTableSetup", sc);
                sda.CommandType = CommandType.StoredProcedure;
                sda.ExecuteNonQuery();
            }
            finally
            {
                if (sda != null) sda.Dispose();
                sda = null;
                if (sc != null)
                {
                    if (sc.State == ConnectionState.Open)
                        sc.Close();
                    sc.Dispose();
                }
                sc = 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
Architect
Australia Australia
George Zabanah has been architecting and developing solutions commercially from start to finish for over 11 years. He has been programming since he was 11 (for over 25 years now!) using many different technologies (almost all Microsoft). George is proficient in many languages and employs best practices wherever possible using design patterns, .NET, XSLT, XML, Regular Expressions, various flavours of SQL (to name just a few). His favourite tools, however, are the whiteboard and Sparx Enterprise Architect/Powerpoint. Many waking moments have been spent by George thinking about solutions in his head (even while watching tv). His best moments are the "Eureka" moments when he wakes up from a good sleep (or after watching tv) to find that his latest challenge has been solved!

Comments and Discussions