Marek Stój
Marek Stój

Reputation: 4175

Sync framework - how to enable and disable unique index inside a sync transaction?

I would like to disable and then rebuild a unique index inside a sync transaction. Can it be done?

I have multiple rows in a table that need to be changed but the values being updated violate a unique index. However, after all rows are changed, the constraint would be satisfied, so I want to disable this index until the end of the synchronization process.

Upvotes: 1

Views: 536

Answers (2)

Marek Stój
Marek Stój

Reputation: 4175

In case anyone would be interested in a solution to a similar problem - one can take control over the transaction used during sync by attaching to some events of the sync provider:

private void DbCacheServerSyncProvider_ApplyingChanges(object sender, ApplyingChangesEventArgs e)
{
  if (e.Transaction == null)
  {
    e.Transaction = e.Connection.BeginTransaction(IsolationLevel.ReadCommitted);

    DisableConstraints(e.Connection, e.Transaction);
  }
}

private void DbCacheServerSyncProvider_ChangesApplied(object sender, ChangesAppliedEventArgs e)
{
  if (e.Transaction != null)
  {
    EnableConstraints(e.Connection, e.Transaction);

    e.Transaction.Commit();
    e.Transaction.Dispose();
  }
}

private void DbCacheServerSyncProvider_ApplyChangeFailed(object sender, ApplyChangeFailedEventArgs e)
{
  if (e.Transaction != null)
  {
    e.Transaction.Rollback();
    e.Transaction.Dispose();
  }

  throw new InternalException("Server-side conflict has occurred during synchronization. Conflict details:\r\n" + SyncUtils.CreateChangeFailedDetails(e).Trim('\r', '\n'));
}

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300749

If you are not changing the Unique index, there is no need to do that.

With SQL Server 2008, you can perform an online reorganise or a rebuild:

What are you trying to do?

Upvotes: 0

Related Questions