Tag Archive: sqlserver


A very good book by the authors that I admire and appreciate mentioned the use of a class called SQLAsyncResult to access data asynchronously using ADO.NET 2.0 via the callback approach. My attempt to try out an example failed as I couldn’t find the SQLAsyncResult class, not in the namespace, not in the MSDN library. However, I did find a post on ASP.NET forum about a dev’s unsuccessful hunt for this class and an MVP answering that it might be a type in the example.
The reason I’m mentioning it at the start of this post is that, if any one developer finds this class, I would really appetite you leaving a comment.
Now, back to the Callback approach …

This post is in continuation to my previous post about Asynchronous data access using ADO.NET however, that post need not be referred if you are interesting in the callback only.
In this approach we leverage .NET infrastructure relating to threading, AsyncCallback delegate, the AsyncResult class and the IAsyncResult interface.
The approach is accomplished by:

  • Creating a call back method which will process the data (essentially call the end invoke command) which accepts the IAsyncResult
  • Creating an AsyncCallback delegate for the callback methods created &
  • Passing the delegate along with the command object in the begin invoke command.

Here’s a simple implementation of the call back approach:
The model class to load data:

public class Customer {

    public Customer() {
    }

    public Customer(SqlDataReader reader) {

        CustomerID = reader["CustomerID"].ToString();
        CompanyName = reader["CompanyName"] == null ? string.Empty : reader["CompanyName"].ToString();
        ContactName = reader["ContactName"] == null ? string.Empty : reader["ContactName"].ToString();
        Phone = reader["Phone"] == null ? string.Empty : reader["Phone"].ToString();
    }

    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string Phone { get; set; }

}

Here’s the data access class that fetches customer asynchronously using the callback approach:

public class CustomerDB {

    /// <summary>
    /// This variable keeps the track of number of asynchronous calls
    /// </summary>
    private static int counter = 0;

    /// <summary>
    /// Class member to store the result.
    /// </summary>
    private List<Customer> _customers = new List<Customer>();

    /// <summary>
    /// This is the main method that needs to be called from other application layer to get Customers.
    /// </summary>
    /// <returns></returns>
    public List<Customer> GetCustomers() {

        //Initiate Connection
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

        //Initiate Command
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "SELECT top 10 * FROM [Customers]";
        cmd.CommandType = CommandType.Text;

        try {

            con.Open();

            // create AsyncCallback delegate
            AsyncCallback callback = new AsyncCallback(RetrieveDataCallback);

            cmd.BeginExecuteReader(callback, cmd, CommandBehavior.CloseConnection);

            //Wait until the call is complete
            while (counter < 1) {
            }

        }
        catch {
            //Log error
        }

        return _customers;

    }

    /// <summary>
    /// The callback method to retrieve data.
    /// </summary>
    /// <param name="result"></param>
    private void RetrieveDataCallback(IAsyncResult result) {

        SqlDataReader dr = null;

        try {
            SqlCommand command = (SqlCommand)result.AsyncState;
            Customer customer = null;

            dr = command.EndExecuteReader(result);

            while (dr.Read()) {

                customer = new Customer(dr);
                _customers.Add(customer);
            }
        }
        catch {
            //Log error
        }
        finally {

            try {
                if (!dr.IsClosed) {
                    dr.Close();
                }
            }
            catch { } //Suppress any exceptions here

            //Increment value to notify that the call is complete.
            Interlocked.Increment(ref counter);

        }
    }
}

Let’s rewrite this code to make multiple calls to different databases using the same customer model class. To make sure that the data access is thread safe we need to add a static object. This static object needs to be locked every time a thread completes and attempts to update the Customers list. Here’s the new CustomerDB class:

public class CustomerDB {

    /// <summary>
    /// static object for thread safety.
    /// </summary>
    static object lockObject = new object();

    /// <summary>
    /// This variable keeps the track of number of asynchronous calls
    /// </summary>
    private static int counter = 0;

    /// <summary>
    /// Class member to store the result.
    /// </summary>
    private List<Customer> _customers = new List<Customer>();

