使用ORM框架使事情变得简单,以下是我们在没有任何ORM框架的情况下创建连接池的基本和通用方法。
该 mysql.connector.pooling 模块实现集中。
在向请求者提供连接时,池会打开许多连接并处理线程安全性。
连接池的大小可以在创建池时进行配置。此后无法调整大小。
创建自己的缓冲池并在连接缓冲池的参数中将其命名为 myPool ,也可以声明缓冲池大小= 5(这是数据库连接数)。
请参阅以下详细信息:
dbconfig = { "database": "test", "user": "joe"}cnx = mysql.connector.connect(pool_name = "mypool", pool_size = 3, **dbconfig)dbconfig,数据库配置是您每次更改数据库时都提供所有配置详细信息的位置。实际上,如果需要,您可以有多个数据库。
请在此处查看此MySQL文档
我们可以看到更多有关如何声明此参数的信息:
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.
您应该在这里看到一些不错的文档
为了使连接池成为多线程,在stackoverflw上的这篇文章可能会有所帮助。请看这篇文章



