VI.Multidocument Transactions

Recipe 6-1. Working with Multidocument Transactions(多文档事务处理)
  1.使用以下命令开启主从数据库并切换至20001的主数据
    start mongod --bind_ip localhost --dbpath E:\study\ducument\technology\2020\mongodb\res1 --port 20001 --replSet myrs
    start mongod --bind_ip localhost --dbpath E:\study\ducument\technology\2020\mongodb\res2 --port 20002 --replSet myrs
    start mongod --bind_ip localhost --dbpath E:\study\ducument\technology\2020\mongodb\res3 --port 20003 --replSet myrs
    mongo.exe localhost:20001
  2.使用employee数据库并创建容器
    use employee
    db.createCollection("employee")
  3.向容器employee中插入数据
    db.employee.insert([{_id:1001,empName:"Subhashini"},{_id:1002, empName:"Shobana"}])
  4.开启事务,在此事务中插入新数据并提交(注意,要一并操作,不然事务会超时)
    session.startTransaction()
    session.getDatabase("employee").employee.insert([{_id:1003,empName:"Taanushree"},{_id:1004, empName:"Aruna M S"}])
    session.commitTransaction()
  5.获取内部事务的数据,即包含session提交的数据,可以看到session事务添加的两条记录
    session.getDatabase("employee").employee.find()
  6.获取外部事务的数据,如果session已经提交,则看到的结果同5,否则,看不到session提交的数据
    db.employee.find()


Recipe 6-2. Isolation Test Between Two Concurrent Transactions(独立两个事务)
 1.基于Recipe 6-1的操作后继续
 2.创建session1并更新数据
    var session1 = db.getMongo().startSession()
    session1.startTransaction()
    session1.getDatabase("employee").employee.update({_id:1003},{$set:{designation: "TL" }})
    session.commitTransaction()
 3.查看更新后的数据,1003的数据改变了
    session1.getDatabase("employee").employee.find()
 4.创建session2并更新数据
    var session2 = db.getMongo().startSession()
    session2.startTransaction()
    session2.getDatabase("employee").employee.update({_id:{$in:[1001,1004]}},{$set:{designation:"SE"}},{multi:"true"})
    session.commitTransaction()
  5.查看session2更新的数据,但是session1更新的数据没有在此session2中变化
    session2.getDatabase("employee").employee.find()

Recipe 6-3. Transactions with Write Conflicts(写事务冲突)
    1.基于Recipe 6-2的操作后继续
    2.开启一个session
        var session1 = db.getMongo().startSession()
        session1.startTransaction()
    3.session1执行更新数据
        session1.getDatabase("employee").employee.update({empName:"Subhashini"},{$set:{empName: "Subha" }})
    4.开启另一个session
        var session2 = db.getMongo().startSession()
        session2.startTransaction()
    5.session2执行更新数据
        session2.getDatabase("employee").employee.update({empName:"Subhashini"},{$set:{empName: "Subha" }})
    6.session2执行事务时会报错
    7.以上的操作最后使用如下一并执行,这样才可以看到结果
        var session1 = db.getMongo().startSession()
        session1.startTransaction()
        session1.getDatabase("employee").employee.update({empName:"Subhashini"},{$set:{empName: "Subha" }})
        var session2 = db.getMongo().startSession()
        session2.startTransaction()
        session2.getDatabase("employee").employee.update({empName:"Subhashini"},{$set:{empName: "Subha" }})

Recipe 6-4. Discarding Data Changes with abortTransaction (丢弃事务执行失败的数据)
    1.基于Recipe 6-1的操作后继续
    2.切换数据库并创建新的容器
        use student;
        db.createCollection("student")
    3.向新的容器中插入数据
        db.student.insert({_id:1001,name:"subhashini"})
        db.student.insert({_id:1002,name:"shobana"})
    4.开启session
        var session1 = db.getMongo().startSession()
        session1.startTransaction()
    5.session1更新数据
        session1.getDatabase("student").student.insert({_id:1003,name:"Taanushree"})
    6.查询数据,可以看到插入的数据
        session1.getDatabase("student").student.find()
    7.使用以下查询,看不到更新数据因为事务没有提交
        db.student.find()
    8.让事务夭折
        session1.abortTransaction()
    9.使用以下的查询,没有看到插入的数据,因为事务没有提交就夭折了
        db.student.find()
        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Transactions.doTran是一个方法,它用于手动处理事务。这个方法包含了一些操作,并且在操作成功后调用了ContextUtil.SetComplete()来标记事务已完成,如果操作失败则调用了ContextUtil.SetAbort()来标记事务已中止。这个方法返回一个字符串,如果所有操作都成功执行,则返回"方法执行成功",否则返回"两个方法至少有一个执行失败!"。 另外,还有一种自动处理事务的方法,可以在方法上加上[AutoComplete(true)]特性,然后使用Using语句来创建一个TransactionScope对象,这样就可以自动处理事务。在Using语句块中执行具体的操作,如果操作成功,则事务会自动提交;如果操作失败,则事务会自动回滚。 总的来说,Transactions.doTran是一个用于手动处理事务的方法,而使用System.Transactions命名空间中的TransactionScope类可以实现自动处理事务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [asp.net事务机制](https://blog.csdn.net/weixin_30284355/article/details/96262580)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [数据库的事务和锁机制(SQL Server)](https://blog.csdn.net/oathevil/article/details/5617113)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值