    /// <summary>
    /// This is the main method that needs to be called from other application layer to get Customers.
    /// </summary>
    /// <returns></returns>
    public List<Customer> GetCustomers() {

        //Initiate Connection
        SqlConnection con1 = new SqlConnection();
        con1.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection con2 = new SqlConnection();
        con1.ConnectionString = ConfigurationManager.ConnectionStrings["SouthwindConnectionString"].ConnectionString;
        SqlConnection con3 = new SqlConnection();
        con1.ConnectionString = ConfigurationManager.ConnectionStrings["EastwindConnectionString"].ConnectionString;

        //Initiate Command
        SqlCommand cmd1 = con1.CreateCommand();
        SqlCommand cmd2 = con2.CreateCommand();
        SqlCommand cmd3 = con3.CreateCommand();

        //The table structures are same in my case.
        //In case of different table structures include logic in the call back method
        //or Customer constructor to DB specific load.
        cmd1.CommandText = "SELECT top 10 * FROM [Customers]";
        cmd1.CommandType = CommandType.Text;
        cmd2.CommandText = "SELECT top 10 * FROM [Customers]";
        cmd2.CommandType = CommandType.Text;
        cmd3.CommandText = "SELECT top 10 * FROM [Customers]";
        cmd3.CommandType = CommandType.Text;

        try {

            con1.Open();
            con2.Open();
            con3.Open();

            // create AsyncCallback delegate
            AsyncCallback callback1 = new AsyncCallback(RetrieveDataCallback);
            AsyncCallback callback2 = new AsyncCallback(RetrieveDataCallback);
            AsyncCallback callback3 = new AsyncCallback(RetrieveDataCallback);

            cmd1.BeginExecuteReader(callback1, cmd1, CommandBehavior.CloseConnection);
            cmd2.BeginExecuteReader(callback2, cmd2, CommandBehavior.CloseConnection);
            cmd3.BeginExecuteReader(callback3, cmd3, CommandBehavior.CloseConnection);

            //Wait until all the call are complete
            while (counter < 3) {
            }

        }
        catch {
            //Log error
        }

        return _customers;

    }

    /// <summary>
    /// The callback method to retrieve data.
    /// </summary>
    /// <param name="result"></param>
    private void RetrieveDataCallback(IAsyncResult result) {

        SqlDataReader dr = null;

        try {
            SqlCommand command = (SqlCommand)result.AsyncState;
            Customer customer = null;

            dr = command.EndExecuteReader(result);

            while (dr.Read()) {

                customer = new Customer(dr);

                // lock the static object when data is loaded
                lock (lockObject) {

                    _customers.Add(customer);
                }

            }
        }
        catch {
            //Log error
        }
        finally {

            try {
                if (!dr.IsClosed) {
                    dr.Close();
                }
            }
            catch { } //Suppress any exceptions here

            //Increment value to notify that the call is complete.
            Interlocked.Increment(ref counter);

        }
    }
}

Asynchronous Data Access using ADO.NET

ADO.NET 2.0 enabled the infrastructure required for accessing data in asynchronous mode.

Three standard approaches available to achieve this are:

  • The Poll approach,
  • The Wait approach &
  • The Callback approach.

The approach to choose will depend upon the scenario. Between the Poll and Wait approaches, the Wait approach is better. It provides a great deal of flexibility and efficiency with a bit more complexity than the other. The Callback approach is extreme and runs more in parallel to the multi threading and other asynchronous process paradigm. It helps creating asynchronous/multithreaded data access mechanism even outside SQL/ADO.NET. Drivers and Frameworks which do not come with asynchronous infrastructure.

To enable asynchronous data access, we would need to add “Asynchronous Processing=true;” in the connection string.

The asynchronous access is triggered by the command methods prefixed by “Begin” and the result is retrieved through their “End” counterparts as below:

Synchronous Methods
Asynchronous methods
Start process returns IAsunchResult End process returns data
ExecuteNonQuery BeginExecuteNonQuery EndExecuteNonQuery
ExecuteReader BeginExecuteReader EndExecuteReader
ExecuteXmlReader BeginExecuteXmlReader EndExecuteXmlReader

Each of these asynchronous execution methods return  IAsyncResult. The IAsyncResult allows us to control and read into the asynchronous call with it properties – IsCompleted, AsyncState,  CompletedSynchronously & AsyncWaitHandle. The AsyncWaitHandle property returns the WaitHandle object.

