内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

【MongoDB】4.0版本事務上手測試-創(chuàng)新互聯(lián)

事務上手測試

基礎:

原來3版本的只能叫單文檔事務,即針對行的事務。所以沒必要顯式提供調用,多文檔事務由于有損耗,照顧到性能的需求,提供了事務開啟關閉的接口。
多行,多集合,多庫之間讀肯定會牽扯到一致性讀,所以多文檔事務肯定是有必要的。
4.2版本可能會支持分片系統(tǒng)多文檔事務,4.0的多文檔事務最多只面向復制集

建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡程序員、網(wǎng)頁設計師等,應用各種網(wǎng)絡程序開發(fā)技術和網(wǎng)頁設計技術配合操作的協(xié)同工作。成都創(chuàng)新互聯(lián)專業(yè)提供成都做網(wǎng)站、網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站制作(企業(yè)站、自適應網(wǎng)站建設、電商門戶網(wǎng)站)等服務,從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗的提升,我們力求做到極致!

準備

集合:

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

精簡命令:


// 顯式開啟一個會話(難道通過shell鏈接來本身不就是會話嗎?)
session=db.getMongo().startSession()
// 顯式開啟事務
session.startTransaction()
// 獲取表對象
coll_1=session.getDatabase('dba').coll_1
coll_2=session.getDatabase('dba').coll_2;
// 使用表對象的插入函數(shù)進行插入
coll_1.insertOne({'not_test':false})
coll_2.insertOne({'not_test':false})
// 提交事務
session.commitTransaction()
// 回滾事務
session.abortTransaction()
// 檢查事務準確性
db.coll_1.find()
db.coll_2.find()

超時參數(shù)怎么設置?

默認事務生命周期為60秒,超出后會被自動清理

// 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"

第一次嘗試

因為忘記設置后向兼容性,執(zhí)行失敗,在同一個shell中進行設置后,再次插入,提示事務不存在

[10.240.129.36:30001_primary@dba]> Session.startTransactions()
2018-06-30T14:17:10.670+0800 E QUERY    [js] ReferenceError: Session is not defined :
@(shell):1:1
[10.240.129.36:30001_primary@dba]> db.getMongo()
connection to 127.0.0.1:30001
[10.240.129.36:30001_primary@dba]> db.getMongo().startSession()
session { "id" : UUID("887f4564-4f8a-4783-886d-a55a536f41aa") }
[10.240.129.36:30001_primary@dba]> session=db.getMongo().startSession()
session { "id" : UUID("9b3e10b9-93cd-4010-b1a3-0e7b49796db3") }
[10.240.129.36:30001_primary@dba]> session.startTransaction()
[10.240.129.36:30001_primary@dba]> coll_1=session.getDatabase('dba').coll_1;
dba.coll_1
[10.240.129.36:30001_primary@dba]> coll_2=session.getDatabase('dba').coll_2;
dba.coll_2
[10.240.129.36:30001_primary@dba]> coll_1.insertOne({'not_test':false})
// 后向兼容性要求為4,也就是說想開啟事務必須是mongod4以上的版本
2018-06-30T14:28:55.708+0800 E QUERY    [js] WriteCommandError: Transactions are only supported in featureCompatibilityVersion 4.0. See http://dochub.mongodb.org/core/4.0-feature-compatibility for more information. :
WriteCommandError({
    "ok" : 0,
    "errmsg" : "Transactions are only supported in featureCompatibilityVersion 4.0. See http://dochub.mongodb.org/core/4.0-feature-compatibility for more information.",
    "code" : 50773,
    "codeName" : "Location50773"
})
WriteCommandError@src/mongo/shell/bulk_api.js:420:48
Bulk/executeBatch@src/mongo/shell/bulk_api.js:902:1
Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9
@(shell):1:1
[10.240.129.36:30001_primary@dba]> db.adminCommand({setFeatureCompatibilityVersion:'4.0'})
{ "ok" : 1 }

