Connection Manager and multi-threaded calls to Save()

Connection Manager and multi-threaded calls to Save()

Old forum URL: forums.lhotka.net/forums/t/9443.aspx


snakebyteme posted on Friday, August 27, 2010

Our BOs use the connection manager class.

Our processing is mainly client side in the code and each object has no dependency on the other object being processed. To speed up the time it takes to process 2000+ objects, we are executing 8 processing threads.

When we try to do parallel processing of 8 business objects at a time, we get deadlock issues from the DB since the connection manager is giving the same connection for each thread. Is there a way for the ConnectionManager to be thread aware and store a structure like (ThreadID,ConnectionString, Connection, Counter) ?

Most of our BOs have similar code like this to do Inserts into the DB:

    Private Sub AddUser()
        Using ctx = ConnectionManager(Of SqlClient.SqlConnection).GetManager(DAL.SQL.DBName)
            Using db As New SQL(ctx.Connection)

                Try
                    Dim params As New List(Of GenericDBParameter)
                    params.Add(New GenericDBParameter("UserID", DbType.Int32))
                    PopulateCoreDBParameters(params)
                    db.ExecuteScalar("UserAdd", params)
                    LoadProperty(mRowVersion, db.GetReturnValue("NewRowVersion"))
                    LoadProperty(mUserID, db.GetReturnValue("UserID"))
                Catch ex As Exception
                    ExceptionPolicy.HandleException(ex, Constants.ExceptionPolicy.DAL)
                End Try
            End Using
        End Using
    End Sub

RockfordLhotka replied on Friday, August 27, 2010

The Csla.Data "manager" classes all use ApplicationContext to maintain their information.

ApplicationContext works differently depending on whether you are running in ASP.NET, WPF, Silverlight or elsewhere.

Sometimes the context is just per-thread, but it can be per-logical-session (ASP.NET) or per-AppDomain (WPF/SL).

If you want to be sure to get a different connection on a per-thread basis, I recommend using the 'label' parameter and set it to a value constructed as you describe (threadid, etc).

Copyright (c) Marimer LLC