2015-09-18 38 views
2

在我的代碼,我打開兩個MySQL連接,並使用HTTP請求,將數據插入到數據庫如何創建一個mysql連接池或更好的方式來初始化多個數據庫?

g.db = mysql.connector.connect(user=a ,password=password, host=localhost,database=mysq1) 
g.db1 = mysql.connector.connect(user=b,password=password, host=localhost,database=mysql2) 

@app.route('/user/<db>') 
def insert(db): 
    #code for inserting data into mysql1 database 
    #code for inserting data into mysql2 database 

我提出的HTTP請求來選擇數據庫。

curl -i 'localhost:5000/user/mysql1' # 

它運作良好,數據是越來越插入到選定的數據庫。 但我想爲兩個連接創建一個連接池,然後使用該池。

問題:

  1. 如何實現MySQL連接池?

  2. 是否有其他更好的方式來初始化connections.Currently連接打開每個請求。

回答

0

使用ORM框架使事情變得更容易,下面是我們用任何ORM框架創建連接池的基本方法和一般方法。

  1. mysql.connector.pooling模塊實現池。

  2. 當 提供連接到請求者時,池會打開多個連接並處理線程安全。

  3. 連接池的大小可以在創建池時進行配置。 此後無法調整大小。

創建自己的池,並將它命名,myPool的在連接池的參數,你也可以聲明池的大小= 5(這是數據庫連接數)。

請參閱下面的詳細資料:

dbconfig = { 
    "database": "test", 
    "user":  "joe" 
} 

cnx = mysql.connector.connect(pool_name = "mypool", 
           pool_size = 3, 
           **dbconfig) 

DBCONFIG,數據庫配置是您給所有的配置細節,每次你改變你的DATABSE。事實上,如果你願意,你可以有多個數據庫。

請參閱本MySQL documentation here

我們可以看到更多的關於這個論點如何可以聲明:

MySQLConnectionPool(pool_name=None, 
        pool_size=5, 
        pool_reset_session=True, 
        **kwargs) 

此構造實例化管理的連接池對象。

參數詳細:

1. pool_name: The pool name. If this argument is not given, Connector/Python automatically generates the name, composed from whichever of the host, port, user, and database connection arguments are given in kwargs, in that order. 

It is not an error for multiple pools to have the same name. An application that must distinguish pools by their 
**pool_name** property should create each pool with a distinct name. 

2. pool_size: The pool size. If this argument is not given, the default is 5. 

你將會看到一些漂亮的文檔here

爲了使您的連接池的多線程,這個職位上stackoverflw可以真正的幫助。請參閱本post

+1

我經歷了這個鏈接,但在例子中只有一個連接打開。它將如何與2個連接一起工作? – mrsteel

+0

感謝您的詢問。在上面的構造函數中,如果你仔細看看,** pool_size = 3,這意味着它會建立的連接數是3 **反正我已經增加了一些東西來讓事情變得清晰。一步一步來,這是很好的話題,享受:) – Jordon

-1

另一種方式是使用任何Web服務器,我建議你使用JBoss。您可以創建一個數據源並使用您想要控制的連接數進行配置,並讓它由JBoss控制。

+0

感謝您的回覆,但我更感興趣的是實現連接池。我試圖不使用web服務器形式。 – mrsteel

3
#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import time 
import mysql.connector.pooling 


dbconfig = { 
    "host":"127.0.0.1", 
    "port":"3306", 
    "user":"root", 
    "password":"123456", 
    "database":"test", 
} 


class MySQLPool(object): 
    """ 
    create a pool when connect mysql, which will decrease the time spent in 
    request connection, create connection and close connection. 
    """ 
    def __init__(self, host="172.0.0.1", port="3306", user="root", 
       password="123456", database="test", pool_name="mypool", 
       pool_size=3): 
     res = {} 
     self._host = host 
     self._port = port 
     self._user = user 
     self._password = password 
     self._database = database 

     res["host"] = self._host 
     res["port"] = self._port 
     res["user"] = self._user 
     res["password"] = self._password 
     res["database"] = self._database 
     self.dbconfig = res 
     self.pool = self.create_pool(pool_name=pool_name, pool_size=pool_size) 

    def create_pool(self, pool_name="mypool", pool_size=3): 
     """ 
     Create a connection pool, after created, the request of connecting 
     MySQL could get a connection from this pool instead of request to 
     create a connection. 
     :param pool_name: the name of pool, default is "mypool" 
     :param pool_size: the size of pool, default is 3 
     :return: connection pool 
     """ 
     pool = mysql.connector.pooling.MySQLConnectionPool(
      pool_name=pool_name, 
      pool_size=pool_size, 
      pool_reset_session=True, 
      **self.dbconfig) 
     return pool 

    def close(self, conn, cursor): 
     """ 
     A method used to close connection of mysql. 
     :param conn: 
     :param cursor: 
     :return: 
     """ 
     cursor.close() 
     conn.close() 

    def execute(self, sql, args=None, commit=False): 
     """ 
     Execute a sql, it could be with args and with out args. The usage is 
     similar with execute() function in module pymysql. 
     :param sql: sql clause 
     :param args: args need by sql clause 
     :param commit: whether to commit 
     :return: if commit, return None, else, return result 
     """ 
     # get connection form connection pool instead of create one. 
     conn = self.pool.get_connection() 
     cursor = conn.cursor() 
     if args: 
      cursor.execute(sql, args) 
     else: 
      cursor.execute(sql) 
     if commit is True: 
      conn.commit() 
      self.close(conn, cursor) 
      return None 
     else: 
      res = cursor.fetchall() 
      self.close(conn, cursor) 
      return res 

    def executemany(self, sql, args, commit=False): 
     """ 
     Execute with many args. Similar with executemany() function in pymysql. 
     args should be a sequence. 
     :param sql: sql clause 
     :param args: args 
     :param commit: commit or not. 
     :return: if commit, return None, else, return result 
     """ 
     # get connection form connection pool instead of create one. 
     conn = self.pool.get_connection() 
     cursor = conn.cursor() 
     cursor.executemany(sql, args) 
     if commit is True: 
      conn.commit() 
      self.close(conn, cursor) 
      return None 
     else: 
      res = cursor.fetchall() 
      self.close(conn, cursor) 
      return res 


if __name__ == "__main__": 
    mysql_pool = MySQLPool(**dbconfig) 
    sql = "select * from store WHERE create_time < '2017-06-02'" 

    # test... 
    while True: 
     t0 = time.time() 
     for i in range(10): 
      mysql_pool.execute(sql) 
      print i 
     print "time cousumed:", time.time() - t0 

您可以創建之初的連接池create_pool()並最終導致MySQLConnectionPool(),當你需要連接到MySQL,你可以從池中獲取與get_connection()的連接,當你不需要連接,您可以將連接添加回池中conn.close()

相關問題