File: SqlTrackingWorkflowInstance.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (873 lines) | stat: -rw-r--r-- 32,051 bytes parent folder | download | duplicates (7)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel.Design.Serialization;

using System.Workflow.Runtime;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime.Hosting;

namespace System.Workflow.Runtime.Tracking
{
    [Obsolete("The System.Workflow.* types are deprecated.  Instead, please use the new types from System.Activities.*")]
    public class SqlTrackingWorkflowInstance
    {
        #region Private Members

        private delegate void LoadFromReader(SqlDataReader reader, object parameter);

        private static int _deadlock = 1205;
        private static short _retries = 5;
        private string _connectionString = null;
        private bool _autoRefresh = false;

        DateTime _currDT = DateTime.UtcNow,
            _actMinDT = SqlDateTime.MinValue.Value,
            _userMinDT = SqlDateTime.MinValue.Value,
            _instMinDT = SqlDateTime.MinValue.Value,
            _childMinDT = SqlDateTime.MinValue.Value,
            _changesMinDT = SqlDateTime.MinValue.Value,
            _invMinDT = SqlDateTime.MinValue.Value;

        long _internalId = -1;

        Guid _id;
        DateTime _initialized;
        Guid _invoker = Guid.Empty;
        WorkflowStatus _status;
        Type _workflowType = null;
        bool _changed = false;

        List<ActivityTrackingRecord> _activityEvents = new List<ActivityTrackingRecord>();
        List<UserTrackingRecord> _userEvents = new List<UserTrackingRecord>();
        List<WorkflowTrackingRecord> _workflowEvents = new List<WorkflowTrackingRecord>();
        List<SqlTrackingWorkflowInstance> _invoked = new List<SqlTrackingWorkflowInstance>();

        Activity _def = null;

        #endregion Private Members

        #region Constructors

        private SqlTrackingWorkflowInstance() { }

        internal SqlTrackingWorkflowInstance(string connectionString)
        {
            if (null == connectionString)
                throw new ArgumentNullException("connectionString");
            _connectionString = connectionString;
        }

        #endregion Constructors

        #region Properties

        public bool AutoRefresh
        {
            get { return _autoRefresh; }
            set { _autoRefresh = value; }
        }

        public Guid WorkflowInstanceId
        {
            get { return _id; }
            set { _id = value; }
        }

        public long WorkflowInstanceInternalId
        {
            get { return _internalId; }
            set { _internalId = value; }
        }

        public DateTime Initialized
        {
            get { return _initialized; }
            set { _initialized = value; }
        }

        public Guid InvokingWorkflowInstanceId
        {
            get { return _invoker; }
            set { _invoker = value; }
        }

        public WorkflowStatus Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public Type WorkflowType
        {
            get { return _workflowType; }
            set { _workflowType = value; }
        }

        public bool WorkflowDefinitionUpdated
        {
            get
            {
                if (_autoRefresh)
                    Refresh();

                LoadDef();
                return _changed;
            }
        }

        public IList<ActivityTrackingRecord> ActivityEvents
        {
            get
            {
                if (_autoRefresh)
                    Refresh();

                LoadActivityEvents();
                return _activityEvents;
            }
        }

        public IList<UserTrackingRecord> UserEvents
        {
            get
            {
                if (_autoRefresh)
                    Refresh();

                LoadUserEvents();
                return _userEvents;
            }
        }

        public IList<WorkflowTrackingRecord> WorkflowEvents
        {
            get
            {
                if (_autoRefresh)
                    Refresh();

                LoadWorkflowEvents();
                return _workflowEvents;
            }
        }

        public Activity WorkflowDefinition
        {
            get
            {
                if (_autoRefresh)
                    Refresh();

                LoadDef();
                return _def;
            }
        }

        public IList<SqlTrackingWorkflowInstance> InvokedWorkflows
        {
            get
            {
                if (_autoRefresh)
                    Refresh();
                LoadInvokedWorkflows();
                return _invoked;
            }
        }
        #endregion Properties

        public void Refresh()
        {
            _currDT = DateTime.UtcNow;
        }

