I have a domain object that looks something like this:
class UserStuff
{
String userid;
boolean primordial;
}
In the table, primordial is a TINYINT. A User can have possibly many UserStuff. What I want to ensure is that only one row (the first row created) will have primordial == 1. All subsequent rows will have primordial == 0. At a glance, there is nothing to worry about. Let's suppose multiple creation requests for UserStuff may happen at the same time, how can I guarantee this constraint ?
My initial idea is to sync on a string (in the domain service)
class DomainService
{
public void create( UserStuff stuff )
{
synchronized( stuff.userid + "/userstuff/lock" )
{
...
}
}
}
I would love to hear comments about this & alternative methods. Thanks