One way to increment your counter is to use the atomicity guarantee inherent in
the LDAP modify operation.

  1. read the current value of the counter attribute.

  2. modify the counter by performing a remove of the previously read value
     followed directly by an add of the new value (in a single operation).
     For example,

     ModificationItem[] atomicIncrement =
         new ModificationItem[]{
             new ModificationItem(
                 new BasicAttribute("nextAvailableUidNumber", CURRENT-VALUE)),
             new ModificationItem(
                 new BasicAttribute("nextAvailableUidNumber", NEXT-VALUE))

    3. if the modify fails then re-read and re-modify.

The key element of this approach is that the entire modify operation will only
be performed if all its modifications are successful. The remove attribute
attempt will fail if the exact value being removed is not already present.

Philip Yarra wrote:
> Hi, I hope this is the right forum.
> I'm writing an LDAP-based user management tool in Java, using JNDI. One
> thing I want to do is use a counter to track the next available uidNumber.
> To this end I've created a attribute to store it.
> Now I need a way to safely increment that value. I see from this
> ( that it can be done, and OpenLDAP
> supports it. But what I cannot understand is how to do this via JNDI. I
> can issue a DirContext.modifyAttribute() command, but the only
> modification operations I can see supported are add, remove and replace.
> One alternative seems to be to get the current value, increment it, then
> do a replace - which of course risks two people created concurrently
> getting the same uidNumber.
> Another option is to create a single-value attribute as a lock - if that
> succeeds, proceed to get, increment and set the counter, then remove the
> locking attribute.
> Ideally, I'd really like to use a mod-increment - can anyone advise? If
> not: what would people recommend?
> Regards, Philip.

Reply via email to