开发者

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

I would like to disable and then rebuild a unique index insid开发者_JAVA百科e 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.


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'));
}


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:

  • Reorganizing and Rebuilding Indexes

What are you trying to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