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...