Getting NHibernate.NonUniqueResultException

495 views
Skip to first unread message

Alexandros Karatzas

unread,
Dec 7, 2015, 3:31:16 AM12/7/15
to Particular Software

Hi,

I have been geting the below exception lately when a specific type of event is published.

NHibernate.NonUniqueResultException: query did not return a unique result: 2

We are on NServiceBus 5.2.9, NServiceBus NHibernate 6.2.5 and NServiceBus.RabbitMQ 3.0.1

I am attaching the log file from today as it has happened several times.

Kind regards,

Alex Karatzas
Log.log

Andreas Ohlund

unread,
Dec 7, 2015, 6:33:13 AM12/7/15
to particula...@googlegroups.com
Seems like you're getting duplicate saga instances in the database. Can you verify that you have the [Unique] attribute properly set on the correlation property for your saga(s)



 Cheers,

Andreas

--
You received this message because you are subscribed to the Google Groups "Particular Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.
To view this discussion on the web visit https://groups.google.com/d/msgid/particularsoftware/5b5b61de-93c5-42a0-b224-59ab28276cb6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexandros Karatzas

unread,
Dec 7, 2015, 8:34:15 AM12/7/15
to Particular Software
Hi Andreas,

the Unique attribute is correctly set, at least on the only saga that subscribes to the InstrumentModified event which causes the error. The only thing I am noticing is that there are 2 different saga instances that are alive and are subscribing to that event.  Would NServiceBus for any reason assume that there is only one saga instance of the same type subscribed to this event? 

There is a saga mapping for this event based on one of the fields on the message and both saga instances match the field value so both would need to be invoked. 

Kind regards,

Alex Karatzas


Andreas Ohlund

unread,
Dec 7, 2015, 8:56:27 AM12/7/15
to particula...@googlegroups.com
Can you double check that you have a unique index in the database for the property? (there is a known issue with NHibernate not creating the index if the attribute was added after the table was created)

Should look like this: (In this case correlating the `ShippingPolicy` saga on OrderId so you need to translate that to your saga)

ALTER TABLE [dbo].[ShippingPolicy] ADD UNIQUE NONCLUSTERED 
(
[OrderId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]



--
You received this message because you are subscribed to the Google Groups "Particular Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.

Alexandros Karatzas

unread,
Dec 7, 2015, 8:59:27 AM12/7/15
to Particular Software
Hi Andreas,

there is a unique index in the database for the property. Besides the 2 instances that have subscribed to the event are different instances. The field that is used to map the event subscription to the saga is not the unique property but some other property that can be found in multiple saga instances. Is that not a valid use case?

Kind regards,

Alex Karatzas 


Andreas Ohlund

unread,
Dec 7, 2015, 9:15:58 AM12/7/15
to particula...@googlegroups.com
Now I'm confused, if there is a unique index it seems impossible that NHibernate can blow with 

2015-12-07 07:30:29.8296 INFO Failed to process message 
query did not return a unique result: 2 NHibernate.NonUniqueResultException: query did not return a unique result: 2
   at NHibernate.Impl.AbstractQueryImpl.UniqueElement(IList list)
   at NHibernate.Impl.CriteriaImpl.UniqueResult[T]()
   at NServiceBus.SagaPersisters.NHibernate.SagaPersister.<>c__DisplayClass4_0`1.<NServiceBus.Saga.ISagaPersister.Get>

Could this be another saga?

--
You received this message because you are subscribed to the Google Groups "Particular Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.

Alexandros Karatzas

unread,
Dec 7, 2015, 9:21:38 AM12/7/15
to Particular Software
All I can tell you is that the class InstrumentModified implements IEvent and is only handled by one specific type of saga but it is possible that multiple instances are interested in that event so the saga mapping is not on the unique key property.

However, there is a property decorated with the Unique attribute and I can also see the unique key in the database. Not sure why it would matter though as the unique key is not used for saga mapping when subscribing to the event.

Kind regards,

Alex Karatzas


Andreas Ohlund

unread,
Dec 7, 2015, 10:17:41 AM12/7/15
to particula...@googlegroups.com
Now I understand!

Sagas in NServiceBus only supports a single instance of the same type loaded by the same message. 

Here's a longer explanation on why this is:


To get this working you would have to redesign your solution slightly:

1. Have an event handler react to the event
2. In the event handler figure out which sagas that should be invoked (by some kind of query)
3. And send a bunch of commands each one targeted at a given saga instance

We have some future plans to give you a more powerful version of custom saga finders that would allow you to return multiple instances. But as you can see here they currently must return a single instance:


Does this make any sense?

Cheers,

Andreas

--
You received this message because you are subscribed to the Google Groups "Particular Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.

Alexandros Karatzas

unread,
Dec 7, 2015, 11:48:13 AM12/7/15
to Particular Software
Hi Andreas,

I understand what you mean. I will have to do a fair amount of refactoring as I have used events in several places to notify multiple instances of the same saga.

Thanks for your help

Kind regards,

Alex Karatzas




Andreas Ohlund

unread,
Dec 8, 2015, 4:06:05 AM12/8/15
to particula...@googlegroups.com
Hi Alexandros!

I understand the situation you're in and I'm sorry that our API lead you into this dead end :(

While it won't help you right now the API in v6 would have stopped you earlier since we don't allow you to map more than one saga property. If you need to correlate on more than one saga property you would have to use a custom saga finder and since they can only return a single instance it would have been impossible to get your scenario to work.

Regards,

Andreas



--
You received this message because you are subscribed to the Google Groups "Particular Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.
Reply all
Reply to author
Forward
0 new messages