In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Instant explosion-MongoDB4.0 will support multiple document transactions
Background
Part1: write at the front
In the early days, when we talked about MongoDB support transactions, we referred to the single document level for MongoDB, which is different from transactions in relational databases such as our MySQL. What is a single document transaction?
> db.username.update ({'name':' helei'}, {$set: {'age': 26,' score': 85}})
The above command updates the row in the username collection where name is the helei column, and if the age update to 26 score fails due to downtime or other reasons, then MongoDB will roll back the operation.
Part2: single document ACID implementation
When MongoDB updates a single document, it locks the document, and to understand the locking mechanism of MongoDB, you need to understand the following concepts:
1.Intent Lock, the intent lock indicates that the reader-writer intends to read or write to a finer-grained resource. For example, if an intention lock is added to a collection, it indicates that the reader or writer intends to read or write to a document in the collection.
2.MGL multi-granularity locking mechanism (Multiple granularity locking), including S lock (Shared lock), IS lock (Intent Share lock), X lock (Exclusive lock), IX lock (Intent Exclusive lock)
In the case of Part1, MongoDB adds an X lock to a document that name is helei, and an intentional write lock (IX) to the collection, database, and instance that contains the document.
Multi-document transaction in MongoDB4.0
Part1: multi-document transaction
MongoDB 4.0 will add support for multiple document transactions, which provide globally consistent data results through snapshot isolation, and execution will either succeed or fail to ensure data integrity.
Transactions in MongoDB4.0 will be as convenient for developers as normal relational databases, such as start_transaction and commit_transaction. Enabling MongoDB for multiple document transactions also does not affect the load on the machine. In this summer's release of MongoDB 4.0, transactions will be the first to provide support on replica sets, while multi-document transactions in sharding architecture will also be implemented in the MongoDB4.2 version.
In earlier versions of MongoDB, only single document transactions were supported, and if you want to use multiple document transactions, you need to rely on special data modeling to ensure that. In MongoDB 4.0, no matter how you model your data, you can support multiple document transactions.
The following figure shows the core features newly supported in each release:
Part2:Python
How to start a transaction in Python
With client.start_session () as s: s.start_transaction (): try: collection.insert_one (doc1, session=s) collection.insert_one (doc2, session=s) except: s.abort_transaction () raise s.commit_transaction ()
Part3:Java
How to start a transaction in Java
Try (ClientSession clientSession = client.startSession ()) {clientSession.startTransaction (); try {collection.insertOne (clientSession, docOne); collection.insertOne (clientSession, docTwo); clientSession.commitTransaction ();} catch (Exception e) {clientSession.abortTransaction ();}}
-- Summary.
Through this article, we learned about the most subversive feature of MongoDB4.0-the support for multiline document transactions, and how to start a transaction in a development language. As the author's level is limited and the writing time is very short, it is inevitable that there will be some errors or inaccuracies in the article. I urge readers to criticize and correct them. Like the author's article, click a wave of attention in the upper right corner, thank you.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.