2

I would like to use connection pooling with mysql-connector 2.1.5

On Ubuntu everything works fine, but I would like to switch to Alpine and have used this file to install the connector (as described here).

The installation seems to work. I can use mysql.connector.connection(), but whenever I try mysql.connector.pooling.MySQLConnectionPool() I get the error AttributeError: 'module' object has no attribute 'pooling'.

I have also tried creating a pool implicitly using cnxpool = mysql.connector.connection(pool_name="...") but I cannot get the connection. Everytime I try cnx = cnxpool.get_connection() I get the error AttributeError: 'MySQLConnection' object has no attribute 'get_connection'

Is there any way to use pooling with the platform independent install package, or is it just not implemented there?

  • How about never using pooling. It is not a requirement, and it does not save much in most environments. – Rick James Feb 13 '17 at 0:12
4

I did some experimentation in console trying to figure out why this was happening for me as well. In the end, I searched right into the file folder for the package looking to ensure that the pooling file was present in the mysql.connector package version I had.

Sure enough there it was.

In the end, I simply changed my import statement. I had been using

import mysql.connector
pool = mysql.connector.pooling.MySQLConnectionPool(**config)

After playing in console for a bit I realized that it would be happy if I just used;

import mysql.connector
from mysql.connector import pooling
pool = pooling.MySQLConnectionPool(**config)

Worked fine. Couldn't tell you "why", but it worked.

1

you can create the connection pool using a MySQL Connector Python’s pooling.MySQLConnectionPool and pooling.PooledMySQLConnection class provides the instantiation and management of connection pools.

import mysql.connector
from mysql.connector import Error
from mysql.connector import pooling
try:
   connection_pool = mysql.connector.pooling.MySQLConnectionPool(pool_name="pool",
                                                              pool_size=1,
                                                              pool_reset_session=True,
                                                              host='yourHost',
                                                              database='mysql_db',
                                                              user='username',
                                                              password='password')

Please refer - Python connection Pooling

0

You can use the python-mysql-pool https://github.com/LuciferJack/python-mysql-pool

so easy config and use support multi database and support dynamic pool and fixed pool.

step:1

"""
file:mysql_config.py change to your db config
"""
db_config = {
    'local': {
        'host': "10.95.130.118", 'port': 8899,
        'user': "root", 'passwd': "123456",
        'db': "marry", 'charset': "utf8",
        'pool': {
            #use = 0 no pool else use pool
            "use":1,
            # size is >=0,  0 is dynamic pool
            "size":0,
            #pool name
            "name":"local",
        }
    },
    'poi': {
        'host': "10.95.130.***", 'port': 8787,
        'user': "lujunxu", 'passwd': "****",
        'db': "poi_relation", 'charset': "utf8",
        'pool': {
            #use = 0 no pool else use pool
            "use":0,
            # size is >=0,  0 is dynamic pool
            "size":0,
            #pool name
            "name":"poi",
        }
    },
}

step:2

"""
Note:create your own table
"""

step:3 (example show below)

"""
pool size special operation
"""
def query_pool_size():
    job_status = 2
    _sql = "select *  from master_job_list j  where j.job_status  in (%s) "
    _args = (job_status,)
    task = query(db_config['local'], _sql,_args)
    logging.info("query_npool method query_npool result is %s ,input _data is %s ", task , _args)
    return

"""
single query
"""
def query_npool():
    job_status = 2
    _sql = "select *  from master_job_list j  where j.job_status  !=%s "
    _args = (job_status,)
    task = query_single(db_config['local'], _sql,_args)
    logging.info("query_npool method query_npool result is %s ,input _data is %s ", task , _args)
    return

"""
insert
"""
def insert(nlp_rank_id,hit_query_word):
    #add more args
    _args = (nlp_rank_id,hit_query_word)
    _sql = """INSERT INTO nlp_rank_poi_online (nlp_rank_id,hit_query_word,rank_type,poi_list,poi_raw_list,article_id,city_id,status,create_time,version,source_from) VALUES (%s,%s,%s, %s, %s,%s, %s,%s, %s,%s,%s)"""
    affect = insertOrUpdate(db_config['local'], _sql, _args)
    logging.info("insert method insert result is %s ,input _data is %s ", affect , _args)
    return

"""
update
"""
def update(query_word,query_id):
    _args = (query_word,query_id)
    _sql = """update nlp_rank  set query_word = %s  WHERE  id = %s"""
    affect = insertOrUpdate(db_config['local'], _sql, _args)
    logging.info("update method update result is %s ,input _data is %s ", affect , _args)
    return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.