The WaitHandle is an abstract class which provides a handle to the asynchronous execution. It exposed methods such as WaitOne(), WaitAny() and WaitAll() notifying the status of the execution. To process more than one databases commands, we can simply create an array containing wait handles for each process and wait till the WaitAll() or WaitAny() responds.

AsyncCallback is a delegate, used to specify which method to be called once the Asynchronous process is over.

For the next few examples we’ll be using the following modal and it’s constructor. Passing the reader is not the best way to initiate a class in many scenarios but it works for our examples.

public class Customer {

    public Customer() {
    }

    public Customer(SqlDataReader reader) {

        CustomerID = reader["CustomerID"].ToString();
        CompanyName = reader["CompanyName"] == null ? string.Empty : reader["CompanyName"].ToString();
        ContactName = reader["ContactName"] == null ? string.Empty : reader["ContactName"].ToString();
        Phone = reader["Phone"] == null ? string.Empty : reader["Phone"].ToString();
    }

    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string Phone { get; set; }

}

The Poll Approach:

In the poll approach, we make a call to the database, executing the required command and keep polling the status or the command to check if the execution is complete. Here’s the method that does just that:

public List<Customer> GetAllCustomers() {

    List<Customer> customers = new List<Customer>();
    IAsyncResult asyncResult = null;;
    SqlDataReader sqlReader = null;

    SqlConnection sqlConnection = new SqlConnection();
    sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlCommand sqlCommand = sqlConnection.CreateCommand();
    sqlCommand.CommandText = "SELECT top 10 * FROM [Customers]";
    sqlCommand.CommandType = CommandType.Text;

    try{
        sqlConnection.Open();

        //Start Async process
        asyncResult = sqlCommand.BeginExecuteReader();

        while (!asyncResult.IsCompleted) {
            //Wait till Async process is executing
            Thread.Sleep(10);
        }

        //Retrieve result from the Async process
        sqlReader = sqlCommand.EndExecuteReader(asyncResult);

        while (sqlReader.Read()) {
            customers.Add(new Customer(sqlReader));
        }

        sqlConnection.Close();
    }
    catch(Exception ex){

    }

    return customers;

}

The Wait Approach

Using this approach we can start multiple asynchronous processes and wait till they are complete. The real benefit of  the wait process doesn’t come into the picture unless you have to make multiple database calls in the same or make calls to different databases to get the same entity.

Imagine we need to fetch customers as well as employees using the following employee model

public class Employee {

    public Employee() { }

    public Employee(SqlDataReader reader) {

        EmployeeID = (int)reader["EmployeeID"];
        LastName = reader["LastName"] == null ? string.Empty : reader["LastName"].ToString();
        FirstName = reader["FirstName"] == null ? string.Empty : reader["FirstName"].ToString();
        Title = reader["Title"] == null ? string.Empty : reader["Title"].ToString();

    }

    public int EmployeeID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Title { get; set; }
}

In order to enable the connection to support multiple result sets, as in this case, we would need to set the MultipleActiveResultSets property of the connection string to true. To do that add “MultipleActiveResultSets = true;” in the connection string.

After those setting taken care of, we fire multiple commands using the wait approach:

List<Customer> customers = new List<Customer>();
    List<Employee> employees = new List<Employee>();

    SqlDataReader customerSqlReader = null;
    SqlDataReader employeeSqlReader = null;

    IAsyncResult customerAsyncResult = null;
    IAsyncResult employeeAsyncResult = null;
    WaitHandle[] waitHandles = new WaitHandle[2];

    SqlConnection sqlConnection = new SqlConnection();
    sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

    SqlCommand customerSqlCommand = new SqlCommand();
    customerSqlCommand.Connection = sqlConnection;
    customerSqlCommand.CommandText = "SELECT top 10 * FROM [Customers]";
    customerSqlCommand.CommandType = CommandType.Text;

    SqlCommand employeeSqlCommand = new SqlCommand();
    employeeSqlCommand.Connection = sqlConnection;
    employeeSqlCommand.CommandText = "SELECT top 10 * FROM [Employees]";
    employeeSqlCommand.CommandType = CommandType.Text;

    try{
        sqlConnection.Open();

        //Start Async processes
        customerAsyncResult = customerSqlCommand.BeginExecuteReader();
        employeeAsyncResult = employeeSqlCommand.BeginExecuteReader();
        waitHandles[0] = customerAsyncResult.AsyncWaitHandle;
        waitHandles[1] = employeeAsyncResult.AsyncWaitHandle;

        WaitHandle.WaitAll(waitHandles);

        //Retrieve result from the Async process
        customerSqlReader = customerSqlCommand.EndExecuteReader(customerAsyncResult);
        while (customerSqlReader.Read()) {
            customers.Add(new Customer(customerSqlReader));
        }

        employeeSqlReader = employeeSqlCommand.EndExecuteReader(employeeAsyncResult);
        while (employeeSqlReader.Read()) {
            employees.Add(new Employee(employeeSqlReader));
        }

        sqlConnection.Close();
    }
    catch(Exception ex){

    }

