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:
- 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:
- 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:
- pool.connection().cursor().execute(...)
- db = pool.connection()
- cur = db.cursor()
- cur.execute(...)
- res = cur.fetchone()
- cur.close() # or del cur
- db.close() # or del db
Example [for your future use]
Use the PersistentDB Module
- import threading,time,datetime
- import MySQLdb
- import DBUtils.PersistentDB
- persist = DBUtils.PersistentDB.PersistentDB(MySQLdb,100,host='localhost',user='root',passwd='321',db='test',charset='utf8')
- conn = persist.connection()
- cursor = conn.cursor()
- cursor.execute("insert into me values(1,'22222')")
- conn.commit()
- conn.close()
With the above content, you can get the database connection!