How about publishing a new event when entity1 is created and a subscriber
can take care of the creation of entity2? That way that subscriber can check
if entity2 already exists before creating it and throw the message away if
not needed. Then, if concurrency is still an issue after doing this
(doubtful) simply move the subscriber that creates entity2 behind another
queue, make that endpoint single-threaded, and you're good to go.

My personal rule of thumb is not having more than one interaction with an
external system (database, web service, etc...) per message consumer - each
can fail and retry independently if you stick to that rule. It also helps
untangle dependency issues such as this and make them obvious fairly
quickly.

Hope that helps,
Matt

On Sep 26, 2011, at 1:30 AM, René M. Andersen <[email protected]>
wrote:

Hi all,

We have encountered a scenario, which we have not yet found a good way to
handle. The scenario seems common enough, to expect that others may already
have dealt with it.

*Scenario*
We have a message type (MyMessage) which results in an entity (Entity1)
being inserted into a database. Entity1 references another entity (Entity2)
which is identified in MyMessage by an id. If Entity2 already exists Entity1
will reference the existing, if not Entity2 will be inserted in the same
unit of work as Entity1. The unit of work is implemented in a message
module.

Our service bus instance processes messages using several threads. If two
instances of MyMessage is processed simultanously and both reference the
same Entity2, two instances of Entity2 will be inserted in the database
instead of one.

Does anyone have a nice solution to this problem?
Currently we are saving Entity2 in the MyMessage consumer (flushing our
NHibernate session) and surrounding the saving of Entity2 with a lock...

-- 
You received this message because you are subscribed to the Google Groups
"Rhino Tools Dev" group.
To view this discussion on the web visit
<https://groups.google.com/d/msg/rhino-tools-dev/-/mSW-XMMH8tcJ>
https://groups.google.com/d/msg/rhino-tools-dev/-/mSW-XMMH8tcJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rhino-tools-dev?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino Tools Dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhino-tools-dev?hl=en.

Reply via email to