        private void LoadActivityEvents()
        {
            SqlCommand cmd = CreateInternalIdDateTimeCommand("[dbo].[GetActivityEventsWithDetails]", _actMinDT);

            ExecuteRetried(cmd, LoadActivityEventsFromReader);
        }

        private void LoadActivityEventsFromReader(SqlDataReader reader, object parameter)
        {
            if (null == reader)
                throw new ArgumentNullException("reader");

            //
            // There should always be 4 recordsets in this reader!
            //

            Dictionary<long, ActivityTrackingRecord> activities = new Dictionary<long, ActivityTrackingRecord>();
            //
            // Build a dictionary of activity records so that we can match 
            // annotation and artifact records from subsequent recordsets
            DateTime tmpMin = SqlDateTime.MinValue.Value;
            while (reader.Read())
            {
                string qId = reader.GetString(0);
                ActivityExecutionStatus status = (ActivityExecutionStatus)reader[1];
                DateTime dt = reader.GetDateTime(2);
                Guid context = reader.GetGuid(3), parentContext = reader.GetGuid(4);
                int order = reader.GetInt32(5);

                if (reader.IsDBNull(6) || reader.IsDBNull(7))
                    throw new InvalidOperationException(String.Format(System.Globalization.CultureInfo.CurrentCulture, ExecutionStringManager.SqlTrackingTypeNotFound, qId));

                Type type = Type.GetType(reader.GetString(6) + ", " + reader.GetString(7), true, false);
                long eventId = reader.GetInt64(8);

                DateTime dbDt = reader.GetDateTime(9);

                activities.Add(eventId, new ActivityTrackingRecord(type, qId, context, parentContext, status, dt, order, null));
                if (dbDt > tmpMin)
                    tmpMin = dbDt;
            }

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidActivityEventReader);

            //
            // If we have annotations on the event itself, add them
            while (reader.Read())
            {
                long eventId = reader.GetInt64(0);
                string annotation = null;

                if (!reader.IsDBNull(1))
                    annotation = reader.GetString(1);

                ActivityTrackingRecord activity = null;
                if (activities.TryGetValue(eventId, out activity))
                {
                    if (null != activity)
                        activity.Annotations.Add(annotation);
                }
            }

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidActivityEventReader);

            //
            // Build a dictionary of artifact records so that we can match 
            // annotation records from subsequent recordsets
            BinaryFormatter formatter = new BinaryFormatter();
            Dictionary<long, TrackingDataItem> artifacts = new Dictionary<long, TrackingDataItem>();
            while (reader.Read())
            {
                long eventId = reader.GetInt64(0);
                long artId = reader.GetInt64(1);
                string name = reader.GetString(2), strData = null;
                object data = null;
                //
                // These may both be null
                if (!reader.IsDBNull(3))
                    strData = reader.GetString(3);

                if (!reader.IsDBNull(4))
                    data = formatter.Deserialize(new MemoryStream((Byte[])reader[4]));

                TrackingDataItem item = new TrackingDataItem();
                item.FieldName = name;
                if (null != data)
                    item.Data = data;
                else
                    item.Data = strData;

                artifacts.Add(artId, item);
                //
                // Find the event to which this artifact belongs and add it to the record
                ActivityTrackingRecord activity = null;
                if (activities.TryGetValue(eventId, out activity))
                {
                    if (null != activity)
                        activity.Body.Add(item);
                }
            }

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidActivityEventReader);

            //
            // If we have annotations add them to the appropriate artifact
            while (reader.Read())
            {
                long artId = reader.GetInt64(0);
                string annotation = null;

                if (!reader.IsDBNull(1))
                    annotation = reader.GetString(1);
                //
                // Find the right artifact and give it the annotation
                TrackingDataItem item = null;
                if (artifacts.TryGetValue(artId, out item))
                {
                    if (null != item)
                        item.Annotations.Add(annotation);
                }
            }

            _activityEvents.AddRange(activities.Values);

            //
            // Set the min value to the most recent event that we got with this query
            // Don't overwrite the previous min if nothing came back for this query
            if (tmpMin > SqlDateTime.MinValue.Value)
                _actMinDT = tmpMin;
            return;
        }

        private void LoadUserEvents()
        {
            SqlCommand cmd = CreateInternalIdDateTimeCommand("[dbo].[GetUserEventsWithDetails]", _userMinDT);

            ExecuteRetried(cmd, LoadUserEventsFromReader);
        }

        private void LoadUserEventsFromReader(SqlDataReader reader, object parameter)
        {
            if (null == reader)
                throw new ArgumentNullException("reader");

            //
            // There should always be 4 recordsets in this reader!
            //

            BinaryFormatter formatter = new BinaryFormatter();
            Dictionary<long, UserTrackingRecord> userEvents = new Dictionary<long, UserTrackingRecord>();
            //
            // Build a dictionary of activity records so that we can match 
            // annotation and artifact records from subsequent recordsets
            DateTime tmpMin = SqlDateTime.MinValue.Value;
            while (reader.Read())
            {
                string qId = reader.GetString(0);
                DateTime dt = reader.GetDateTime(1);
                Guid context = reader.GetGuid(2), parentContext = reader.GetGuid(3);
                int order = reader.GetInt32(4);
                string key = null;
                if (!reader.IsDBNull(5))
                    key = reader.GetString(5);
                //
                // Get the user data from the serialized column if we can
                // Try the string column if serialized column is null
                // If both are null the user data was null originally
                object userData = null;
                if (!reader.IsDBNull(7))
                    userData = formatter.Deserialize(new MemoryStream((Byte[])reader[7]));
                else if (!reader.IsDBNull(6))
                    userData = reader.GetString(6);

                if (reader.IsDBNull(8) || reader.IsDBNull(9))
                    throw new InvalidOperationException(String.Format(System.Globalization.CultureInfo.CurrentCulture, ExecutionStringManager.SqlTrackingTypeNotFound, qId));

                Type type = Type.GetType(reader.GetString(8) + ", " + reader.GetString(9), true, false);
                long eventId = reader.GetInt64(10);

                DateTime dbDt = reader.GetDateTime(11);

                userEvents.Add(eventId, new UserTrackingRecord(type, qId, context, parentContext, dt, order, key, userData));

                if (dbDt > tmpMin)
                    tmpMin = dbDt;
            }

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidUserEventReader);

            //
            // If we have annotations on the event itself, add them
            while (reader.Read())
            {
                long eventId = reader.GetInt64(0);
                string annotation = null;

                if (!reader.IsDBNull(1))
                    annotation = reader.GetString(1);

                UserTrackingRecord user = null;
                if (userEvents.TryGetValue(eventId, out user))
                {
                    if (null != user)
                        user.Annotations.Add(annotation);
                }
            }

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidUserEventReader);

            //
            // Build a dictionary of artifact records so that we can match 
            // annotation records from subsequent recordsets
            Dictionary<long, TrackingDataItem> artifacts = new Dictionary<long, TrackingDataItem>();
            while (reader.Read())
            {
                long eventId = reader.GetInt64(0);
                long artId = reader.GetInt64(1);
                string name = reader.GetString(2), strData = null;
                object data = null;
                //
                // These may both be null
                if (!reader.IsDBNull(3))
                    strData = reader.GetString(3);

                if (!reader.IsDBNull(4))
                    data = formatter.Deserialize(new MemoryStream((Byte[])reader[4]));

                TrackingDataItem item = new TrackingDataItem();
                item.FieldName = name;
                if (null != data)
                    item.Data = data;
                else
                    item.Data = strData;

                artifacts.Add(artId, item);
                //
                // Find the event to which this artifact belongs and add it to the record
                UserTrackingRecord user = null;
                if (userEvents.TryGetValue(eventId, out user))
                {
                    if (null != user)
                        user.Body.Add(item);
                }
            }

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidUserEventReader);

            //
            // If we have annotations add them to the appropriate artifact
            while (reader.Read())
            {
                long artId = reader.GetInt64(0);
                string annotation = null;

                if (!reader.IsDBNull(1))
                    annotation = reader.GetString(1);
                //
                // Find the right artifact and give it the annotation
                TrackingDataItem item = null;
                if (artifacts.TryGetValue(artId, out item))
                {
                    if (null != item)
                        item.Annotations.Add(annotation);
                }
            }

            _userEvents.AddRange(userEvents.Values);
            //
            // Set the min dt to query for next time to the most recent event we got for this query.
            // Don't overwrite the previous min if nothing came back for this query
            if (tmpMin > SqlDateTime.MinValue.Value)
                _userMinDT = tmpMin;
            return;
        }

        private void LoadWorkflowEvents()
        {
            SqlCommand cmd = CreateInternalIdDateTimeCommand("[dbo].[GetWorkflowInstanceEventsWithDetails]", _instMinDT);

            ExecuteRetried(cmd, LoadWorkflowEventsFromReader);
        }

        private void LoadWorkflowEventsFromReader(SqlDataReader reader, object parameter)
        {
            if (null == reader)
                throw new ArgumentNullException("reader");

            //
            // There should always be 2 recordsets in this reader!
            //

            DateTime tmpMin = SqlDateTime.MinValue.Value;

            Dictionary<long, WorkflowTrackingRecord> inst = new Dictionary<long, WorkflowTrackingRecord>();
            while (reader.Read())
            {
                TrackingWorkflowEvent evt = (TrackingWorkflowEvent)reader[0];
                DateTime dt = reader.GetDateTime(1);
                int order = reader.GetInt32(2);

                object tmp = null;
                EventArgs args = null;
                if (!reader.IsDBNull(3))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    tmp = formatter.Deserialize(new MemoryStream((Byte[])reader[3]));
                    if (tmp is EventArgs)
                        args = (EventArgs)tmp;
                }
                long eventId = reader.GetInt64(4);

                DateTime dbDt = reader.GetDateTime(5);

                inst.Add(eventId, new WorkflowTrackingRecord(evt, dt, order, args));

                if (dbDt > tmpMin)
                    tmpMin = dbDt;
            }

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidWorkflowInstanceEventReader);

            //
            // Add any annotations
            while (reader.Read())
            {
                long eventId = reader.GetInt64(0);
                string annotation = null;

                if (!reader.IsDBNull(1))
                    annotation = reader.GetString(1);

                WorkflowTrackingRecord rec = null;
                if (inst.TryGetValue(eventId, out rec))
                {
                    if (null != rec)
                        rec.Annotations.Add(annotation);
                }
            }

            if (!reader.IsClosed)
                reader.Close();
            //
            // Check if we have any WorkflowChange events in this list
            // If so pull back the change actions and reconstruct the args property
            foreach (KeyValuePair<long, WorkflowTrackingRecord> kvp in inst)
            {
                WorkflowTrackingRecord rec = kvp.Value;
                if (TrackingWorkflowEvent.Changed != rec.TrackingWorkflowEvent)
                    continue;

                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "[dbo].[GetWorkflowChangeEventArgs]";
                cmd.Parameters.Add(new SqlParameter("@WorkflowInstanceInternalId", _internalId));
                cmd.Parameters.Add(new SqlParameter("@BeginDateTime", SqlDateTime.MinValue.Value));
                cmd.Parameters.Add(new SqlParameter("@WorkflowInstanceEventId", kvp.Key));

                ExecuteRetried(cmd, LoadWorkflowChangeEventArgsFromReader, rec);
            }

            _workflowEvents.AddRange(inst.Values);
            //
            // set the min for the next query to the most recent event from this query
            // Don't overwrite the previous min if nothing came back for this query
            if (tmpMin > SqlDateTime.MinValue.Value)
                _instMinDT = tmpMin;
        }

        private void LoadWorkflowChangeEventArgsFromReader(SqlDataReader reader, object parameter)
        {
            if (null == reader)
                throw new ArgumentNullException("reader");

            if (null == parameter)
                throw new ArgumentNullException("parameter");

            WorkflowTrackingRecord record = parameter as WorkflowTrackingRecord;

            if (null == record)
                throw new ArgumentException(ExecutionStringManager.InvalidWorkflowChangeEventArgsParameter, "parameter");

            if (!reader.Read())
                throw new ArgumentException(ExecutionStringManager.InvalidWorkflowChangeEventArgsReader);

            StringReader sr = new StringReader(reader.GetString(0));

            //Deserialize the xoml and set the root activity
            Activity def = null;
            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
            DesignerSerializationManager serializationManager = new DesignerSerializationManager();
            IList errors = null;
            try
            {
                using (serializationManager.CreateSession())
                {
                    using (XmlReader xmlReader = XmlReader.Create(sr))
                    {
                        def = serializer.Deserialize(serializationManager, xmlReader) as Activity;
                        errors = serializationManager.Errors;
                    }
                }
            }
            finally
            {
                sr.Close();
            }

            if ((null == def) || ((null != errors) && (errors.Count > 0)))
                throw new WorkflowMarkupSerializationException(ExecutionStringManager.WorkflowMarkupDeserializationError);

            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidWorkflowChangeEventArgsReader);
            //
            // There is a result set that we don't care about for this scenario, skip it
            if (!reader.NextResult())
                throw new ArgumentException(ExecutionStringManager.InvalidWorkflowChangeEventArgsReader);

            List<WorkflowChangeAction> actions = new List<WorkflowChangeAction>();
            DateTime currDT = DateTime.MinValue;
            int currEventOrder = -1;
            int currOrder = -1;

            while (reader.Read())
            {
                DateTime dt = reader.GetDateTime(1);
                int eventOrder = reader.GetInt32(2);
                int order = reader.GetInt32(3);
                //
                // Build temp lists as we read the results but
                // only save the last set of change actions
                if (dt > currDT && eventOrder > currEventOrder)
                {
                    currEventOrder = eventOrder;
                    currOrder = order;
                    currDT = dt;
                    actions = new List<WorkflowChangeAction>();
                }

                using (sr = new StringReader(reader.GetString(0)))
                {
                    using (serializationManager.CreateSession())
                    {
                        using (XmlReader xmlReader = XmlReader.Create(sr))
                        {
                            ActivityChangeAction aAction = serializer.Deserialize(serializationManager, xmlReader) as ActivityChangeAction;

                            errors = serializationManager.Errors;
                            if (null == aAction)
                                throw new WorkflowMarkupSerializationException(ExecutionStringManager.WorkflowMarkupDeserializationError);

                            actions.Add(aAction);
                            aAction.ApplyTo(def);
                        }
                    }
                }
            }

            record.EventArgs = new TrackingWorkflowChangedEventArgs(actions, def);
        }

        private void LoadDef()
        {
            SqlCommand cmd = null;
            //
            // If we don't have the definition load it
            if (null == _def)
            {
                cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "[dbo].[GetWorkflowDefinition]";
                cmd.Parameters.Add(new SqlParameter("@WorkflowInstanceInternalId", _internalId));

                ExecuteRetried(cmd, LoadDefFromReader);
            }
            //
            // Now check for changes.  If we find changes apply them to the definition
            cmd = CreateInternalIdDateTimeCommand("[dbo].[GetWorkflowChanges]", _changesMinDT);

            ExecuteRetried(cmd, LoadChangesFromReader);
        }

        private void LoadDefFromReader(SqlDataReader reader, object parameter)
        {
            if (null == reader)
                throw new ArgumentNullException("reader");

            if (!reader.Read())
                throw new ArgumentException(ExecutionStringManager.InvalidDefinitionReader);

            StringReader sr = new StringReader(reader.GetString(0));

            //Deserialize the xoml and set the root activity
            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
            DesignerSerializationManager serializationManager = new DesignerSerializationManager();
            IList errors = null;
            try
            {
                using (serializationManager.CreateSession())
                {
                    using (XmlReader xmlReader = XmlReader.Create(sr))
                    {
                        _def = serializer.Deserialize(serializationManager, xmlReader) as Activity;
                        errors = serializationManager.Errors;
                    }
                }
            }
            finally
            {
                sr.Close();
            }

            if ((null == _def) || ((null != errors) && (errors.Count > 0)))
                throw new WorkflowMarkupSerializationException(ExecutionStringManager.WorkflowMarkupDeserializationError);
        }

        private void LoadChangesFromReader(SqlDataReader reader, object parameter)
        {
            if (!reader.Read())
                return;
            //
            // Reset the min to the most recent change event
            DateTime tmpDT = _changesMinDT;
            if (!reader.IsDBNull(0))
                tmpDT = reader.GetDateTime(0);

            if (reader.NextResult())
            {
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                DesignerSerializationManager serializationManager = new DesignerSerializationManager();
                while (reader.Read())
                {
                    IList errors = null;
                    using (StringReader sr = new StringReader(reader.GetString(0)))
                    {
                        using (serializationManager.CreateSession())
                        {
                            using (XmlReader xmlReader = XmlReader.Create(sr))
                            {
                                ActivityChangeAction aAction = serializer.Deserialize(serializationManager, xmlReader) as ActivityChangeAction;

                                errors = serializationManager.Errors;
                                if (null != aAction)
                                    aAction.ApplyTo(_def);
                                else
                                    throw new WorkflowMarkupSerializationException(ExecutionStringManager.WorkflowMarkupDeserializationError);
                            }
                        }
                    }
                }
            }

            if (tmpDT > _changesMinDT)
            {
                _changed = true;
                _changesMinDT = tmpDT;
            }
        }

        private void LoadInvokedWorkflows()
        {
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "[dbo].[GetInvokedWorkflows]";
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter param = new SqlParameter("@WorkflowInstanceId", SqlDbType.UniqueIdentifier);
            param.Value = _id;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@BeginDateTime", SqlDbType.DateTime);
            param.Value = _invMinDT;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@EndDateTime", SqlDbType.DateTime);
            param.Value = _currDT;
            cmd.Parameters.Add(param);

            ExecuteRetried(cmd, LoadInvokedWorkflowsFromReader);
        }

        private void LoadInvokedWorkflowsFromReader(SqlDataReader reader, object parameter)
        {
            if (null == reader)
                throw new ArgumentNullException("reader");

            DateTime tmpMin = SqlDateTime.MinValue.Value;
            while (reader.Read())
            {
                SqlTrackingWorkflowInstance inst = SqlTrackingQuery.BuildInstance(reader, _connectionString);

                if (inst.Initialized > tmpMin)
                    tmpMin = inst.Initialized;

                _invoked.Add(inst);
            }
            //
            // set the min for the next query to the most recently invoked instance from this query
            // Don't overwrite the previous min if nothing came back for this query
            if (tmpMin > SqlDateTime.MinValue.Value)
                _invMinDT = tmpMin;
        }

        private void ExecuteRetried(SqlCommand cmd, LoadFromReader loader)
        {
            ExecuteRetried(cmd, loader, null);
        }

        private void ExecuteRetried(SqlCommand cmd, LoadFromReader loader, object loadFromReaderParam)
        {
            SqlDataReader reader = null;
            short count = 0;
            while (true)
            {
                try
                {
                    using (SqlConnection conn = new SqlConnection(_connectionString))
                    {
                        cmd.Connection = conn;
                        cmd.Connection.Open();
                        reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        loader(reader, loadFromReaderParam);
                        break;
                    }
                }
                catch (SqlException se)
                {
                    //
                    // Retry if we deadlocked.
                    // All other exceptions bubble
                    if ((_deadlock == se.Number) && (++count < _retries))
                        continue;

                    throw;
                }
                finally
                {
                    if ((null != reader) && (!reader.IsClosed))
                        reader.Close();
                }
            }
        }

        private SqlCommand CreateInternalIdDateTimeCommand(string commandText, DateTime minDT)
        {
            return CreateInternalIdDateTimeCommand(commandText, minDT, _currDT);
        }

        private SqlCommand CreateInternalIdDateTimeCommand(string commandText, DateTime minDT, DateTime maxDT)
        {
            if (null == commandText)
                throw new ArgumentNullException("commandText");

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = commandText;
            //
            // Add parameter values for GetActivityEvents
            SqlParameter param = new SqlParameter("@WorkflowInstanceInternalId", SqlDbType.BigInt);
            param.Value = _internalId;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@BeginDateTime", SqlDbType.DateTime);
            param.Value = minDT;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@EndDateTime", SqlDbType.DateTime);
            param.Value = maxDT;
            cmd.Parameters.Add(param);

            return cmd;
        }

    }
}