It's not possible to lock a mongodb document. What if I need to?

前端 未结 12 1677
死守一世寂寞
死守一世寂寞 2020-12-24 11:34

I know that I can\'t lock a single mongodb document, in fact there is no way to lock a collection either.

However, I\'ve got this scenario, where I think I need some

12条回答
  •  执念已碎
    2020-12-24 12:31

    If you have a system with > 1 servers then you'll need a distributive lock.

    I prefer to use Hazelcast.

    While saving you can get Hazelcast lock by entity id, fetch and update data, then release a lock.

    As an example: https://github.com/azee/template-api/blob/master/template-rest/src/main/java/com/mycompany/template/scheduler/SchedulerJob.java

    Just use lock.lock() instead of lock.tryLock()

    Here you can see how to configure Hazelcast in your spring context:

    https://github.com/azee/template-api/blob/master/template-rest/src/main/resources/webContext.xml

提交回复
热议问题