How does TransactionScope guarantee data integrity across multiple databases?

Can someone tell me the principle of how TransactionScope ensures data integrity across multiple databases? I assume that he first sends commands to the databases and then waits for the databases to respond before sending them a message to use the command sent earlier. However, when execution is interrupted when these messages are sent, we can still get the database that applied this command, and the other to it. Can anyone shed some light on this?

Edit:

I assume that I am asking if I can rely on TransactionScope to guarantee data integrity when writing to multiple databases in the event of a power outage or a sudden outage.

Thanks Bas

Example:

using(var scope=new TransactionScope()) { using (var context = new FirstEntities()) { context.AddToSomethingSet(new Something()); context.SaveChanges(); } using (var context = new SecondEntities()) { context.AddToSomethingElseSet(new SomethingElse()); context.SaveChanges(); } scope.Complete(); } 
+4
source share
2 answers

With distributed transactions, it can actually happen that databases become inconsistent. You said:

At some point, both databases should be prompted to apply their changes. Let's say, after turning off the power, saying that the first db is applied, then the databases are not synchronized. Or am I missing something?

Not. I think this is called the generals problem. It is impossible to prevent. The windows of failure, however, are rather small.

+2
source

He promotes it to the Distributed Transaction Coordinator (msdtc) if he discovers several databases that use each area as part of a two-phase commit. Each area votes for commit and, therefore, we get ACID properties, but distribute to the databases. It can also be integrated with TxF, TxR. You should be able to use it as you describe.

Two databases are sequential, since they are connected to transactions with distributed COM +, operating under the control of MTC, database transactions.

If one database votes to commit (for example, by executing (: TransactionScope) .Commit ()), she tells the DTC what it votes to commit. When all the databases have done this, they have a list of changes. Since database transactions are no longer blocked or in conflict with other transactions (for example, using a fairness algorithm that prevents one transaction), all operations for each database are in the transaction log. If the system loses electricity, if it is not yet completed for one database, but it has for another, it was recorded in the transaction log, which all resources voted for commit, so there are no logical consequences that should happen with commit. Therefore, the next time a database that cannot fix downloads completes these transactions that remained in this undefined state.

+6
source

Source: https://habr.com/ru/post/1303445/


All Articles