Research on the use of flask-sqlalchemy

Recently used to sqlalchemy flask, the flask because of sqlalchemy made some package, plus itself is not familiar with sqlalchemy, take a lot of detours on usage.

Sqlalchemy because no time to study the source code, you can only use a simple test.

1, flask-sqlalchemy is thread-safe

  Specifically refer to the article  https://blog.csdn.net/luffyser/article/details/89380186

 

2, each time after the completion of inquiry, remember that commit, or will take up the connection pool

  I made a simple test locally, if a single query request to finish, then do not commit continuous request several times, and then there will be no response to a request initiated, presumably did not release the database connection pool, is occupied. Cause the request to suspend the database, resulting in no response.

      End each request, direct commit, can solve this problem.  

    def queryLast(cls):
        try:
            ret = db.session.query(cls).order_by(cls.version.desc()).first()
       #db.session.expunge(ret) db.session.commit() except: db.session.rollback() ret = None return ret

 

3. However, if Commit, the query results may be cleared from the cache, if the object re-use query results, but also to establish a connection query again. So the question of the connection pool exhaustion will appear.

   As can be seen from the yellow icon of the log, after commit, when using the query results are returned, but also perform a query tasks and return the results, my god!

2019-12-10 12:31:21,650 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2019-12-10 12:31:21,651 INFO sqlalchemy.engine.base.Engine SELECT appversion.id AS appversion_id, appversion.version AS appversion_version, appversion.`appUrl` AS `appversion_appUrl`, appversion.des AS appversion_des, appversion.`createTime` AS `appversion_createTime`, appversion.`lastModiftyTime` AS `appversion_lastModiftyTime`, appversion.type AS appversion_type 
FROM appversion ORDER BY appversion.version DESC 
 LIMIT %s
2019-12-10 12:31:21,651 INFO sqlalchemy.engine.base.Engine (1,)
2019-12-10 12:31:21,661 INFO sqlalchemy.engine.base.Engine COMMIT
2019-12-10 12:31:21,680 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2019-12-10 12:31:21,681 INFO sqlalchemy.engine.base.Engine SELECT appversion.id AS appversion_id, appversion.version AS appversion_version, appversion.`appUrl` AS `appversion_appUrl`, appversion.des AS appversion_des, appversion.`createTime` AS `appversion_createTime`, appversion.`lastModiftyTime` AS `appversion_lastModiftyTime`, appversion.type AS appversion_type FROM appversion WHERE appversion.id = %s 2019-12-10 12:31:21,681 INFO sqlalchemy.engine.base.Engine (1,)

4, but sometimes (possibly slightly longer time) after the commit, then use the results of the query object, an error may occur: Instance <User at 0x32768d0> is not bound to a Session   

  Such a situation may be due to the binding of the session has been recovered, which no longer allows query, so wrong.

 

5. In summary, for safety reasons, if necessary, after the results of the query, plus db.session.expunge (RET) , disconnect the relational query results to the session. Let it be a local entity, will not be purged from the cache, the use of time, it will not inquire.

6, but the test, it was found a strange problem, and get a user interface, and the above code is almost no difference, actually will automatically rollback, this makes me baffled.

  My logic is taken to a query result (ie user), the user's status to determine whether the field is 1, if it is 1, then modify user attributes, and then commit, if not 1, the operation is not done, there is no call rollback.

  But I tested it was found, if not a time, it will automatically rollback.

        Perhaps this is sqlalchemy advanced features?

 

7. Summary:

  1, SQLAlchemy object entity (model), and the session established contact, you get, set these model of time, even if already commit, and the database will automatically re-establish the connection (get time to re-select, set time will be re to establish a connection, waiting for you to submit if you do not submit, this connection has always existed, will eventually run out.), so be careful using the model of the field, unless you really know what you're doing, what will happen.

  2, using entities and relations db.session.expunge will cut off the session. This is a good usage.

  3, but I strongly recommend myself to do a model, to do the business logic layer. sqlalchemy object database entity only to do the operation. This will avoid a lot of the time, the connection accidentally did not release the pit.

Guess you like

Origin www.cnblogs.com/windstore/p/12016224.html