Quoting Stanislav Muhametsin <[email protected]>:
Okay, let's reverse the setting of a problem.
I have a role X with property y. I need X.y() to be unique in scope
of entity type. That means, from all entites of type Z, that have
role X, if there is some entity that has V as value of X.y(), there
are no other entites of type Z, that have same V as value of X.y().
How do I achieve that?
One way to do this by creating life-cycle mixin, which in create()
queries all the entities of current type, having their X.y() as the
value of X.y() of current entity. If query returns any results, it
throws lifecycle-exception. This requires adding a querying service
to your app.
Another way is described in QI-272. Is that too hacky?
Are there any other ways?
Came to think about it, I think the query-option might be
thread-unsafe. Is the following situation possible?
We have two threads, Thread1 and Thread2, executing parallelly. They
both have their own, separate units of work. Now, Thread1 and Thread2
are in a point where they both create entity of type Z. They both set
X.y() to be same value, let's say V. Thread1 gets to call
newInstance() method for EntityBuilder first.
Lifecycle-mixin for Z kicks in. It executes a query, and sees that
there are no other entities, which have V as value of X.y(). Then the
scheduler puts Thread1 on hold, and starts executing Thread2.
Thread2 calls newInstance() as well. Lifecycle-mixin for Z kicks in
again, now in Thread2. It also performs a query, and sees that no
other entity has V as value of X.y().
Now we have two threads, both having entity with different identity,
but same X.y() value. So if this situation is possible, using queries
in create() mixin to ensure uniqueness of some property (possibly also
association and many-association?) is only valid when we wait for all
possibly concurrently executing uow's creating entities of type Z to
finish. Clearly, that is a huge time-sink, both for application, and
coder. Not to mention that when having semaphore, which signals the
end of UoW handling entites of type Z, the semaphore must be unique in
scope of whole domain - be that one application or many applications,
on one or many computers.
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev