Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...Threading issues with DNN Provider architectureThreading issues with DNN Provider architecture
Previous
 
Next
New Post
5/21/2008 11:25 AM
 

Whenever I create a dynamic DNN module in VS, the installed DNN template automatically creates a DataProvider, with a shared object for the Data Provider as follows:

#Region "Shared/Static Methods"

        ' singleton reference to the instantiated object
        Private Shared objProvider As DataProvider = Nothing

        ' constructor
        Shared Sub New()
            CreateProvider()
        End Sub

        ' dynamically create provider
        Private Shared Sub CreateProvider()
            objProvider = CType(Framework.Reflection.CreateObject("data", NameSpace, ""), DataProvider)
        End Sub

        ' return the provider
        Public Shared Shadows Function Instance() As DataProvider
            Return objProvider
        End Function

#End Region

Now, controllers are intended to use Instance to obtain the shared copy of the Provider, and execute operations. There might be Threading issues with this approach according to me.

Firstly, this approach cannot support transactions.

More importantly, this approach can lead to inconsistencies. Suppose, this provider is for manipulating 'Department' entity in an application.

Now, suppose 2 users are simultaneously adding departments by calling AddDept whose definition is:

1)        Public Overrides Function AddDepartment(ByVal info As DepartmentInfo) As Integer

2)            Return (SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("AddDepartment"), _
3)                            info.portalId, info.name, info.acronym))
4)        End Function

After 1st user's Thread for creating Department reached Line 1), suppose the OS context switches to the Thread of the other user also creating a department. Now, info has been populated with the data from First user, when the context switch occurs.

After the switch, info is poplulated with data from 2nd user, and is successfully entered via ExecuteNonQuery. Then, the control returns back to the Thread of the first user. The important point to remember is that both used Instance() to get a copy of the provider, which returned the shared copy.

So, the AddDept method's Activation Record in memory would be same for both Threads. When the control returns to 1st user's thread, "info" reference in this activation record has effectively been over-written by 2nd user's data.

So, when ExecuteNonQuery() happens for the first user, it is actually the second user's data which is being inserted.

Anyone, feels this is a valid problem??? And if yes, then the entire DNN provider model is at question!!!

One point to notice here is that the problem does not reside with the Provider Model, but the practise of sharing the Provider instance. A new Instance for each request would never lead to this problem!!!

 
New Post
5/21/2008 11:46 PM
 

r_honey wrote

So, the AddDept method's Activation Record in memory would be same for both Threads. When the control returns to 1st user's thread, "info" reference in this activation record has effectively been over-written by 2nd user's data.

I was still thinking over as to why hasn't anyone encountered this Threading issue, leading to Data Corruption. And then, this came to my mind...

In languages like C# & VB.NET, which inherently support Recursion, each call to a method generates a new Activation Record for that method, effectively duplicating the method's local variables. The above problem would have arisen only when the local variable, info was being shared across Threads. But as each Activation Record for the method call will have its own local copy of info, this problem would not arise.

But if the instance of the Data Provider being created has its member variables (like reference to a transaction) that need to be different for different invocations of the Provider object by the Controller, then the above problem would again arise, because the Provider Object has some internal state that needs to be different for different invocations to it. In that case, the shared Instance() method should return a new instance of the Provider, instead of returning the shared copy.

 
Previous
 
Next
HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...Threading issues with DNN Provider architectureThreading issues with DNN Provider architecture


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out