与 SQL 数据库通信的多线程应用程序的最佳实践
我厂有20台机器全部加工任务。我有一个数据库适配器,它与机器存储所有信息的 MYSQL 数据库进行通信。让每个工厂机器面板都有自己的数据库适配器和更新程序线程是否安全?更新程序线程只是不断检查面板的当前任务 ID 是否与数据库中的当前任务 ID 相同,如果不是,则使用有关新任务的信息重新填充面板。
我不确定连接太多是否会增加开销?
I have 20 factory machines all processing tasks. I have a database adapter that talks to a MYSQL database that the machines store all their info in. Is it safe to have each factory machine panel have it's own database adapter and updater thread? The updater thread just continuously checks to see if the panel's current taskID is the same as the current in the database and if not it repopulates the panel with information about the new task.
I'm not certain if having too many connections will add overhead or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RDBS 设计为在给定时间由多个客户端访问。这是他们的目的之一。
所以我认为 20 个,甚至 1000 个同时连接根本不会造成任何问题。
RDBS are designed to be accessed by multiple clients at a given time. It's one of their purpose.
So I don't think 20, or even a thousand of simultaneous connection will cause any problem at all.
与其让许多连接都执行相同的任务,不如创建一个进程来维护任务 ID 列表(如果所有计算机都不同)并检查数据库中的当前任务 ID。如果发生更改,则向所有机器(其任务 ID 已更改)发送消息以更新其面板。这将避免数据库上不必要的负载,并且还可以处理机器数量的增加而不会产生任何影响。
Rather than having many connection all doing same task create one process which maintains the list of taskid (if it is different for all machines) and checks the current taskid in database. if it is changed then send message to all the machines (which has change in their taskid)to update their panels. This will avoid unnecessary load on database and will also handle increase in number of machines without any impact.
mysql中的连接数由
max_connections
(允许同时连接的总数)系统变量和max_user_connections
(每个用户的最大同时连接数)控制。查看您的服务器设置,也许可以更改它们。默认数字肯定大于 20。The number of connection in mysql is controlled by
max_connections
(total number of allowed simultaneous connection) system variable andmax_user_connections
(max number of simultaneous connections per user). Take a look on your server settings and maybe change them. Default numbers are definitely bigger than 20 though.