NserviceBus-NHibernate.StaleStateException:批处理更新从更新返回了意外的行数;实际行数:-1;预期:1

时间:2018-10-16 12:03:16

标签: nservicebus

我正在使用以下版本

NServiceBus:5.2.14

NServiceBus.Host:6.0.0

NServiceBus.NHibernate:6.2.7

我正在将NSB5与Nibernate持久性和sql服务器传输一起使用。在将队列移到其他数据库之前,我的服务曾经可以正常工作,我在另一个数据库中创建了所有队列。审核和错误队列保留在同一数据库上(其他NSB主机使用相同的队列)。我从其他端点删除了所有旧的订阅条目,重新启动了服务并检查,但订阅条目未出现在新表中,并在下面提供了错误日志。有什么解决办法?

 018-10-16 07:11:03.2878 Info NServiceBus.Unicast.Transport.TransportReceiver Failed to process message
    NHibernate.StaleStateException: Batch update returned unexpected row count from update; actual row count: -1; expected: 1
       at NHibernate.AdoNet.Expectations.VerifyOutcomeBatched(Int32 expectedRowCount, Int32 rowCount)
       at NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand ps)
       at NHibernate.AdoNet.AbstractBatcher.ExecuteBatchWithTiming(IDbCommand ps)
       at NHibernate.AdoNet.AbstractBatcher.ExecuteBatch()
       at NHibernate.Engine.ActionQueue.ExecuteActions()
       at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
       at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
       at NHibernate.Impl.SessionImpl.Flush()
       at NHibernate.Transaction.AdoTransaction.Commit()
       at NServiceBus.Unicast.Subscriptions.NHibernate.SubscriptionPersister.Subscribe(Address address, IEnumerable`1 messageTypes) in C:\BuildAgent\work\5135de308b2f3016\src\NServiceBus.NHibernate\Subscriptions\SubscriptionPersister.cs:line 41
       at NServiceBus.SubscriptionReceiverBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Subscriptions\MessageDrivenSubscriptions\SubscriptionReceiverBehavior.cs:line 76
       at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
       at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 95
       at NServiceBus.UnitOfWorkBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\UnitOfWork\UnitOfWorkBehavior.cs:line 42
       at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
       at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 95
       at NServiceBus.Persistence.NHibernate.OpenSqlConnectionBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\5135de308b2f3016\src\NServiceBus.NHibernate\SharedSession\OpenSqlConnectionBehavior.cs:line 43
       at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
       at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 95
       at NServiceBus.ChildContainerBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\ChildContainerBehavior.cs:line 17
       at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
       at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 95
       at NServiceBus.ProcessingStatisticsBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Monitoring\ProcessingStatisticsBehavior.cs:line 23
       at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
       at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 95
       at NServiceBus.AuditBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Audit\AuditBehavior.cs:line 20
       at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
       at NServiceBus.BehaviorChain`1.Invoke() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 52
       at NServiceBus.Pipeline.PipelineExecutor.Execute[T](BehaviorChain`1 pipelineAction, T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 129
       at NServiceBus.Unicast.Transport.TransportReceiver.OnTransportMessageReceived(TransportMessage msg) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 413
       at NServiceBus.Unicast.Transport.TransportReceiver.ProcessMessage(TransportMessage message) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 344
       at NServiceBus.Unicast.Transport.TransportReceiver.TryProcess(TransportMessage message) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 230
       at NServiceBus.Transports.SQLServer.AmbientTransactionReceiveStrategy.TryReceiveFrom(TableBasedQueue queue) in C:\BuildAgent\work\888dd942b44bad5a\src\NServiceBus.SqlServer\AmbientTransactionReceiveStrategy.cs:line 57

1 个答案:

答案 0 :(得分:0)

下面从NserviceBus讨论中获得答案 https://discuss.particular.net/t/nhibernate-stalestateexception-batch-update-returned-unexpected-row-count-from-update-actual-row-count-1-expected-1/817

代码

public class CustomDriverConnectionProvider : 
NHibernate.Connection.DriverConnectionProvider
{
public override System.Data.IDbConnection GetConnection()
{
    var conn = base.GetConnection();
    DisableNoCount(conn);
    return conn;
}

private void DisableNoCount(System.Data.IDbConnection connection)
{
    using (var command = connection.CreateCommand())
    {
        command.CommandText = "SET NOCOUNT OFF";
        command.ExecuteNonQuery();
    }
}
}

public void Customize(BusConfiguration configuration)
    {
  //Setup NHibernate
        var hibernateConfig = new NHibernate.Cfg.Configuration();
        hibernateConfig.DataBaseIntegration(x =>
        {
            x.ConnectionString = GetPersistenceConnectionString();
            x.Dialect<MsSql2008Dialect>();
            x.ConnectionProvider<CustomDriverConnectionProvider>();
        });
        hibernateConfig.SetProperty("default_schema", GetPersistenceSchema());

        //Wire NHibernate
        configuration.UsePersistence<NHibernatePersistence>()
            .RegisterManagedSessionInTheContainer()
            .DisableSchemaUpdate()
   }