1
votes

I'm using Apache Ignite with Spring Data. I need a column "username" to be unique. In fact "username" is the key of the Ignite cache. I think ignite doesn't implement unique constraint yet. Using plain ignite API, I'm not sure if I can do a lock like:

IgniteCache<String, Integer> cache = ignite.cache("userCache");
Lock lock = cache.lock("username1");
lock.lock();
//check if doesn't exist yet
...

as "username1" doesn't exist yet. Is there other approach?

2

2 Answers

2
votes

I believe your approach will work, however, there is a suitable method for what you are trying to achieve: IgniteCache.putIfAbsent

0
votes

Current solution with Spring Data and SpringTransactionManager is:

@Transactional("pessimisticTransactionManager")
public void create(final User user) throws AlreadyExistsException {
    logger.info("Creating {}", user);
    if(userRepository.findByName(user.getName()).isPresent()) {
        throw new AlreadyExistsException(user.getName() + " already in use.") ;
    }
    userRepository.save(user.getName(), user);      
}

but with optimisticTransactionManager should also work and have better performance as the chance of collision in names is low for my system.