Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create safe per-web-request transaction in .net's MVC?

I need to be all my entire application transactional in its every web request it processes.

I need the transaction to start and, if there was no exceptions in controllers, commit it. Otherwise, rollback.

So far, I have the following implementation:

  • First, I create transaction on controller as a dependency.
  • Then, I do my controller/service/repositories/other stuff work.
  • And finally, the main abstract controller class executes its OnActionExecuted method, where I either commit it or I don't.

Here goes the list of technologies that I use:

  • MVC 4
  • Ninject
  • Automapper
  • Service pattern

Now, what I want to know is what about the deadlocks? What will happen when two web requests will be simultaneously processed, and besides they will acquire the right to work with two repositories (which are linked to the theirs DataContext instances), and which means, two tables in the database?

For example: one request at first wants to read table Table1 and then Table2, in meantime the other request wants to work with Table2 and then with Table1.

What should I do?

like image 270
AgentFire Avatar asked Mar 06 '13 15:03

AgentFire


People also ask

How to handle concurrency in asp net mvc?

Test concurrency handlingChange a field in the first browser tab and click Save. The browser shows the Index page with the changed value. Click Save again. The value you entered in the second browser tab is saved along with the original value of the data you changed in the first browser.


1 Answers

you can handle start and stop of transaction for call to DB please check this

How to prevent EntityFramework deadlock when concurrently running these two statements

hope this help

like image 80
zaheer ahmad Avatar answered Sep 20 '22 14:09

zaheer ahmad