Microsoft.Synchronization.Data.DbSyncException:无法为表执行命令“ InsertCommand”

时间:2019-02-25 08:22:47

标签: c# sql-server synchronization microsoft-sync-framework

我的项目自7个月前开始运行。 现在我想使用Microsoft Sync Framework将数据库与服务器同步 但是在设置之后开始同步时,几分钟后上传时就会出现此错误。

有人知道错在哪里吗?

Windows:10 x64

SqlServer:2016

应用程序:任何CPU(首选32位)

syncOrchestrator.State:正在上传

Microsoft.Synchronization.Data.DbSyncException: Failed to execute the command 'InsertCommand' for table 'tbl_Loading'; the transaction was rolled back. Ensure that the command syntax is correct. ---> System.Data.SqlClient.SqlException: A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.) ---> System.ComponentModel.Win32Exception: The semaphore timeout period has expired
       --- End of inner exception stack trace ---
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
       at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
       at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
       at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
       at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
       at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value)
       at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at Microsoft.Synchronization.Data.DbChangeHandler.ExecuteCommandReturnRowcount(IDbCommand command, Boolean& found)
       at Microsoft.Synchronization.Data.DbChangeHandler.ExecuteChangeApplicationCommandBase(IDbCommand command, SyncRowMetadata rowMetadata, String commandName)
       --- End of inner exception stack trace ---
       at Microsoft.Synchronization.Data.ChangeHandlerBase.CheckZombieTransaction(String commandName, String table, Exception ex)
       at Microsoft.Synchronization.Data.ChangeHandlerBase.ExecuteChangeApplicationCommandInternal(IDbCommand command, SyncRowMetadata rowMetadata, String commandName)
       at Microsoft.Synchronization.Data.DbChangeHandler.ApplyInsert()
       at Microsoft.Synchronization.Data.ChangeHandlerBase.ApplyChange(FailedDeleteDelegate_type failedDeleteDelegate)
       at Microsoft.Synchronization.Data.RelationalSyncProvider.ApplyChangesInternal(DbSyncScopeMetadata scopeMetadata, IDbTransaction transaction, FailedDeleteDelegate_type failedDeleteDelegate, DataSet dataSet, ChangeApplicationType applyType)
       at Microsoft.Synchronization.Data.RelationalSyncProvider.ApplyChanges(DbSyncScopeMetadata scopeMetadata, IDbTransaction applyTransaction, DataSet dataSet, DbSyncSession DbSyncSession, Boolean commitTransaction, FailedDeleteDelegate_type failedDeleteDelegate, String batchFileName, ChangeApplicationAction& action)
       at Microsoft.Synchronization.Data.RelationalSyncProvider.SingleTransactionApplyChangesAdapter.Apply(DataSet dataSet, Boolean commitTransaction, FailedDeleteDelegate_type failedDeleteDelegate, String batchFileName, ChangeApplicationAction& action)
       at Microsoft.Synchronization.Data.RelationalSyncProvider.ApplyChanges(DbSyncScopeMetadata scopeMetadata, DataSet dataSet, DbSyncSession dbSyncSession, Boolean commitTransaction)
       at Microsoft.Synchronization.Data.RelationalSyncProvider.ProcessChangeBatch(ConflictResolutionPolicy resolutionPolicy, ChangeBatch sourceChanges, Object changeDataRetriever, SyncCallbacks syncCallbacks, SyncSessionStatistics sessionStatistics)
       at Microsoft.Synchronization.KnowledgeProviderProxy.ProcessChangeBatch(CONFLICT_RESOLUTION_POLICY resolutionPolicy, ISyncChangeBatch pSourceChangeManager, Object pUnkDataRetriever, ISyncCallback pCallback, _SYNC_SESSION_STATISTICS& pSyncSessionStatistics)
       at Microsoft.Synchronization.CoreInterop.ISyncSession.Start(CONFLICT_RESOLUTION_POLICY resolutionPolicy, _SYNC_SESSION_STATISTICS& pSyncSessionStatistics)
       at Microsoft.Synchronization.KnowledgeSyncOrchestrator.DoOneWaySyncHelper(SyncIdFormatGroup sourceIdFormats, SyncIdFormatGroup destinationIdFormats, KnowledgeSyncProviderConfiguration destinationConfiguration, SyncCallbacks DestinationCallbacks, ISyncProvider sourceProxy, ISyncProvider destinationProxy, ChangeDataAdapter callbackChangeDataAdapter, SyncDataConverter conflictDataConverter, Int32& changesApplied, Int32& changesFailed)
       at Microsoft.Synchronization.KnowledgeSyncOrchestrator.DoOneWayKnowledgeSync(SyncDataConverter sourceConverter, SyncDataConverter destinationConverter, SyncProvider sourceProvider, SyncProvider destinationProvider, Int32& changesApplied, Int32& changesFailed)
       at Microsoft.Synchronization.KnowledgeSyncOrchestrator.Synchronize()
       at Microsoft.Synchronization.SyncOrchestrator.Synchronize()
       at TruckerWinApp.Tools.Sync..()