// 可以看到,報錯,重新設置版本兼容性后,原先的事務出現(xiàn)了問題
[10.240.129.36:30001_primary@dba]> 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-progress transactions.",
    "code" : 251,
    "codeName" : "NoSuchTransaction"
})
WriteCommandError@src/mongo/shell/bulk_api.js:420:48
Bulk/executeBatch@src/mongo/shell/bulk_api.js:902:1
Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9
@(shell):1:1

// 事務怎么也調不好了

[10.240.129.36:30001_primary@dba]> session.startTransaction()
2018-06-30T14:33:53.293+0800 E QUERY    [js] Error: Transaction already in progress on this session. :
startTransaction@src/mongo/shell/session.js:732:1
startTransaction@src/mongo/shell/session.js:925:17
@(shell):1:1
[10.240.129.36:30001_primary@dba]> db.getMongo().abortTransaction
[10.240.129.36:30001_primary@dba]> db.getMongo().abortTransaction
[10.240.129.36:30001_primary@dba]> db.getMongo().abortTransaction
[10.240.129.36:30001_primary@dba]> db.getMongo().abortTransaction
[10.240.129.36:30001_primary@dba]> db.getMongo().abortTransaction
[10.240.129.36:30001_primary@dba]>
[10.240.129.36:30001_primary@dba]> db.getMongo().abortTransaction
[10.240.129.36:30001_primary@dba]> db.getMongo().abortTransaction
[10.240.129.36:30001_primary@dba]> exit
// 直接退出當前連接

第二次嘗試:

[10.240.129.36:30001_primary@admin]> session=db.getMongo().startSession()
session { "id" : UUID("b82d71b0-b698-4909-b5b8-a7845dba98b2") }
[10.240.129.36:30001_primary@admin]>  session.startTransaction()
[10.240.129.36:30001_primary@admin]> coll_1=session.getDatabase('dba').coll_1
dba.coll_1
[10.240.129.36:30001_primary@admin]> coll_2=session.getDatabase('dba').coll_2;
dba.coll_2
[10.240.129.36:30001_primary@admin]> coll_1.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b37270429c5c0c486b4b17b")
}
[10.240.129.36:30001_primary@admin]> coll_2.insertOne({'not_test':false})

// 這里的事務回滾可能是因為超時引起的,默認事務生命周期大值為1分鐘

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"
})
WriteCommandError@src/mongo/shell/bulk_api.js:420:48
Bulk/executeBatch@src/mongo/shell/bulk_api.js:902:1
Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9
@(shell):1:1
[10.240.129.36:30001_primary@admin]> coll_2.insertOne({'not_test':false})
2018-06-30T15:00:59.308+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"
})
WriteCommandError@src/mongo/shell/bulk_api.js:420:48
Bulk/executeBatch@src/mongo/shell/bulk_api.js:902:1
Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9
@(shell):1:1

// 事務超時回滾之后,應該還可以重開新的事務才對,這里老是報錯

[10.240.129.36:30001_primary@admin]> session.startTransaction()
2018-06-30T15:01:04.932+0800 E QUERY    [js] Error: Transaction already in progress on this session. :
startTransaction@src/mongo/shell/session.js:732:1
startTransaction@src/mongo/shell/session.js:925:17
@(shell):1:1

// 內(nèi)部已經(jīng)回滾掉了,結果竟然需要我手動回滾才行,暈

[10.240.129.36:30001_primary@admin]> session.abortTransaction()
[10.240.129.36:30001_primary@admin]> session.startTransaction()
[10.240.129.36:30001_primary@admin]> coll_1.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b372ac229c5c0c486b4b17e")
}
[10.240.129.36:30001_primary@admin]> coll_2.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b372ac529c5c0c486b4b17f")
}

// 試試能不能進行explain操作

