python sqlalchemy db.session 的commit()和colse()到底会不会清空session中的对象

虽然网上很多博客都在说:而commit动作会将session内的所有对象进行提交并删除,这就使得后续再从session里访问不到对象。

但是我实验下来不是这么回事:

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

@db_test.route("/db_test",methods=["GET"])
def db_test():
    stu=StuTest()
    stu.stu_age=22
    stu.stu_name="nnannns"
    stu.stu_class=11
    print("session1:",id(db.session))
    print("--------11111:",stu in db.session)
    db.session.add(stu)
    print("--------22222:",stu in db.session)
    db.session.commit()
    print("id=",stu.id)
    print("session2:",id(db.session))
    print("--------33333:",stu in db.session)
    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是否改变 

@db_test.route("/db_test",methods=["GET"])
def db_test():
    stu=StuTest()
    stu.stu_age=22
    stu.stu_name="nnannns"
    stu.stu_class=11
    print("session1:",id(db.session))
    print("--------11111:",stu in db.session)
    db.session.add(stu)
    print("--------22222:",stu in db.session)
    db.session.commit()
    db.session.close()
    print("id=",stu.id)
    print("session2:",id(db.session))
    print("--------33333:",stu in db.session)
    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取值去掉

@db_test.route("/db_test",methods=["GET"])
def db_test():
    stu=StuTest()
    stu.stu_age=22
    stu.stu_name="nnannns"
    stu.stu_class=11
    print("session1:",id(db.session))
    print("--------11111:",stu in db.session)
    db.session.add(stu)
    print("--------22222:",stu in db.session)
    db.session.commit()
    db.session.close()
    print("session2:",id(db.session))
    print("--------33333:",stu in db.session)
    return "hello"

 结果:

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

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

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

@db_test.route("/db_test",methods=["GET"])
def db_test():
    stu=StuTest()
    stu.stu_age=22
    stu.stu_name="nnannns"
    stu.stu_class=11
    print("session1:",id(db.session))
    print("--------11111:",stu in db.session)
    db.session.add(stu)
    print("--------22222:",stu in db.session)
    db.session.commit()
    print("id=",stu.id)
    print("session2:",id(db.session))
    print("--------33333:",stu in db.session)
    db.session = db.create_scoped_session()
    print("--------44444:",stu in db.session)
    print("session3:",id(db.session))
    return "hello"

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

db.create_scoped_session()会创建新的session 

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

@db_test.route("/db_test",methods=["GET"])
def db_test():
    stu=StuTest()
    stu.stu_age=22
    stu.stu_name="nnannns"
    stu.stu_class=11
    print("session1:",id(db.session))
    print("--------11111:",stu in db.session)
    db.session.add(stu)
    print("111 stu id:",id(stu))
    print("--------22222:",stu in db.session)
    db.session.commit()
    print("111 id=",stu.id)
    print("session2:",id(db.session))
    print("--------33333:",stu in db.session)
    db.session = db.create_scoped_session()
    print("--------44444:",stu in db.session)
    print("session3:",id(db.session))
    print("222 id=",stu.id)
    print("222 stu id:",id(stu))
    print("--------55555:",stu in db.session)
    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

你可能感兴趣的:(python,数据库,开发语言)