@@ -31,88 +31,93 @@ public abstract partial class CoreProvider
31
31
using ( connection = this . CreateConnection ( ) )
32
32
{
33
33
await connection . OpenAsync ( ) ;
34
+ await this . InterceptAsync ( new ConnectionOpenArgs ( context , connection ) ) ;
34
35
35
36
// Create a transaction
36
- applyTransaction = connection . BeginTransaction ( ) ;
37
-
38
- context . SyncStage = SyncStage . DatabaseChangesApplying ;
37
+ using ( applyTransaction = connection . BeginTransaction ( ) )
38
+ {
39
39
40
- // Launch any interceptor if available
41
- await this . InterceptAsync ( new ConnectionOpenArgs ( context , connection , applyTransaction ) ) ;
42
- await this . InterceptAsync ( new DatabaseChangesApplyingArgs ( context , connection , applyTransaction ) ) ;
40
+ await this . InterceptAsync ( new TransactionOpenArgs ( context , connection , applyTransaction ) ) ;
43
41
44
- // Disable check constraints
45
- if ( this . Options . DisableConstraintsOnApplyChanges )
46
- changeApplicationAction = this . DisableConstraints ( context , message . Schema , connection , applyTransaction , message . FromScope ) ;
42
+ context . SyncStage = SyncStage . DatabaseChangesApplying ;
47
43
48
- // -----------------------------------------------------
49
- // 0) Check if we are in a reinit mode
50
- // -----------------------------------------------------
51
- if ( context . SyncWay == SyncWay . Download && context . SyncType != SyncType . Normal )
52
- {
53
- changeApplicationAction = this . ResetInternal ( context , message . Schema , connection , applyTransaction , message . FromScope ) ;
44
+ // Launch any interceptor if available
45
+ await this . InterceptAsync ( new DatabaseChangesApplyingArgs ( context , connection , applyTransaction ) ) ;
54
46
55
- // Rollback
56
- if ( changeApplicationAction == ChangeApplicationAction . Rollback )
57
- throw new SyncException ( "Rollback during reset tables" , context . SyncStage , SyncExceptionType . Rollback ) ;
58
- }
47
+ // Disable check constraints
48
+ if ( this . Options . DisableConstraintsOnApplyChanges )
49
+ changeApplicationAction = this . DisableConstraints ( context , message . Schema , connection , applyTransaction , message . FromScope ) ;
59
50
60
- // -----------------------------------------------------
61
- // 1) Applying deletes. Do not apply deletes if we are in a new database
62
- // -----------------------------------------------------
63
- if ( ! message . FromScope . IsNewScope )
64
- {
65
- // for delete we must go from Up to Down
66
- foreach ( var table in message . Schema . Tables . Reverse ( ) )
51
+ // -----------------------------------------------------
52
+ // 0) Check if we are in a reinit mode
53
+ // -----------------------------------------------------
54
+ if ( context . SyncWay == SyncWay . Download && context . SyncType != SyncType . Normal )
67
55
{
68
- changeApplicationAction = await this . ApplyChangesInternalAsync ( table , context , message , connection ,
69
- applyTransaction , DmRowState . Deleted , changesApplied ) ;
56
+ changeApplicationAction = this . ResetInternal ( context , message . Schema , connection , applyTransaction , message . FromScope ) ;
57
+
58
+ // Rollback
59
+ if ( changeApplicationAction == ChangeApplicationAction . Rollback )
60
+ throw new SyncException ( "Rollback during reset tables" , context . SyncStage , SyncExceptionType . Rollback ) ;
70
61
}
71
62
72
- // Rollback
73
- if ( changeApplicationAction == ChangeApplicationAction . Rollback )
63
+ // -----------------------------------------------------
64
+ // 1) Applying deletes. Do not apply deletes if we are in a new database
65
+ // -----------------------------------------------------
66
+ if ( ! message . FromScope . IsNewScope )
74
67
{
75
- RaiseRollbackException ( context , "Rollback during applying deletes" ) ;
76
- }
77
- }
68
+ // for delete we must go from Up to Down
69
+ foreach ( var table in message . Schema . Tables . Reverse ( ) )
70
+ {
71
+ changeApplicationAction = await this . ApplyChangesInternalAsync ( table , context , message , connection ,
72
+ applyTransaction , DmRowState . Deleted , changesApplied ) ;
73
+ }
78
74
79
- // -----------------------------------------------------
80
- // 2) Applying Inserts and Updates. Apply in table order
81
- // -----------------------------------------------------
82
- foreach ( var table in message . Schema . Tables )
83
- {
84
- changeApplicationAction = await this . ApplyChangesInternalAsync ( table , context , message , connection ,
85
- applyTransaction , DmRowState . Added , changesApplied ) ;
75
+ // Rollback
76
+ if ( changeApplicationAction == ChangeApplicationAction . Rollback )
77
+ {
78
+ RaiseRollbackException ( context , "Rollback during applying deletes" ) ;
79
+ }
80
+ }
86
81
87
- // Rollback
88
- if ( changeApplicationAction == ChangeApplicationAction . Rollback )
82
+ // -----------------------------------------------------
83
+ // 2) Applying Inserts and Updates. Apply in table order
84
+ // -----------------------------------------------------
85
+ foreach ( var table in message . Schema . Tables )
89
86
{
90
- RaiseRollbackException ( context , "Rollback during applying inserts" ) ;
91
- }
87
+ changeApplicationAction = await this . ApplyChangesInternalAsync ( table , context , message , connection ,
88
+ applyTransaction , DmRowState . Added , changesApplied ) ;
92
89
93
- changeApplicationAction = await this . ApplyChangesInternalAsync ( table , context , message , connection ,
94
- applyTransaction , DmRowState . Modified , changesApplied ) ;
90
+ // Rollback
91
+ if ( changeApplicationAction == ChangeApplicationAction . Rollback )
92
+ {
93
+ RaiseRollbackException ( context , "Rollback during applying inserts" ) ;
94
+ }
95
95
96
- // Rollback
97
- if ( changeApplicationAction == ChangeApplicationAction . Rollback )
98
- {
99
- RaiseRollbackException ( context , "Rollback during applying updates" ) ;
96
+ changeApplicationAction = await this . ApplyChangesInternalAsync ( table , context , message , connection ,
97
+ applyTransaction , DmRowState . Modified , changesApplied ) ;
98
+
99
+ // Rollback
100
+ if ( changeApplicationAction == ChangeApplicationAction . Rollback )
101
+ {
102
+ RaiseRollbackException ( context , "Rollback during applying updates" ) ;
103
+ }
100
104
}
101
- }
102
105
103
106
104
- // Progress & Interceptor
105
- context . SyncStage = SyncStage . DatabaseChangesApplied ;
106
- var databaseChangesAppliedArgs = new DatabaseChangesAppliedArgs ( context , connection , applyTransaction ) ;
107
- this . ReportProgress ( context , databaseChangesAppliedArgs , connection , applyTransaction ) ;
108
- await this . InterceptAsync ( databaseChangesAppliedArgs ) ;
107
+ // Progress & Interceptor
108
+ context . SyncStage = SyncStage . DatabaseChangesApplied ;
109
+ var databaseChangesAppliedArgs = new DatabaseChangesAppliedArgs ( context , connection , applyTransaction ) ;
110
+ this . ReportProgress ( context , databaseChangesAppliedArgs , connection , applyTransaction ) ;
111
+ await this . InterceptAsync ( databaseChangesAppliedArgs ) ;
109
112
110
- // Re enable check constraints
111
- if ( this . Options . DisableConstraintsOnApplyChanges )
112
- changeApplicationAction = this . EnableConstraints ( context , message . Schema , connection , applyTransaction , message . FromScope ) ;
113
+ // Re enable check constraints
114
+ if ( this . Options . DisableConstraintsOnApplyChanges )
115
+ changeApplicationAction = this . EnableConstraints ( context , message . Schema , connection , applyTransaction , message . FromScope ) ;
113
116
114
- applyTransaction . Commit ( ) ;
117
+ await this . InterceptAsync ( new TransactionCommitArgs ( context , connection , applyTransaction ) ) ;
118
+ applyTransaction . Commit ( ) ;
115
119
120
+ }
116
121
117
122
return ( context , changesApplied ) ;
118
123
}
@@ -383,9 +388,8 @@ internal async Task<ChangeApplicationAction> ApplyChangesInternalAsync(
383
388
/// </summary>
384
389
internal async Task < ( ChangeApplicationAction , int , DmRow ) > HandleConflictAsync ( DbSyncAdapter syncAdapter , SyncContext context , SyncConflict conflict , ConflictResolutionPolicy policy , ScopeInfo scope , long fromScopeLocalTimeStamp , DbConnection connection , DbTransaction transaction )
385
390
{
386
- DmRow finalRow = null ;
387
- var conflictApplyAction = ApplyAction . Continue ;
388
-
391
+ DmRow finalRow ;
392
+ ApplyAction conflictApplyAction ;
389
393
( conflictApplyAction , finalRow ) = await this . GetConflictActionAsync ( context , conflict , policy , connection , transaction ) ;
390
394
391
395
// Default behavior and an error occured
0 commit comments