[10.240.129.36:30001_primary@admin]> coll_1.explain().find({'test_explain':1})
2018-06-30T15:03:09.375+0800 E QUERY    [js] Error: explain failed: {
    "ok" : 0,
    "errmsg" : "Cannot run 'explain' in a multi-document transaction.",
    "code" : 50767,
    "codeName" : "Location50767"
} :  // 果然是報錯了呢,在事務中不能進行explain操作
_getErrorWithCode@src/mongo/shell/utils.js:25:13
throwOrReturn@src/mongo/shell/explainable.js:31:1
constructor/this.finish@src/mongo/shell/explain_query.js:171:24
constructor/this.shellPrint@src/mongo/shell/explain_query.js:210:26
shellPrintHelper@src/mongo/shell/utils.js:594:1
@(shell2):1:1

// 嘗試提交時報錯
[10.240.129.36:30001_primary@admin]> 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,
    "codeName" : "NoSuchTransaction"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
commitTransaction@src/mongo/shell/session.js:929:17
@(shell):1:1

[10.240.129.36:30001_primary@dba]> db.coll_1.find()
{ "_id" : ObjectId("5b371e120d6160b73a9fcee6"), "test" : true }
{ "_id" : ObjectId("5b371e6d0d6160b73a9fcee7"), "test" : true }
[10.240.129.36:30001_primary@dba]> db.coll_2.find()
// 這邊可以看到,插入語句并沒有真正被執(zhí)行

[10.240.129.36:30001_primary@dba]> session.commitTransaction()
2018-06-30T15:05:46.288+0800 E QUERY    [js] Error: command failed: {
    "errorLabels" : [
        "TransientTransactionError"
    ],
    "ok" : 0,
    "errmsg" : "Transaction 1 has been aborted.",
    "code" : 251,
    "codeName" : "NoSuchTransaction"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
commitTransaction@src/mongo/shell/session.js:929:17
@(shell):1:1

// 這兒又抽風了,內(nèi)部回滾之后禁止提交操作,嘗試進行回滾,結果說已經(jīng)提交了,不能回滾,真是暈死

[10.240.129.36:30001_primary@dba]> session.abortTransaction()
2018-06-30T15:06:04.162+0800 E QUERY    [js] Error: Cannot call abortTransaction after calling commitTransaction. :
abortTransaction@src/mongo/shell/session.js:764:1
abortTransaction@src/mongo/shell/session.js:934:17
@(shell):1:1

第三次嘗試(成功提交):

[10.240.129.36:30001_primary@admin]> session=db.getMongo().startSession()
session { "id" : UUID("e7935985-160d-4969-adfe-3bae401e155f") }
[10.240.129.36:30001_primary@admin]> session.startTransaction()
[10.240.129.36:30001_primary@admin]> coll_1=session.getDatabase('dba').coll_1
dba.coll_1
[10.240.129.36:30001_primary@admin]> coll_2=session.getDatabase('dba').coll_2;
dba.coll_2
[10.240.129.36:30001_primary@admin]> coll_1.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b373202d0b208b7386e9c20")
}
[10.240.129.36:30001_primary@admin]> coll_2.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b373202d0b208b7386e9c21")
}
[10.240.129.36:30001_primary@admin]> session.commitTransaction()
[10.240.129.36:30001_primary@admin]> use dba
switched to db dba
[10.240.129.36:30001_primary@dba]> db.coll_1.find()
{ "_id" : ObjectId("5b371e120d6160b73a9fcee6"), "test" : true }
{ "_id" : ObjectId("5b371e6d0d6160b73a9fcee7"), "test" : true }
{ "_id" : ObjectId("5b373202d0b208b7386e9c20"), "not_test" : false }
[10.240.129.36:30001_primary@dba]> db.coll_2.find()
{ "_id" : ObjectId("5b373202d0b208b7386e9c21"), "not_test" : false }

第四次嘗試(測試回滾):

