PESSIMISTIC locking in ISPN will not help you in this case because that only takes place when session.save() is called, which is probably too late. In other words, both T1 and T2 (threads) can/will see "a" as non-existent and add "a" as a child node of the root node and eventually call "session.save()" which in PESSIMISTIC mode will "serialize" the data changes, but both will perform the add.
IMO you need to solve this problem "at a higher level" in your application. I can think of several ways of doing this (not all may apply given your context):
- create "a" from a single thread (always)
- use an intermediate parent under the WS root which doesn't allow SNS and handle the exception
- use Java locks or some other mechanism to synchronize the part which creates "a"
- use JCR locks and handle LockException