Qui-Gon Jinn
Qui-Gon Jinn
CC#
Created by Qui-Gon Jinn on 5/21/2023 in #help
✅ do i use BeginTransaction() correctly?
@TeBeCringe yes i am using blazor server app and i use dependency injection, however i often had concurrency issues ("the 'second operation started on the same dbcontext exception' ") when i only had one dbcontext instance at the time. so this is why i am adding a dbcontextfactory in the program.cs like this:
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
AddDbContextFactory creates a factory with lifetime singleton by default, unless it is defined otherwise. then i am injecting the factory in my razor component like this:
@inject IDbContextFactory<ApplicationDbContext> dbContextFactory;
@inject IDbContextFactory<ApplicationDbContext> dbContextFactory;
with this i can create a dbcontext wherever i want without worrying about dbcontext concurrency because they work independently of each other. i also have components where i create a dbcontext from the factory and use it in the whole component. regarding my actual issue i think i should just use "IsolationLevel.Serializable" this way i avoid that an offer can be deleted while the transaction is running. i also could have something like : (offer exists) ? context.SaveChanges() : return; instead of having the nullcheck inside the ifstatement
8 replies
CC#
Created by Qui-Gon Jinn on 5/21/2023 in #help
✅ do i use BeginTransaction() correctly?
i tested my code multiple times but unfortunately the pendingoffer still gets added to the database, even if the offer has been deleted. I could use "IsolationLevel.Serializable" but that does the opposite what i want to do. this isolationlevel prevents the offer to be deleted until the transaction is completed. but i dont want to lock it, i want others to delete the offer anytime they want and if the offer is deleted right after the offer null check i want the transaction detect it and dont add the data to the database
8 replies