This project has moved. For the latest updates, please go here.
3
Vote

Threading bug in ProviderFactory

description

Hi,

The code below is not thread safe. The first thread will see _providers is null and create the new Dictionary. A second thread can then see that _providers is not null and return the empty dictionary before the first calls ProvideFactory.Register<>() to initalise it.

This results in an exception

EntityFramework.BulkInsert.Exceptions.BulkInsertProviderNotFoundException: BulkInsertProvider not found for 'System.Data.SqlClient.SqlConnection.
To register new provider use EntityFramework.BulkInsert.ProviderFactory.Register() method'
private static Dictionary<string, Func<IEfBulkInsertProvider>> Providers
{
    get
    {
        if (ProviderFactory._providers == null)
        {
            ProviderFactory._providers = new Dictionary<string, Func<IEfBulkInsertProvider>>();
            ProviderFactory.Register<EfSqlBulkInsertProviderWithMappedDataReader>("System.Data.SqlClient.SqlConnection");
        }
        return ProviderFactory._providers;
    }
}
Regards
Dave

comments