Mongodb doesn’t “lock” a document, does it?

I’m trying to check that mongodb does behave the way I expect it to behave and lock a document when it’s modified in a transaction.
I’m using

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>

and spring-data with a replicaset of

mongo 7.0.5 community

My goal is to do the following:

  • I create a transaction
  • I “lock” a document by writing to it
  • Another process tries to write on the same document
  • This second process “write” shall either hang until the transaction is comited or be refused because of a WriteConflict (I’d rather it to be refused)

I’m using this code :

@Getter
@Setter
class DummyDocument {
    @MongoId
    private String uuid;
    private String lock;
    private List<String> simpleCollection = new ArrayList<>();

    public DummyDocument(String uuid) {
        this.uuid = uuid;
    }
}

public class ConcurrentDocumentAccess {

    @Autowired
    private MongoClient client;

    @Autowired
    private MongoTemplate mongoTemplate;

    @After
    public void cleanup() {
        DummyDocument doc = mongoTemplate.findById("test", DummyDocument.class);
        mongoTemplate.remove(doc);
    }

    @Test
    public void documentLockingTest() {

        // Create a document
        DummyDocument doc = new DummyDocument("test");
        mongoTemplate.save(doc);

        // A query to find the doc
        Query findDoc = new Query().addCriteria(Criteria.where("uuid").is("test"));

        // An update to change the lock value in the doc
        Update lock = new Update().set("lock", "locked");

        // An update of the doc
        Update updateCollection = new Update().addToSet("simpleCollection", "something");

        try (ClientSession session = client.startSession()) {

            session.startTransaction();

            // Acquire lock on doc by writing on it
            UpdateResult lockResult = mongoTemplate.withSession(session).updateFirst(findDoc, lock,
                    DummyDocument.class);
            assertThat(lockResult.getModifiedCount() == 1L).isTrue();

            // Try to update the collection out of the transaction
            UpdateResult changeResult = mongoTemplate.updateFirst(findDoc, updateCollection, DummyDocument.class);
            // assertThat(changeResult.getModifiedCount() == 0L).isTrue();
            
            session.commitTransaction();
        } catch (MongoCommandException e) {
            e.printStackTrace();
        }
        DummyDocument updatedDoc = mongoTemplate.findOne(findDoc, DummyDocument.class);
        assertThat(updatedDoc.getLock()).isEqualTo("locked");
        assertThat(updatedDoc.getSimpleCollection()).doesNotContain("something");
    }
}

What I observe is that the query outside the transaction is forced on the document, then the transaction hangs until it fails with a response : {"errorLabels": ["TransientTransactionError"], "ok": 0.0, "errmsg": "Transaction with { txnNumber: 2 } has been aborted.", "code": 251, "codeName": "NoSuchTransaction"

Is there something I’m doing wrong or is it the expected behavior ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật