CodeIgniter 的模型加载问题
如果我在一个控制器中加载 5 个模型,是否会为所有模型分别建立与数据库的连接?
因为它会造成服务器挂起的问题,因为为每个模型的数据库连接打开多个端口。
如果有什么解决办法吗?
提前致谢。
If I load 5 models in a controller, will the connection to database will be established for all the models separately ?
Because it is creating problem of server hanging, due to opening multiple ports for the database connection for each model.
If there is any solution?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,codeigniter 应该为您加载的每个模型建立一个新的数据库连接。您如何得出 codeigniter 打开多个端口的结论?那不应该是不可能的
No, codeigniter should establish a new database connection for every model you load. How did you draw the conclusion codeigniter is opening multiple ports? That shouldn't be possible
不,CodeIgniter 使用单例模式,这意味着您只能为每个模型/库创建一个实例(或一次一个数据库连接)。
问题可能出在您的数据库查询中 - 它们可能会减慢响应速度。您在数据库配置中使用持久连接吗?
有很多问题,但您提供的代码不多,所以我们只能猜测。
No, CodeIgniter is using the singleton pattern, which means that you can only make one instance of each model/library (or one database connection at a time).
The problem is probably within your database queries - they might be slowing the responses. Are you using a persistant connection in the database config?
There's a lot of questions, but not much code from you, so we can only guess.
你100%确定问题就是这个吗?我从来没有加载过那么多模型,也是因为 CI 的工作方式有点单例,所以对象始终是一个,而且如果一个实例已经存在,类也不会重新加载/重新实例化。
如果您的数据库有问题,请尝试在 config/database.php 中将 pconnect(持久连接)设置为 FALSE。您可能还想打开缓存,这可能会有所帮助。
Are you 100% sure that the problem is that? I never had any loading that amount of models, also because CI works as kinda singleton, so the object is always one, and also classes aren't re-loaded/re-instantiated if one instance already exists.
If you have probelms with your database, try setting pconnect (persistent connection) to FALSE in
config/database.php
. You might also want to turn caching ON, it might help.