Python Database connection Pool

Source: Internet
Author: User

To install the Dbutils library:

python3-m pip install dbutils to install Dbutils Library

To create a database object by connecting the pool:

Here is a reference to my previous blog: http://www.cnblogs.com/letmeiscool/p/8434381.html and dbutils User guide: Http://blog.csdn.net/gashero/article /details/1577187 to write. A separate method for creating a connection pool was written and executed when the database object was created. Each time you go to execute SQL, you do not need to create a connection pool, you need to execute the connection method _getconnect,sql every time you execute SQL to close the connection, the connection is collected by the database connection pool.

#-*-coding:utf-8-*-s#mysql and SQL Server Library import pymysql,pymssqlfrom dbutils.pooleddb import Pooleddbclass database:def            __init__ (SELF,*DB): If Len (db) ==5: #mysql数据库 self.host=db[0] self.port=db[1]            SELF.USER=DB[2] self.pwd=db[3] self.db=db[4] Else: #sqlserver数据库        Self.host=db[0] Self.port=none self.user=db[1] self.pwd=db[2] self.db=db[3] Self._createpool () def _createpool (self): if is not self.db:raise nameerror+ "database information not set" if (Self.port==none): self. POOL=POOLEDDB (creator=pymssql,mincached=2, maxcached=5,maxshared=3, maxconnections=6, blocking=True,host=            Self.host,user=self.user, password=self.pwd,database=self.db,charset= "UTF8") Else: Self. POOL=POOLEDDB (creator=pymysql,mincached=2, maxcached=5,maxshared=3, maxconnections=6, blocking=True,host= Self.host,Port=self.port, user=self.user,password=self.pwd,database =self.db,charset= "UTF8") def _ GetConnect (self): self.conn=self. Pool.connection () cur=self.conn.cursor () If not cur:raise "database not Connected" Else:retu RN cur #查询sql def execquery (self,sql): Cur=self._getconnect () cur.execute (SQL) Relist=cur.fetch All () Cur.close () Self.conn.close () return relist #非查询的sql def execnoquery (self,sql): CU R=self._getconnect () cur.execute (SQL) Self.conn.commit () Cur.close () Self.conn.close ()

  

Parameters of the POOLEDDB:

  • Dbapi: The Db-api 2 module that needs to be used
  • mincached : Number of empty connections opened at startup (default 0 means no connection is created at start)
  • maxcached: The maximum number of connections used by the connection pool (the default value of 0 means that the connection pool size is not limited)
  • maxshared: The maximum number of shared connections allowed (the default value of 0 means that all connections are private) if the maximum number is reached, the connection that is requested to be shared will be used for sharing.
  • maxconnections: Maximum allowable number of connections (default 0 means no limit)
  • Blocking: Sets the behavior when the maximum number is reached (the default value of 0 or False means that an error is returned; others are blocked until the number of connections is reduced)
  • maxusage: The maximum number of reusable times a single connection is allowed (the default value of 0 or False for unrestricted multiplexing). When the maximum value is reached, the connection is automatically reconnected (closed and re-opened)
  • setsession: An optional list of SQL commands is used to prepare each session, such as ["Set Datestyle to German", ...]
  • Other, you can set the parameters for passing to true Db-api 2, such as hostname, database, user name, password, and so on.

Python 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.