原來3版本的只能叫單文檔事務,即針對行的事務。所以沒必要顯式提供調用,多文檔事務由于有損耗,照顧到性能的需求,提供了事務開啟關閉的接口。
多行,多集合,多庫之間讀肯定會牽扯到一致性讀,所以多文檔事務肯定是有必要的。
4.2版本可能會支持分片系統多文檔事務,4.0的多文檔事務最多只面向復制集
集合:
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;
// 使用表對象的插入函數進行插入
coll_1.insertOne({'not_test':false})
coll_2.insertOne({'not_test':false})
// 提交事務
session.commitTransaction()
// 回滾事務
session.abortTransaction()
// 檢查事務準確性
db.coll_1.find()
db.coll_2.find()
默認事務生命周期為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"
因為忘記設置后向兼容性,執行失敗,在同一個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 }
// 可以看到,報錯,重新設置版本兼容性后,原先的事務出現了問題
[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
// 內部已經回滾掉了,結果竟然需要我手動回滾才行,暈
[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()
// 這邊可以看到,插入語句并沒有真正被執行
[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
// 這兒又抽風了,內部回滾之后禁止提交操作,嘗試進行回滾,結果說已經提交了,不能回滾,真是暈死
[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的空間。
另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章名稱:【MongoDB】4.0版本事務上手測試-創新互聯
文章網址:http://vcdvsql.cn/article10/iiggo.html
成都網站建設公司_創新互聯,為您提供做網站、網站改版、網站設計公司、電子商務、小程序開發、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