I've been troubleshooting this thing for hours.
When you use the SEO Settings page to regenerate urls, it fires two async tasks. One for Products, and one for Categories.
Mine is crashing when it hits a category with 4 products and all 4 products have the exact same product name. SQL throws a unique index error on ac_CatalogUrls.
Here's how the code runs:
Hits Copy 1 of product
Sees the url does not already exist
Goes ahead and generates standard url (without a -P123)
Sets the product url
Saves the product, which cascade saves the catalogObject child to create the ac_CatalogUrl record
Hits copy 2 of product
Sees the url does not already exist
That's the problem. This code isn't seeing the url was created and saved by the first copy of the product. And the initial url would be exactly the same because the product name is exactly the same.
Code:
/// <inheritdoc />
public CatalogUrl LoadCatalogUrl(string url)
{
url = url.ToLowerInvariant();
ICriteria criteria = NHibernateHelper.CreateCriteria<CatalogUrl>();
criteria.Add(Restrictions.Eq("Store", AbleContext.Current.Store));
criteria.Add(Restrictions.Eq("LoweredUrl", url.ToLowerInvariant()));
IList<CatalogUrl> urls = criteria.List<CatalogUrl>();
if (urls.Count > 0) return urls[0];
return null;
}
This routine keeps returning null for all 4 products. So the calling routines think they're supposed to use the standard url, which is (by then) already created.
So when the CommitTransaction() is fired, SQL tries to write all the records it was told to save but hits a duplicate unique key and throws the exception.
What I've found as the cause is the use of BeginTransaction() inside the AWAIT task routines. If I comment out the Begin/Commit Transaction lines in BOTH the Products routine and Categories routine, the whole thing takes an eternity to run but it correctly identifies duplicated urls and adjusts the urls accordingly.
I can't find a lot about it, but it seems like we've got a problem with using SQL Transactions inside AWAIT async routines. At least in my case, it's not working.
I set up a short test scenario in a default AC904 install, just to see if I could reproduce the problem using the sample data. Just added 4 of the exact same product name to a category. I don't get an error there. That tells me it has something to do with the size of the data of my catalog, 700+ categories and 9,000+ products.
Why, I don't know. But I do know taking out the begin/commit in the routines solves the issue I am having.