在 ado.net 中重用命令和连接对象是减少新对象创建的合法方法吗?

发布于 2024-09-02 01:58:39 字数 222 浏览 1 评论 0原文

我们的应用程序当前的编写方式涉及在访问 sqlite 数据库的每个方法中创建一个新的连接和命令对象。考虑到我们需要它在 WM5 设备上运行,这会导致性能糟糕。

我们的计划是每个线程仅使用一个连接对象,但我们也想到每个线程也使用一个全局命令对象。这样做的好处是它减少了通过在各处实例化对象而创建的垃圾收集器的开销。

我找不到任何反对这样做的建议,但想知道是否有人可以明确回答这是好事还是坏事,为什么?

The current way our application is written, involves creating a new connection and command object in every method that access our sqlite db. Considering we need it to run on a WM5 device, that is leading to hideous performance.

Our plan is to use just one connection object per-thread but it's also occurred to us to use one global command object per-thread too. The benefit of this is it reduces the overhead on the garbage collector created by instantiating objects all over the place.

I can't find any advice against doing this but wondered if anyone can answer definitively if this is a good or bad thing to do, and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2024-09-09 01:58:39

虽然我不确定是否要减少命令对象的数量,但减少连接数量绝对是一个好计划。它们的设置成本相对较高(嘿,它们实际上涉及打开磁盘文件!),因此将它们保留相对较长的时间是非常明智的。因此,执行计划的第一阶段并重新计时,看看这是否使事情足够好,或者您是否需要做更多的优化工作……

请注意,很有可能每个连接生成一次命令对象也将节省费用,因为这将允许它们被编译一次并重复使用多次。除非您首先坚持连接,否则这并不重要!

While I'm not sure about reducing the number of command objects, reducing the number of connections is definitely a good plan. They're designed to be relatively expensive to set up (hey, they involve actually opening a disk file!) so keeping them around for a relatively long time is highly sensible. So do the first stage of your plan and retime to see if that makes things good enough, or if you need to do more work optimizing…

Note that it is quite possible that generating the command objects once per connection will be a saving too, since that will allow them to be compiled once and reused multiple times. Not that that matters until you're persisting the connection in the first place!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文