其他分享
首页 > 其他分享> > VI.Multidocument Transactions

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()
        

标签:session1,session2,Transactions,--,VI,db,Multidocument,session,employee
来源: https://blog.csdn.net/owen_william/article/details/116307541