,错误来自以下几行:

Line : 
Void Start(CONFLICT_RESOLUTION_POLICY, _SYNC_SESSION_STATISTICS ByRef)
(line : 0 col : 0)
--------------------------------------------
Void DoOneWaySyncHelper(Microsoft.Synchronization.SyncIdFormatGroup, Microsoft.Synchronization.SyncIdFormatGroup, Microsoft.Synchronization.KnowledgeSyncProviderConfiguration, Microsoft.Synchronization.SyncCallbacks, ISyncProvider, ISyncProvider, Microsoft.Synchronization.ChangeDataAdapter, Microsoft.Synchronization.SyncDataConverter, Int32 ByRef, Int32 ByRef)
(line : 0 col : 0)
--------------------------------------------
Void DoOneWayKnowledgeSync(Microsoft.Synchronization.SyncDataConverter, Microsoft.Synchronization.SyncDataConverter, Microsoft.Synchronization.SyncProvider, Microsoft.Synchronization.SyncProvider, Int32 ByRef, Int32 ByRef)
(line : 0 col : 0)
--------------------------------------------
Microsoft.Synchronization.SyncOperationStatistics Synchronize()
(line : 0 col : 0)
--------------------------------------------
Microsoft.Synchronization.SyncOperationStatistics Synchronize()
(line : 0 col : 0)
--------------------------------------------
Void ()
(line : 0 col : 0)
--------------------------------------------

这是我的代码:

try
{
    string scopeName = "scopeAll";
    var serverConn = new SqlConnection(sv);
    var clientConn = new SqlConnection(dbConStr);

    try
    {
        List<string> tablesList = ClassLibrary.ToolsGeneral.getDbTables(new DbWinAppEntities());

        //provision server
        var serverProvision = new SqlSyncScopeProvisioning(serverConn);
        serverProvision.CommandTimeout = 0;
        if (!serverProvision.ScopeExists(scopeName))
        {
            var serverScopeDesc = new DbSyncScopeDescription(scopeName);
            foreach (var item in tablesList)
                serverScopeDesc.Tables.Add(SqlSyncDescriptionBuilder.GetDescriptionForTable(item, serverConn));

            serverProvision.PopulateFromScopeDescription(serverScopeDesc);
            serverProvision.Apply();
        }

        //provision client
        var clientProvision = new SqlSyncScopeProvisioning(clientConn);
        clientProvision.CommandTimeout = 0;
        if (!clientProvision.ScopeExists(scopeName))
        {
            var scopeDesc = SqlSyncDescriptionBuilder.GetDescriptionForScope(scopeName, serverConn);
            clientProvision.PopulateFromScopeDescription(scopeDesc);
            clientProvision.Apply();
        }
    }
    catch (Exception eee)
    {

    }

    var localProvider = new SqlSyncProvider(scopeName, clientConn);
    var remoteProvider = new SqlSyncProvider(scopeName, serverConn);
    localProvider.CommandTimeout = 0;
    remoteProvider.CommandTimeout = 0;

    var syncOrchestrator = new SyncOrchestrator();
    syncOrchestrator.LocalProvider = localProvider;
    syncOrchestrator.RemoteProvider = remoteProvider;
    syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;
    syncOrchestrator.SessionProgress += (ss, ee) =>
    {
       //some code
    };
    syncOrchestrator.StateChanged += (ss, ee) =>
    {
         //some code
    };

    SyncOperationStatistics syncStat = syncOrchestrator.Synchronize();

}catch{}

0 个答案:

没有答案