"MongoDB" 4.0 version transaction get started test

Source: Internet
Author: User
Tags assert dba rollback uuid dochub

The basics of getting started with transactions:

The original 3 version can only be called single-document transactions, that is, for row transactions. So there is no need to explicitly provide a call, the multi-document transaction due to the loss, to take care of the performance requirements, provides the transaction to open the shutdown interface.
Multi-line, multi-collection, multi-Library reading is bound to be involved in consistent reading, so multi-document transactions must be necessary.
Version 4.2 may support multipart system multi-document transactions, and 4.0 of multi-document transactions are targeted at most replication sets

Get ready

Collection:

use dba;[10.240.129.36:[email protected]]> db.coll_2.find()[10.240.129.36:[email protected]]> db.coll_1.find(){ "_id" : ObjectId("5b371e120d6160b73a9fcee6"), "test" : true }{ "_id" : ObjectId("5b371e6d0d6160b73a9fcee7"), "test" : true }

Streamline commands:

// 显式开启一个会话(难道通过shell链接来本身不就是会话吗?)session=db.getMongo().startSession()// 显式开启事务session.startTransaction()// 获取表对象coll_1=session.getDatabase(‘dba‘).coll_1coll_2=session.getDatabase(‘dba‘).coll_2;// 使用表对象的插入函数进行插入coll_1.insertOne({‘not_test‘:false})coll_2.insertOne({‘not_test‘:false})// 提交事务session.commitTransaction()// 回滚事务session.abortTransaction()// 检查事务准确性db.coll_1.find()db.coll_2.find()
How is the timeout parameter set?

The default transaction life cycle is 60 seconds and is automatically cleaned up after it is exceeded

// The minimum value for transactionLifetimeLimitSeconds is 1 second.db.adminCommand( { setParameter: 1, transactionLifetimeLimitSeconds: 30 } )// You can also set parameter transactionLifetimeLimitSeconds at startup time.// 可以考虑写在配置文件中mongod --setParameter "transactionLifetimeLimitSeconds=30"
First attempt

Because forget to set the back compatibility, execution fails, after setting in the same shell, insert again, prompt the transaction does not exist

