Dbutil Configuring the Database connection pool

Source: Internet
Author: User

Dbutils:
ImportPymysql fromDbutils.pooleddbImportPooleddb, Shareddbconnection#Defining pool Connection PoolsPOOL =Pooleddb (creator=pymysql,#modules that use linked databasesMaxconnections=6,#the maximum number of connections allowed for a connection pool, 0 and none means no limit on the number of connectionsmincached=2,#at least 0 of the free links created in the link pool are not created when initializingMaxcached=5,#most idle links in the link pool, 0 and none are not limitedMaxshared=3,    #The maximum number of links shared in a linked pool, 0 and none means sharing all. PS: Useless, because Pymysql and mysqldb modules such as threadsafety are 1, all values regardless of set to how much, _maxcached forever is 0, so forever is all links are shared. Blocking=true,#If there are no available connections in the connection pool, wait is blocked. True, wait, False, not wait and then errorMaxusage=none,#the maximum number of times a link is reused, none means unlimitedSetsession=[],#a list of commands to execute before starting the session. such as: ["Set Datestyle to ...", "Set time zone ..."]ping=0,#Ping the MySQL server to check if the service is available. # for example: 0 = None = never, 1 = default = Whenever it is requested, 2 = When a cursor is created, 4 = When a query is executed, 7 = Alwayshost='127.0.0.1', Port=3306, the user='Root', Password='123', Database='Db_conn', CharSet='UTF8')deffunc (): Conn=pool.connection () cursor=conn.cursor (pymysql.cursors.DictCursor) Cursor.execute ('SELECT * from user') Result=Cursor.fetchall ()#print (Result)    #print (list result )cursor.close () conn.close ( )

Use the connection pool to change the additions and deletions
 fromDb_demoImportDb_connImportPymysql#Create a function to open a connection pooldefOpen_conn (): Conn=Db_conn. Pool.connection () cur=conn.cursor (pymysql.cursors.DictCursor)returnConn, cur#function to close the connection pooldefclose_conn (cur, conn): Cur.close () conn.close ()#View all information for a tabledefselect_all (sql, args): conn, cur=Open_conn () cur.execute (SQL, args) result=Cur.fetchall () close_conn (cur, conn)returnresult#Insert Table Functiondefinsert_help (sql, args): conn, cur=Open_conn () cur.execute (SQL, args) result=Conn.commit () close_conn (cur, conn)returnResult
View Code



Dbutil Configuring the Database connection pool

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.