[10.240.129.36:30001_primary@admin]> session=db.getMongo().startSession()
session { "id" : UUID("e7935985-160d-4969-adfe-3bae401e155f") }
[10.240.129.36:30001_primary@admin]> session.startTransaction()
[10.240.129.36:30001_primary@admin]> coll_1=session.getDatabase('dba').coll_1
dba.coll_1
[10.240.129.36:30001_primary@admin]> coll_2=session.getDatabase('dba').coll_2;
dba.coll_2
[10.240.129.36:30001_primary@admin]> coll_1.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b373202d0b208b7386e9c20")
}
[10.240.129.36:30001_primary@admin]> coll_2.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b373202d0b208b7386e9c21")
}
[10.240.129.36:30001_primary@admin]> session.commitTransaction()
[10.240.129.36:30001_primary@admin]> use dba
switched to db dba
[10.240.129.36:30001_primary@dba]> db.coll_1.find()
{ "_id" : ObjectId("5b371e120d6160b73a9fcee6"), "test" : true }
{ "_id" : ObjectId("5b371e6d0d6160b73a9fcee7"), "test" : true }
{ "_id" : ObjectId("5b373202d0b208b7386e9c20"), "not_test" : false }
[10.240.129.36:30001_primary@dba]> db.coll_2.find()
{ "_id" : ObjectId("5b373202d0b208b7386e9c21"), "not_test" : false }
[10.240.129.36:30001_primary@dba]> session=db.getMongo().startSession()
session { "id" : UUID("cc0e2d23-1766-4ea5-87a5-957efcae28ad") }
[10.240.129.36:30001_primary@dba]> session.startTransaction()
[10.240.129.36:30001_primary@dba]> coll_1=session.getDatabase('dba').coll_1
dba.coll_1
[10.240.129.36:30001_primary@dba]> coll_2=session.getDatabase('dba').coll_2;
dba.coll_2
[10.240.129.36:30001_primary@dba]> coll_1.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b3732f0d0b208b7386e9c22")
}
[10.240.129.36:30001_primary@dba]> coll_2.insertOne({'not_test':false})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5b3732f1d0b208b7386e9c23")
}
[10.240.129.36:30001_primary@dba]> session.abortTransaction()
[10.240.129.36:30001_primary@dba]> db.coll_2.find()
{ "_id" : ObjectId("5b373202d0b208b7386e9c21"), "not_test" : false }
[10.240.129.36:30001_primary@dba]> db.coll_1.find()
{ "_id" : ObjectId("5b371e120d6160b73a9fcee6"), "test" : true }
{ "_id" : ObjectId("5b371e6d0d6160b73a9fcee7"), "test" : true }
{ "_id" : ObjectId("5b373202d0b208b7386e9c20"), "not_test" : false }

注意

默認事務超時時間有點短,1分鐘
默認鎖等待時間短,5毫秒
事務的操作有些繁瑣,不如RDS的 begin commit rollback方便(指SHELL操作)
一個事務的對應的oplog不能超過16M(指BSON格式的文件大小)
顯式事務功能的開啟對WT緩存提出了新的要求。類似于IBP事務不按時提交,類似undo用于存放快照或者舊版本的東西肯定會占用大量WT的空間。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章名稱:【MongoDB】4.0版本事務上手測試-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://m.rwnh.cn/article10/iiggo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站改版、網(wǎng)站設計公司、電子商務、小程序開發(fā)、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設
临西县| 耿马| 塔河县| 新河县| 林州市| 新竹市| 铜陵市| 西吉县| 佳木斯市| 和田市| 江口县| 灯塔市| 乾安县| 德清县| 云霄县| 迁安市| 太原市| 山阳县| 千阳县| 丹东市| 交城县| 开平市| 阳西县| 略阳县| 沅陵县| 峡江县| 梨树县| 宜宾市| 白朗县| 加查县| 永靖县| 大竹县| 张家界市| 客服| 嘉义市| 万载县| 濮阳市| 襄樊市| 仪陇县| 申扎县| 乐山市|