• python sqlalchemy db.session 的commit()和colse()对session中的对象的影响


    实验一:commit()之后查看stu的属性id,查看db.session是否改变

    1. @db_test.route("/db_test",methods=["GET"])
    2. def db_test():
    3. stu=StuTest()
    4. stu.stu_age=22
    5. stu.stu_name="nnannns"
    6. stu.stu_class=11
    7. print("session1:",id(db.session))
    8. print("--------11111:",stu in db.session)
    9. db.session.add(stu)
    10. print("--------22222:",stu in db.session)
    11. db.session.commit()
    12. print("id=",stu.id)
    13. print("session2:",id(db.session))
    14. print("--------33333:",stu in db.session)
    15. return "hello"

    结果: 

    session1: 2205376105296
    --------11111: False
    --------22222: True
    id= 33
    session2: 2205376105296
    --------33333: True

    结论:commit之后db.session对象仍然存在,并且stu对象仍然在db.session中。 

    实验二:close()之后查看stu的属性id,查看db.session是否改变 

    1. @db_test.route("/db_test",methods=["GET"])
    2. def db_test():
    3. stu=StuTest()
    4. stu.stu_age=22
    5. stu.stu_name="nnannns"
    6. stu.stu_class=11
    7. print("session1:",id(db.session))
    8. print("--------11111:",stu in db.session)
    9. db.session.add(stu)
    10. print("--------22222:",stu in db.session)
    11. db.session.commit()
    12. db.session.close()
    13. print("id=",stu.id)
    14. print("session2:",id(db.session))
    15. print("--------33333:",stu in db.session)
    16. return "hello"

     结果:

    session1: 2666995239888
    --------11111: False
    --------22222: True

     raise orm_exc.DetachedInstanceError(
    sqlalchemy.orm.exc.DetachedInstanceError: Instance is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: http://sqlalche.me/e/14/bhk3)

    结论:close()之后会清除session中的已存在对象。

    为了查看是否会清除session,暂时先将stu.id取值去掉

    1. @db_test.route("/db_test",methods=["GET"])
    2. def db_test():
    3. stu=StuTest()
    4. stu.stu_age=22
    5. stu.stu_name="nnannns"
    6. stu.stu_class=11
    7. print("session1:",id(db.session))
    8. print("--------11111:",stu in db.session)
    9. db.session.add(stu)
    10. print("--------22222:",stu in db.session)
    11. db.session.commit()
    12. db.session.close()
    13. print("session2:",id(db.session))
    14. print("--------33333:",stu in db.session)
    15. return "hello"

     结果:

    session1: 2704349383504
    --------11111: False
    --------22222: True
    session2: 2704349383504
    --------33333: False

    结论:close()并不会销毁session,而是清除之内的实例对象。 

    实现三:db.create_scoped_session()创建新的session

    1. @db_test.route("/db_test",methods=["GET"])
    2. def db_test():
    3. stu=StuTest()
    4. stu.stu_age=22
    5. stu.stu_name="nnannns"
    6. stu.stu_class=11
    7. print("session1:",id(db.session))
    8. print("--------11111:",stu in db.session)
    9. db.session.add(stu)
    10. print("--------22222:",stu in db.session)
    11. db.session.commit()
    12. print("id=",stu.id)
    13. print("session2:",id(db.session))
    14. print("--------33333:",stu in db.session)
    15. db.session = db.create_scoped_session()
    16. print("--------44444:",stu in db.session)
    17. print("session3:",id(db.session))
    18. return "hello"

     session1: 2259114183632
    --------11111: False
    --------22222: True
    id= 39
    session2: 2259114183632
    --------33333: True
    --------44444: False
    session3: 2259147304080

    db.create_scoped_session()会创建新的session 

    实验室四,新的session创建后,还能读到原来的对象及其属性吗?

    1. @db_test.route("/db_test",methods=["GET"])
    2. def db_test():
    3. stu=StuTest()
    4. stu.stu_age=22
    5. stu.stu_name="nnannns"
    6. stu.stu_class=11
    7. print("session1:",id(db.session))
    8. print("--------11111:",stu in db.session)
    9. db.session.add(stu)
    10. print("111 stu id:",id(stu))
    11. print("--------22222:",stu in db.session)
    12. db.session.commit()
    13. print("111 id=",stu.id)
    14. print("session2:",id(db.session))
    15. print("--------33333:",stu in db.session)
    16. db.session = db.create_scoped_session()
    17. print("--------44444:",stu in db.session)
    18. print("session3:",id(db.session))
    19. print("222 id=",stu.id)
    20. print("222 stu id:",id(stu))
    21. print("--------55555:",stu in db.session)
    22. return "hello"

    打印:

    session1: 2131572083344
    --------11111: False
    111 stu id: 2131571801616
    --------22222: True
    111 id= 45
    session2: 2131572083344
    --------33333: True
    --------44444: False
    session3: 2131605649744
    222 id= 45
    222 stu id: 2131571801616
    --------55555: False 

    结论:虽然生成了新的session,但是stu实例对象还是存在的,但是不存在session中了。还是能读到stu.id的。

    到这里已经不明白了,有大神能解释这个原理吗?求解释。。。 

    python sqlachemy的session可以查看这个:https://www.cnblogs.com/ChangAn223/p/11277468.html#%E4%BA%8C%E5%8D%95%E7%BA%BF%E7%A8%8B%E4%B8%8B-scoped_session-%E5%AF%B9%E5%88%9B%E5%BB%BA-session-%E7%9A%84%E5%BD%B1%E5%93%8D

  • 相关阅读:
    AtCoder Beginner Contest 342 (ABCDEFG题)视频讲解
    1-乙基-3-甲基咪唑四氟硼酸盐1E-3MI-TFB|石墨烯/导电高分子/离子液体修饰的黄曲霉毒素B1(科研)
    阿里云oss(图片存储)--fastdfs (视频存储)
    DLP是如何防止数据泄露的?
    特斯拉/小鹏「调整」软件策略,智能驾驶「标配」进入全新周期
    Window10安装docker后双击打不开,命令行 unable to resolve docker endpoint
    Ant-Design-Pro使用QianKun微前端最佳实践
    Zookeeper leader选举源码分析(超详细)
    Mask R-CNN
    JAVA计算机毕业设计制药企业人力资源管理系统Mybatis+源码+数据库+lw文档+系统+调试部署
  • 原文地址:https://blog.csdn.net/yuxuan89814/article/details/132597739