Hi all
Using LLBLGEN 3.5, Adapter, MSSQL (2005-2012).
What is the best way to achieve a high insert rate using LLBLGEN?
I sometimes have to add like 10k rows. SQL Server does not parallelize inserts AFAIK, and I want to speed that up using multithreading / connection pooling / other technique.. ?
Currently, i'm doing a Parallel.ForEach:
Parallel.ForEach(items, currentItem => ItemWrapper.Add(LinkDatabaseConnection.Get(currentItem.LinkId, true), Convert(currentItem)));
Where items is a collection of items, which gets Converted to an LLBLGEN Entity.
Let's simplify this for this forum thread:
SqlConnectionStringBuilder cs = new SqlConnectionStringBuilder();
cs.DataSource = "test";
EntityCollection<ItemEntity> items = new EntityCollection<ItemEntity>();
for (i = 0; i<10000;i++)
{
items.Add(new ItemEntity(i));
}
Parallel.ForEach(items, currentItem => ItemWrapper.Add(cs, currentItem));
ItemWrapper.Add is:
public static void Add(SqlConnectionStringBuilder connectionString, ItemEntity item)
{
try
{
Database.AddEntity(connectionString, item, false);
}
catch (ORMQueryExecutionException ex)
{
if (ex.InnerException is SqlException)
{
Logger.Trace("Exception Number: ");
switch ((ex.InnerException as SqlException).Errors[0].Number)
{
case 2627:
Database.UpdateEntitiesDirectly(connectionString, item, new RelationPredicateBucket(ItemFields.ItemId == item.ItemId));
break;
default:
Logger.Trace("SQL Error Number: {0}", (ex.InnerException as SqlException).Errors[0].Number);
throw;
}
}
else
{
throw;
}
}
}
which ultimately does (Database.AddEntity<T>):
public static T AddEntity<T>(SqlConnectionStringBuilder connectionString, T entity, bool refetch)
where T : EntityBase2, IEntity2, new()
{
try
{
using (var adapter = Connection.GetAdapter(connectionString))
{
adapter.SaveEntity(entity, refetch);
return entity;
}
}
catch (Exception e)
{
Logger.ErrorException("Error while saving entity", e);
throw;
}
}
in this code, would KeepConnectionOpen on the DataAccessAdapter help in any way?
I suppose not, as the adapter object is disposed after every SaveEntity anyway? (Which I have to do because DAA is not thread-safe)
Is there a better way to do it?
Let me know if you need more information.
Thanks for your thoughts
Michel