栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何创建一个mysql连接池或任何更好的方法来初始化多个数据库?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何创建一个mysql连接池或任何更好的方法来初始化多个数据库?

使用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,数据库配置是您每次更改数据库时都提供所有配置详细信息的位置。实际上,如果需要,您可以有多个数据库。

请在此处查看此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上的这篇文章可能会有所帮助。请看这篇文章



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414546.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号