ServiceStack.OrmLite MultiThread Error "Field definition identifier not found"

While running some tests with OrmLite, I ran into some multithreading issue. In some cases, using my repo from different threads, I found a random "concurrency" error on the FieldDefinitionMap field.

Reproducing the error is quite simple, just run 10 threads with massive read / write operations to get a random error: 'Field definition identifier not found'

It is very important to reproduce the error for using the update / insert functions for the first time for new threads.

The error arises from the function:

public virtual void SetParameterValues<T>(IDbCommand dbCmd, object obj)

I was able to solve the problem by locking the FieldDefinitionMap, as shown below:

namespace ServiceStack.OrmLite
public class ModelDefinition  

public Dictionary<string, FieldDefinition> FieldDefinitionMap
{
get
{
    lock (this) // Locking the get works without problems
    {
        if (fieldDefinitionMap == null)
        {
            fieldDefinitionMap = new Dictionary<string, FieldDefinition>();
            foreach (var fieldDef in FieldDefinitionsArray)
            {
                fieldDefinitionMap[fieldDef.FieldName] = fieldDef;
            }
        }
        return fieldDefinitionMap; 
    }
}
}

By the way, I do not know if there is a better solution.

+4

All Articles