Autor Beitrag
erfahrener Neuling
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 05.12.18 15:19 
Moin Leute,

in eine Funktion, welche bestimmte Firebird-Datenbank-Inserts macht, wollte ich nun ein TransactionScope implementieren, um alle Inserts evtl. wieder rückgängig machen zu können. Die Funktion hat im wesentlichen folgenden Aufbau:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
private void DoSomething()
{
    new Thread((ThreadStart)delegate
    {
        using (var transactionScope = new System.Transactions.TransactionScope())
        {
            while (/*Bedingung erfüllt*/)
            {
                // Mache die Inserts
                // ...
                try
                {
                    Invoke((MethodInvoker)delegate
                    {
                        RefreshDisplayControls();
                    });
                }
                catch (Exception)
                {
                    break;
                }
            }

            if (/*Bedingung erfüllt*/)
                transactionScope.Complete();
        }
    }).Start();
}


Meiner Erwartung nach dürften die Inserts nur commited werden, wenn transactionScope.Complete() aufgerufen wird. Tatsächlich werden die Datensätze aber immer angelegt.
Getestet habe ich auch schon:
- für alle Inserts nur eine offene Connection benutzen (aktuell mache ich immer eine neue)
- das ganze nicht in einem neuen Thread laufen zu lassen
==> brachte das selbe Ergebnis

Hat jemand den rettenden Tipp für mich oder erkennt den Fehler bei der Umsetzung?

Danke im voraus

Julian


Moderiert von user profile iconChristian S.: Topic aus Basistechnologien verschoben am Mi 05.12.2018 um 15:22
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 05.12.18 17:05 
Laut Firebird and Entity Framework Transaction Rollback mußt du "Enlist=true" im Connection-String angeben!
Und du solltest (bzw. mußt) auf jeden Fall für alle Inserts die gleiche Connection benutzen.

Für diesen Beitrag haben gedankt: erfahrener Neuling
erfahrener Neuling Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 05.12.18 17:18 
Danke für die schnelle Antwort! Beides ist richtig...