How to use transactions in nopcommerce?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hello
I am beginner and I want to really know about how transactions are handles in nopcommerce?(commit & rollback) I checked source code but I am not able to get idea about it.

Can anyone help me please?

Thanks
6 years ago
For the most part, nopCommerce doesn't do any explicit transaction handling. It uses the default Entity Framework transaction behavior which will wrap SaveChanges() in a transaction (using isolation level READ COMMITTED). https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

iDbContext.ExecuteSqlCommand does support wrapping SQL commands in a transaction. https://github.com/nopSolutions/nopCommerce/blob/b5f7884ce5be583e0d5e4deaa300b9176eca42fa/src/Libraries/Nop.Data/IDbContext.cs#L49 You can use this to wrap your own custom SQL in a transaction.
6 years ago
Thanks for the reply.

But suppose for some case I need to use explicit transaction can you provide some some implantation how can we achieve this?

Thank You.
6 years ago
mayur.lohite wrote:

But suppose for some case I need to use explicit transaction can you provide some some implantation how can we achieve this?


NopCommerce is using EF, so basically your question is: How to use transaction in EF.

Here is a simple example, you can convert your services to following way.


using (var context = new MyContext())
{
    using (var dbContextTransaction = context.Database.BeginTransaction())
    {
        try
        {
             // your code
            context.SaveChanges();

            dbContextTransaction.Commit();
        }
        catch (Exception)
        {
            dbContextTransaction.Rollback();
            
        }
    }
}
6 years ago
Hello Divyang

Thank you for reply. Sorry for my silly question I am beginner. Above your code is tightly couple. How can we implement using Dependency Injection.

Please can you provide me how service will look like? Just pseudo code will be enough

Thank you.
6 years ago
I have an idea for this. please visit my blog and the link is in below:
http://www.cnblogs.com/YUTOUYUWEI/p/7589119.html
If someone want to get these code, my email account is [email protected].
I'm in China so i can't use the google email except for some kind of VPN.
6 years ago
johntseng wrote:
For the most part, nopCommerce doesn't do any explicit transaction handling. It uses the default Entity Framework transaction behavior which will wrap SaveChanges() in a transaction (using isolation level READ COMMITTED). https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

iDbContext.ExecuteSqlCommand does support wrapping SQL commands in a transaction. https://github.com/nopSolutions/nopCommerce/blob/b5f7884ce5be583e0d5e4deaa300b9176eca42fa/src/Libraries/Nop.Data/IDbContext.cs#L49 You can use this to wrap your own custom SQL in a transaction.


So that, nopcommerce does not guarantee Consistency attribute in ACID concept ?.

For example, I look at the source code of Nop.Web.Controllers.VendorController.ApplyVendorSubmit. (version 3.90)

There are multiple services in the same function without Transaction wrapping




Line 151: _vendorService.InsertVendor(vendor);
Line xxx: ...
Line 154: _urlRecordService.SaveSlug(vendor, seName, 0);
Line xxx: ...
Line 160: _customerService.UpdateCustomer(_workContext.CurrentCustomer);
Line 161:
Line 162: //update picture seo file name
Line 163: UpdatePictureSeoNames(vendor);



In above code,
If <Line 151>, <Line 154> sucess. But <Line 160> fail. Then, "Vendor" record was inserted into Database but "Customer" record have not been updated yet ( I expected that Customer record should be updated too).
6 years ago
I have just looked around, and found this :
https://nopcommerce.codeplex.com/workitem/10605

I seem that nopcommerce have not supported transaction guarantee yet.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.