Project

General

Profile

rollback_patch.txt

Constantin Asofiei, 06/09/2020 06:13 AM

Download (5.42 KB)

 
1
### Eclipse Workspace Patch 1.0
2
#P p2j4011a
3
Index: src/com/goldencode/p2j/persist/orm/BaseRecord.java
4
===================================================================
5
--- src/com/goldencode/p2j/persist/orm/BaseRecord.java	(revision 2332)
6
+++ src/com/goldencode/p2j/persist/orm/BaseRecord.java	(working copy)
7
@@ -682,22 +682,24 @@
8
     *          the full transaction block, and a positive number represents that number of nested
9
     *          sub-transactions.
10
     */
11
-   void rollback(Session session, int txLevel)
12
+   List<Runnable> rollback(Session session, int txLevel)
13
    {
14
       if (changeSet == null)
15
       {
16
          // this should not happen; we should only get this call if this record had at least one change
17
          // during an application transaction
18
-         return;
19
+         return null;
20
       }
21
       
22
-      changeSet.rollback(session, txLevel, this);
23
+      List<Runnable> actions = changeSet.rollback(session, txLevel, this);
24
       
25
       // discard the change set after rollback of the full transaction
26
       if (txLevel == 0)
27
       {
28
          changeSet = null;
29
       }
30
+      
31
+      return actions;
32
    }
33
    
34
    /**
35
Index: src/com/goldencode/p2j/persist/orm/ChangeSet.java
36
===================================================================
37
--- src/com/goldencode/p2j/persist/orm/ChangeSet.java	(revision 2332)
38
+++ src/com/goldencode/p2j/persist/orm/ChangeSet.java	(working copy)
39
@@ -66,6 +66,8 @@
40
 
41
 import java.util.*;
42
 import java.util.logging.*;
43
+
44
+import com.goldencode.p2j.persist.PersistenceException;
45
 import com.goldencode.p2j.util.*;
46
 
47
 /**
48
@@ -215,12 +217,12 @@
49
     * @param   dmo
50
     *          Record to be rolled back.
51
     */
52
-   void rollback(Session session, int txLevel, BaseRecord dmo)
53
+   List<Runnable> rollback(Session session, int txLevel, BaseRecord dmo)
54
    {
55
       if (changes == null || txLevel < outer || txLevel > inner)
56
       {
57
          // TODO: what is appropriate here?
58
-         return;
59
+         return null;
60
       }
61
       
62
       // live data to be updated by rollback
63
@@ -287,9 +289,13 @@
64
          for (int j = touched.nextSetBit(0); j >= 0; j = touched.nextSetBit(j + 1))
65
          {
66
             data[j] = baseline[j];
67
+            
68
+            dmo.setDatum(j, data[j]);
69
          }
70
       }
71
       
72
+      List<Runnable> rollbackActions = new LinkedList<>();
73
+      
74
       // roll back state changed by events
75
       for (int i = inner; i >= txLevel; i--)
76
       {
77
@@ -307,12 +313,33 @@
78
          
79
          if ((next & Event.UPDATE) == Event.UPDATE)
80
          {
81
-            // TODO: is this needed?
82
+            rollbackActions.add(() -> 
83
+            {
84
+               try
85
+               {
86
+                  session.save(dmo, false);
87
+               }
88
+               catch (PersistenceException e)
89
+               {
90
+                  throw new RuntimeException(e);
91
+               }
92
+            });
93
          }
94
          
95
          if ((next & Event.INSERT) == Event.INSERT)
96
          {
97
             dmo.updateState(DmoState.NEW, true);
98
+            rollbackActions.add(() ->
99
+            {
100
+               try
101
+               {
102
+                  session.save(dmo, false);
103
+               }
104
+               catch (PersistenceException e)
105
+               {
106
+                  throw new RuntimeException(e);
107
+               }
108
+            });
109
          }
110
          
111
          // handle create and/or delete
112
@@ -335,6 +362,8 @@
113
          // events have been rolled back; clear the flags
114
          events[i] = Event.NONE;
115
       }
116
+      
117
+      return rollbackActions.isEmpty() ? null : rollbackActions;
118
    }
119
    
120
    /**
121
Index: src/com/goldencode/p2j/persist/orm/SavepointManager.java
122
===================================================================
123
--- src/com/goldencode/p2j/persist/orm/SavepointManager.java	(revision 2332)
124
+++ src/com/goldencode/p2j/persist/orm/SavepointManager.java	(working copy)
125
@@ -292,7 +292,7 @@
126
             if (session != null)
127
             {
128
                // mark all tracked, undoable records as STALE
129
-               endingBlock.rollback(session);
130
+               List<Runnable> rollbackActions = endingBlock.rollback(session);
131
                
132
                // the active block is now the parent block (if any); roll up all tracked
133
                // information for the ending block to it
134
@@ -307,6 +307,14 @@
135
                {
136
                   session.rollbackSavepoint(endingBlock.savepoint);
137
                }
138
+               
139
+               if (rollbackActions != null)
140
+               {
141
+                  for (Runnable action : rollbackActions)
142
+                  {
143
+                     action.run();
144
+                  }
145
+               }
146
             }
147
          }
148
       }
149
@@ -633,11 +641,16 @@
150
       /**
151
        * Roll back changes to undoable records and activate redoable actions for NO-UNDO records.
152
        */
153
-      void rollback(Session session)
154
+      List<Runnable> rollback(Session session)
155
       {
156
+         List<Runnable> actions = new LinkedList<>();
157
          for (BaseRecord dmo : changed)
158
          {
159
-            dmo.rollback(session, txLevel);
160
+            List<Runnable> dmoActions = dmo.rollback(session, txLevel);
161
+            if (dmoActions != null)
162
+            {
163
+               actions.addAll(dmoActions);
164
+            }
165
          }
166
          
167
          int redoSize = redoable.size();
168
@@ -650,6 +663,8 @@
169
                redoable.get(i).setActive();
170
             }
171
          }
172
+         
173
+         return actions.isEmpty() ? null : actions;
174
       }
175
       
176
       /**