How to create a connection request in the Python database connection pool

Source: Internet
Author: User

If you have set the connection pool during operations in the Python database connection pool, you can perform the following operations. Through the following content, you can easily use the Python database connection pool related steps. I hope the following article will be useful to you.

RequestConnection:

 
 
  1. db = pool.connection() 

You can use these connections like the original DB-API 2. However, we actually use the strong connection of the ''steadydb'' version. Note that the connection can be shared with other threads, as long as you set the maxshared parameter to non-zero, and the DB-API 2 module also allows. If you want to use a dedicated connection, use:

 
 
  1. db = pool.connection(0) 

If you no longer need this connection, you can return it to the connection pool using db. close (). You can also use the same method to obtain another connection. Warning do not use the following method in a multi-threaded environment:

 
 
  1. pool.connection().cursor().execute(...)  
  2. db = pool.connection()  
  3. cur = db.cursor()  
  4. cur.execute(...)  
  5. res = cur.fetchone()  
  6. cur.close() # or del cur  
  7. db.close() # or del db 

Example [for your future use]

Use the PersistentDB Module

 
 
  1. import threading,time,datetime  
  2. import MySQLdb  
  3. import DBUtils.PersistentDB  
  4. persist = DBUtils.PersistentDB.PersistentDB(MySQLdb,100,host='localhost',user='root',passwd='321',db='test',charset='utf8')  
  5. conn = persist.connection()  
  6. cursor = conn.cursor()  
  7. cursor.execute("insert into me values(1,'22222')")  
  8. conn.commit()  
  9. conn.close()   

With the above content, you can get the database connection!

Related Article

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.