In the above code snippet  we wait for all the database calls to complete before processing it. An improvement that can be made here is processing data as we receive it. To achieve that, we would have to use the WaitAny() instead of the WaitAll() method and running a loop for each execution. Here’s how:

List<Customer> customers = new List<Customer>();
    List<Employee> employees = new List<Employee>();

    SqlDataReader customerSqlReader = null;
    SqlDataReader employeeSqlReader = null;

    IAsyncResult customerAsyncResult = null;
    IAsyncResult employeeAsyncResult = null;
    WaitHandle[] waitHandles = new WaitHandle[2];

    SqlConnection sqlConnection = new SqlConnection();
    sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

    SqlCommand customerSqlCommand = new SqlCommand();
    customerSqlCommand.Connection = sqlConnection;
    customerSqlCommand.CommandText = "SELECT top 10 * FROM [Customers]";
    customerSqlCommand.CommandType = CommandType.Text;

    SqlCommand employeeSqlCommand = new SqlCommand();
    employeeSqlCommand.Connection = sqlConnection;
    employeeSqlCommand.CommandText = "SELECT top 10 * FROM [Employees]";
    employeeSqlCommand.CommandType = CommandType.Text;

    try{
        sqlConnection.Open();

        //Start Async processes
        customerAsyncResult = customerSqlCommand.BeginExecuteReader();
        employeeAsyncResult = employeeSqlCommand.BeginExecuteReader();
        waitHandles[0] = customerAsyncResult.AsyncWaitHandle;
        waitHandles[1] = employeeAsyncResult.AsyncWaitHandle;

        for (int i = 0; i < 2; i++) {

            int waitHandleIndex = WaitHandle.WaitAny(waitHandles);

            switch (waitHandleIndex) {

                case 0:
                    customerSqlReader = customerSqlCommand.EndExecuteReader(customerAsyncResult);

                    while (customerSqlReader.Read()) {
                        customers.Add(new Customer(customerSqlReader));
                    }

                    break;

                case 1:
                    employeeSqlReader = employeeSqlCommand.EndExecuteReader(employeeAsyncResult);

                    while (employeeSqlReader.Read()) {
                        employees.Add(new Employee(employeeSqlReader));
                    }

                    break;
            }
        }

        sqlConnection.Close();
    }
    catch(Exception ex){

    }

The Callback approach:

This wait approach it great will give the best solution in all the practical scenarios that you will come across.  Please refer to the next blog post for the callback approach.

Batch Updates in ADO.NET 2.0 for Improved Performance

When you updated a database using the DataAdapter in .NET 1.1 each command was sent to the database one at a time.  This caused a lot of roundtrips to the database.

ADO.NET 2.0 has introduced the concept of Batch Updates, which allows you to designate the number of commands sent to the database at a given time.  If used correctly, this can increase the performance of your data access layer by reducing the number of roundtrips to the database.

DataAdapter.UpdateBatchSize Property

The DataAdapter has an UpdateBatchSize Property that allows you to set the number of commands that will be sent to the database with each request.

  • UpdateBatchSize = 1, disables batch updates
  • UpdateBatchSize = X where X > 1, sends x statements to the database at a time
  • UpdateBatchSize = 0, sends the maximum number of statements at a time allowed by the server

Command.UpdatedRowSource Property

When using batch mode, the UpdatedRowSource property of the command can only be set to either UpdatedRowSource.None or UpdatedRowSource.OutputParameters

Continue reading “Batch Updates in ADO.NET 2.0 for Improved Performance” »

Powered by WordPress | Theme: by 85ideas. Editor by Khoanguyen