[10.240.129.36:[email protected]]> session.starttransactions () 2018-06-30t14:17:10.670+0800 E QUERY [JS] Referenceerror:session is not defined: @ (shell):1:1[10.240.129.36:[email protected]]> Db.getmongo () Connection to 127.0.0.1:30001[10.240.129.36:[email protected]]> Db.getmongo (). Startsession () session {"id": UUID ("887F4564-4F8A-4783-886D-A55A536F41AA")}[10.240.129.36:[email protected]]> Session=db.getMongo (). Startsession () session {"id": UUID ("9B3E10B9-93CD-4010-B1A3-0E7B49796DB3")}[10.240.129.36:[email protected]] > session.starttransaction () [10.240.129.36:[email protected]]> coll_1=session.getdatabase (' dba '). Coll _1;dba.coll_1[10.240.129.36:[email protected]]> coll_2=session.getdatabase (' DBA '). coll_2;dba.coll_2[ 10.240.129.36:[email protected]]> coll_1.insertone ({' Not_test ': false})//Post compatibility requirement is 4,  This means that you want to open the transaction must be more than MONGOD4 version 2018-06-30t14:28:55.708+0800 E QUERY [JS] writecommanderror:transactions is only supported inFeaturecompatibilityversion 4.0. See Http://dochub.mongodb.org/core/4.0-feature-compatibility for more information. : Writecommanderror ({"OK": 0, "errmsg": "Transactions is only supported in Featurecompatibilityversion 4.0. See Http://dochub.mongodb.org/core/4.0-feature-compatibility for more information. "," code ": 50773," codename ":" L ocation50773 "}) [email protected]/mongo/shell/bulk_api.js:420:48bulk/[email protected]/mongo/shell/ Bulk_api.js:902:1bulk/[email protected]/mongo/shell/bulk_api.js:1150:21[email protected]/mongo/shell /crud_api.js:252:9@ (Shell):1:1[10.240.129.36:[email protected]]> Db.admincommand ({ Setfeaturecompatibilityversion: ' 4.0 '}) {"OK": 1}//can see, error, reset version compatibility, the original transaction has a problem [10.240.129.36:[email  Protected]]> coll_1.insertone ({' Not_test ': false}) 2018-06-30t14:31:16.706+0800 E QUERY [JS] Writecommanderror: Given Transaction number 0 does not match any in-progress transactions. : Writecommanderror ({"Errorlabels":["Transienttransactionerror"], "OK": 0, "errmsg": "Given transaction number 0 does not match any IN-PR ogress transactions. "," code ": 251," codename ":" Nosuchtransaction "}) [EMAIL PROTECTED]/MONGO/SHELL/BULK_API.J S:420:48bulk/[email protected]/mongo/shell/bulk_api.js:902:1bulk/[email protected]/mongo/shell/bulk_ api.js:1150:21[email protected]/mongo/shell/crud_api.js:252:9@ (Shell): 1:1//transaction is not well adjusted [10.240.129.36:[email  protected]]> session.starttransaction () 2018-06-30t14:33:53.293+0800 E QUERY [JS] error:transaction already I n progress on the session. : [Email protected]/mongo/shell/session.js:732:1[email protected]/mongo/shell/session.js:925:17@ ( Shell):1:1[10.240.129.36:[email protected]]> Db.getmongo () .aborttransaction[10.240.129.36:[email  Protected]]> Db.getmongo () .aborttransaction[10.240.129.36:[email protected]]> Db.getMongo (). Aborttransaction[10.240.129.36:[email protected]]> Db.getmoNGO () .aborttransaction[10.240.129.36:[email protected]]> Db.getmongo (). aborttransaction[10.240.129.36:[ Email protected]]>[10.240.129.36:[email protected]]> Db.getmongo (). abortTransaction[ 10.240.129.36:[email protected]]> Db.getmongo (). aborttransaction[10.240.129.36:[email protected] > exit//exit the current connection directly
Second attempt:
[10.240.129.36:[email protected]]> Session=db.getmongo (). Startsession () session {"id": UUID (" B82d71b0-b698-4909-b5b8-a7845dba98b2 ")}[10.240.129.36:[email protected]]> session.startTransaction () [ 10.240.129.36:[email protected]]> coll_1=session.getdatabase (' DBA '). coll_1dba.coll_1[10.240.129.36:[ Email protected]]> coll_2=session.getdatabase (' dba ') .coll_2;dba.coll_2[10.240.129.36:[email  Protected]]> coll_1.insertone ({' Not_test ': false}) {"Acknowledged": true, "Insertedid": ObjectId ("5b37270429c5c0 c486b4b17b ")}[10.240.129.36:[email protected]]> coll_2.insertone ({' Not_test ': false})//The transaction rollback here may be due to a timeout , the default transaction lifetime maximum value is 1 minutes 2018-06-30t15:00:50.629+0800 E QUERY [JS] writecommanderror:transaction 0 has been aborted.  : Writecommanderror ({"Errorlabels": ["Transienttransactionerror"], "OK": 0, "errmsg": "Transaction 0 has been aborted. "," code ": 251," codename ":" Nosuchtransaction "}) [email protected]/mongo/shell/bulk_api.js:420:48bulk/[email protected]/mongo/shell/bulk_api.js:902:1bulk/[email protected]/ mongo/shell/bulk_api.js:1150:21[email protected]/mongo/shell/crud_api.js:252:9@ (Shell): 1:1[10.240.129.36: [Email protected]] > Coll_2.insertone ({' Not_test ': false}) 2018-06-30t15:00:59.308+0800 E QUERY [js] writecommanderror:transaction 0 ha s been aborted.  : Writecommanderror ({"Errorlabels": ["Transienttransactionerror"], "OK": 0, "errmsg": "Transaction 0 has been aborted. "," code ": 251," codename ":" Nosuchtransaction "}) [EMAIL PROTECTED]/MONGO/SHELL/BULK_API.J S:420:48bulk/[email protected]/mongo/shell/bulk_api.js:902:1bulk/[email protected]/mongo/shell/bulk_ api.js:1150:21[email protected]/mongo/shell/crud_api.js:252:9@ (shell): After the 1:1//transaction time-out rollback, you should also be able to re-open the new transaction, here always error [ 10.240.129.36:[email protected]]> session.starttransaction () 2018-06-30t15:01:04.932+0800 E QUERY [JS] Error : Transaction already in progress on tHis session. : [Email protected]/mongo/shell/session.js:732:1[email protected]/mongo/shell/session.js:925:17@ ( Shell): 1:1//internal has been rolled back, the results need me to manually rollback, Halo [10.240.129.36:[email protected]]> session.aborttransaction () [ 10.240.129.36:[email protected]]> session.starttransaction () [10.240.129.36:[email protected]]> Coll_1.insertone ({' Not_test ': false}) {"Acknowledged": true, "Insertedid": ObjectId ("5b372ac229c5c0c486b4b17e")}[10 .240.129.36:[email protected]]> coll_2.insertone ({' Not_test ': false}) {"Acknowledged": true, "insertedId": ObjectId ("5b372ac529c5c0c486b4b17f")}//try to explain Operation [10.240.129.36:[email protected]]> Coll_1. Explain (). Find ({' Test_explain ': 1}) 2018-06-30t15:03:09.375+0800 e QUERY [JS] Error:explain failed: {"OK": 0, "E Rrmsg ":" Cannot run ' explain ' in a multi-document transaction. "," code ": 50767," codename ":" Location50767 "}:/ /It is an error, explain operation cannot be performed in a transaction [EMAIL PROTECTED]/MONGO/SHELL/UTILS.Js:25:13[email protected]/mongo/shell/explainable.js:31:1constructor/[email protected]/mongo/shell/ explain_query.js:171:24constructor/[email protected]/mongo/shell/explain_query.js:210:26[email  protected]/mongo/shell/utils.js:594:1@ (SHELL2): 1:1//try to submit the times wrong [10.240.129.36:[email protected]]>        Session.committransaction () 2018-06-30t15:04:21.985+0800 E QUERY [JS] Error:command failed: {"errorlabels": [ "Transienttransactionerror"], "OK": 0, "errmsg": "Transaction 1 has been aborted.", "code": 251, "Co Dename ":" Nosuchtransaction "}: [email protected]/mongo/shell/utils.js:25:13[email protected]/mongo/ shell/assert.js:18:14[email protected]/mongo/shell/assert.js:534:17[email protected]/mongo/shell/ assert.js:618:16[email protected]/mongo/shell/session.js:929:17@ (Shell):1:1[10.240.129.36:[email  Protected]]> Db.coll_1.find () {"_id": ObjectId ("5b371e120d6160b73a9fcee6"), "test": true} {"_id": ObjectId ("5B371e6d0d6160b73a9fcee7 ")," test ": True}[10.240.129.36:[email protected]]> db.coll_2.find ()//This side can be seen, The INSERT statement is not actually executed [10.240.129.36:[email protected]]> session.committransaction () 2018-06-30t15:05:46.288+0800 E QUERY [JS] Error:command failed: {"errorlabels": ["Transienttransactionerror"], "OK": 0, "errm SG ":" Transaction 1 has been aborted. "," code ": 251," codename ":" Nosuchtransaction "}: [Email protected]/mon go/shell/utils.js:25:13[email protected]/mongo/shell/assert.js:18:14_asser[email protected]/mongo/ shell/assert.js:534:17[email protected]/mongo/shell/assert.js:618:16[email protected]/mongo/shell/ session.js:929:17@ (shell): 1:1//here again, after internal rollback prohibit commit operation, try to rollback, the result said has been submitted, can't rollback, really dizzy death [10.240.129.36:[email  Protected]]> session.aborttransaction () 2018-06-30t15:06:04.162+0800 E QUERY [JS] Error:cannot call AbortTransactio n after calling CommitTransaction. : [Email protected]/mongo/shell/session.js:764:1[email protected]/mongo/shell/session.js:934:17@ (shell): 1:1 
Third attempt (successfully submitted):
[10.240.129.36:[email protected]]> Session=db.getmongo (). Startsession () session {"id": UUID (" e7935985-160d-4969-adfe-3bae401e155f ")}[10.240.129.36:[email protected]]> session.startTransaction () [ 10.240.129.36:[email protected]]> coll_1=session.getdatabase (' DBA '). coll_1dba.coll_1[10.240.129.36:[ Email protected]]> coll_2=session.getdatabase (' dba ') .coll_2;dba.coll_2[10.240.129.36:[email  Protected]]> coll_1.insertone ({' Not_test ': false}) {"Acknowledged": true, "Insertedid": ObjectId ("5b373202d0b208 B7386e9c20 ")}[10.240.129.36:[email protected]]> coll_2.insertone ({' Not_test ': false}) {" acknowledged ": True, "Insertedid": ObjectId ("5b373202d0b208b7386e9c21")}[10.240.129.36:[email protected]]> Session.committransaction () [10.240.129.36:[email protected]]> use dbaswitched to DB dba[10.240.129.36:[ Email protected]]> Db.coll_1.find () {"_id": ObjectId ("5b371e120d6160b73a9fcee6"), "test": true} {"_id": objec TId("5b371e6d0d6160b73a9fcee7"), "test": true} {"_id": ObjectId ("5b373202d0b208b7386e9c20"), "Not_test": false}[10.240. 129.36:[email protected]]> Db.coll_2.find () {"_id": ObjectId ("5b373202d0b208b7386e9c21"), "Not_test": false }
Fourth attempt (test rollback):
[10.240.129.36:[email protected]]> Session=db.getmongo (). Startsession () session {"id": UUID (" e7935985-160d-4969-adfe-3bae401e155f ")}[10.240.129.36:[email protected]]> session.startTransaction () [ 10.240.129.36:[email protected]]> coll_1=session.getdatabase (' DBA '). coll_1dba.coll_1[10.240.129.36:[ Email protected]]> coll_2=session.getdatabase (' dba ') .coll_2;dba.coll_2[10.240.129.36:[email  Protected]]> coll_1.insertone ({' Not_test ': false}) {"Acknowledged": true, "Insertedid": ObjectId ("5b373202d0b208 B7386e9c20 ")}[10.240.129.36:[email protected]]> coll_2.insertone ({' Not_test ': false}) {" acknowledged ": True, "Insertedid": ObjectId ("5b373202d0b208b7386e9c21")}[10.240.129.36:[email protected]]> Session.committransaction () [10.240.129.36:[email protected]]> use dbaswitched to DB dba[10.240.129.36:[ Email protected]]> Db.coll_1.find () {"_id": ObjectId ("5b371e120d6160b73a9fcee6"), "test": true} {"_id": objec TId("5b371e6d0d6160b73a9fcee7"), "test": true} {"_id": ObjectId ("5b373202d0b208b7386e9c20"), "Not_test": false}[10.240. 129.36:[email protected]]> Db.coll_2.find () {"_id": ObjectId ("5b373202d0b208b7386e9c21"), "Not_test": false }[10.240.129.36:[email protected]]> Session=db.getmongo (). Startsession () session {"id": UUID (" Cc0e2d23-1766-4ea5-87a5-957efcae28ad ")}[10.240.129.36:[email protected]]> session.startTransaction () [ 10.240.129.36:[email protected]]> coll_1=session.getdatabase (' DBA '). coll_1dba.coll_1[10.240.129.36:[ Email protected]]> coll_2=session.getdatabase (' dba ') .coll_2;dba.coll_2[10.240.129.36:[email  Protected]]> coll_1.insertone ({' Not_test ': false}) {"Acknowledged": true, "Insertedid": ObjectId ("5b3732f0d0b208 B7386e9c22 ")}[10.240.129.36:[email protected]]> coll_2.insertone ({' Not_test ': false}) {" acknowledged ": True, "Insertedid": ObjectId ("5b3732f1d0b208b7386e9c23")}[10.240.129.36:[email protected]]> session.aborttransaction () [10.240.129.36:30001_primar[email protected]]> db.coll_2.find () {"_id": ObjectId ("5b373202d0b208b7386e9c21"), "Not_test": false}[10.240.129.36:[email protected]]> Db.coll_1.find () {"_id": ObjectId ("5b371e120d6160b73a9fcee6"), "test": true} {"_id": ObjectId ("5b371e6d0d6160b73a9fcee7"), "test": true} {"_id": ObjectId ("5b373202d0b208b7386e9c20"), "Not_test": false}
Attention

Default transaction timeout time is a little short, 1 minutes
Default lock Wait time is short, 5 milliseconds
The operation of the transaction is cumbersome, not as easy as RDS begin commit rollback (referred to as Shell operations)
The corresponding oplog of a transaction cannot exceed 16M (refers to the file size in Bson format)
The opening of the explicit transaction feature presents a new requirement for the WT cache. Similar to the IBP transaction is not submitted on time, similar to undo used to hold the snapshot or the old version of Things will certainly occupy a lot of wt space.

"MongoDB" 4.0 version transaction get started test

Related Article

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.