Sqlalchemy connection pool

Source: Internet
Author: User

Sqlalchemy's pool_size = 5
The pool is stored in the remote connection with the data volume,
Use C1 = engine. Connect () or
Session = scoped_session (sessionmaker (BIND = engine ))
Will be used to build the connection, and the step for building the connection
1. check whether there is a response in the pool. If yes, the response is returned.
2. If the pool does not have any available connections, a new connection is established.
When you use engine. Close () or
Session2.commit (), session2.rollback (), session2.close ()
Then, the response is returned to the pool.

Http://simple-is-better.com/news/651

The article says that we need to add the finally web. CTX. Orm. Commit ()
Web. CTX. Orm. Close () # <=-close the session, or use. Remove ()
I personally think this is not necessary, because the user uses Web. CTX. Orm. Commit ()

#!/usr/bin/env pythonfrom sqlalchemy import create_engineengine = create_engine("mysql://root:liukesun@172.17.22.131/testdb", pool_size=5,echo_pool=True)c1 = engine.connect()c2 = engine.connect()c3 = engine.connect()c1.close()c2.close()c3.close()# pool size is now three.raw_input()for i in xrange(10):    c = engine.connect()    print c.execute("select 1").fetchall()    c.close()

import stringimport randomimport webfrom sqlalchemy.orm import scoped_session, sessionmakerfrom models import *urls = (    "/", "add",    "/view", "view")def load_sqla(handler):    web.ctx.orm = scoped_session(sessionmaker(bind=engine))    try:        return handler()    except web.HTTPError:       web.ctx.orm.commit()       raise    except:        web.ctx.orm.rollback()        raise    finally:        web.ctx.orm.commit()        # If the above alone doesn't work, uncomment         # the following line:        #web.ctx.orm.expunge_all() app = web.application(urls, locals())app.add_processor(load_sqla)class add:    def GET(self):        web.header('Content-type', 'text/html')        fname = "".join(random.choice(string.letters) for i in range(4))        lname = "".join(random.choice(string.letters) for i in range(7))        u = User(name=fname                ,fullname=fname + ' ' + lname                ,password =542)        web.ctx.orm.add(u)        return "added:" + web.websafe(str(u)) \                            + "<br/>" \                            + '<a href="/view">view all</a>'class view:    def GET(self):        web.header('Content-type', 'text/plain')        return "\n".join(map(str, web.ctx.orm.query(User).all()))if __name__ == "__main__":    app.run()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.