Instant explosion-mongodb4.0 will support multi-document transactions

Source: Internet
Author: User
Tags mongodb mongodb support


Instant explosion-mongodb4.0 will support multi-document transactions


Background


part1: written in the first

In the early days, we said that MongoDB support transaction refers to the single document level for MongoDB, which is different from the transaction in our relational database such as MySQL, what is a single document transaction?

>db.username.update ({' name ': ' Helei '}, {$set: {' age ': $, ' score ': 85}})

The preceding command updates the row in the Username collection, name is the Helei column, and if age is updated to 26,score the update fails due to an outage or other cause, MongoDB rolls back the operation.


Part2: Single Document ACID implementation

When MongoDB updates a single document, it locks the document, and understanding the lock mechanism of MONGODB requires understanding the following concepts:

1.Intent Lock, Intent lock indicates that the read-write party (Reader-writer) intends to read or write to a finer-grained resource. For example, if an intent lock is added to a collection, the read or write intent is to read or write to a document in the collection.

2.MGL Multi-granularity locking mechanism (multiple granularity locking  ), with S lock (Shared lock), is lock (Intent Share Lock), x Lock (Exclusive Lock), IX lock (Intent Exclusive Lock)

In the example in Part1, MongoDB adds an X lock to the document named Helei, and the database and instance have an intent write lock (IX) for the collection containing the document, and the operation on that document guarantees atomicity.


Multi-document transactions in the MongoDB4.0

Part1: Multi-Document transactions

MongoDB 4.0 will increase support for multi-document transactions by snapshot isolation, which provides globally consistent data results, and performs either all succeeds or all fails to guarantee data integrity.


Transactions in MongoDB4.0 will be as convenient for developers as common relational databases, such as Start_transaction and commit_transaction. MongoDB, which enables multi-document transactions, also does not affect the load on the machine. In MongoDB 4.0 released this summer, transactions will be the first to provide support on replica sets, and multi-document transactions in the Sharding architecture will be implemented in the MongoDB4.2 version.


In earlier versions of MongoDB, only single-document transactions were supported, and if multiple-document transactions were to be used, it would be warranted to rely on special data modeling. In MongoDB 4.0, no matter how you model your data, you can support multiple document transactions.


Shows the core features that are newly supported in each release:





Part2: Python

How to open a transaction in Python

With Client.start_session () as S:s.start_transaction () Try:collection.insert_one (Doc1, Session=s) CO Llection.insert_one (DOC2, Session=s) s.commit_transaction () except Exception:s.abort_transaction ()


Part3: Java

How to open 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 --


Instant explosion-mongodb4.0 will support multi-document transactions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.