简体   繁体   中英

How to use a single Sql Server Transaction between multiple page requests in Asp.Net MVC

I need to store records in multiple dependent tables of database, updated partially in each HTTP Request to Asp.net MVC Action.

For this I need to use a Sql Server Transaction across multiple Page Requests that are hitting Asp.Net MVC Action Methods.

How can this be done?

You can store the data in objects while it's still not ready to be saved.

Some examples:

1) Store data in objects in session .

2) You can create a Manager class (with a singleton object living through the entire lifetime of your application) which will allow you to gather data across multiple requests. If you use IOC container, just register is as a singleton.

3) You can store the data client-side in cookies or local storage and push all of the data with another request when it's ready.

And then when you have a complete data set you can just save it to the database the normal way.

EDIT

If you insist on using transactions you can use the same principle here simply keep you transaction or a database context in the session instead of the data. However, I would advise against performing long running transactions. This may cause nasty locks on your tables. Yes, you can play with isolation levels, but I believe there are betters solutions.

How about a message bus eg MassTransit with a Saga instead? When the saga is complete you can simply transfer its data to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM