每个线程一个DAO还是线程安全DAO? [英] One DAO per thread or threadsafe DAO?

查看:209
本文介绍了每个线程一个DAO还是线程安全DAO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道多线程应用程序中是否有经过批准的实践。我应该每个线程使用一个DAO还是仅将一个DAO做成一个线程安全的单例对象。

I'm wondering if there's an approved practice in a multi-threaded app. Should I have one DAO per thread or simply make one DAO a thread safe singleton.

推荐答案

这实际上很大程度上取决于机制您用于数据访问。如果您具有非常可伸缩的数据访问权限,并且有很多线程,则使用某种形式的线程静态数据访问可能会很有用。

This really depends a lot on the mechanism you're using for data access. If you have a very scalable data access, and lots of threads, using some form of thread static data access can be advantageous.

如果您没有可伸缩的数据访问权限,则您的提供程序不支持每个进程多个线程,或者您那时不需要可伸缩性,请使用

If you don't have scalable data access, your provider doesn't support multiple threads per process, or you just don't need the scalability at that point, using a singleton with appropriate synchronization is simpler and easier to implement.

对于大多数商务样式应用程序,我个人认为单例方法更易于维护,并且可能更好(如果适用)没有别的原因,这比有效测试容易得多。可能不需要多线程进行数据访问,因为数据访问可能不会成为影响可用性的瓶颈(如果设计正确,并且可以适当地批处理请求)。

For most business style applications, I personally think the singleton approach is easier to maintain, and probably better - if for no other reason than it's much, much easier to test effectively. Having multiple threads for data access is likely not required, as the data access is probably not going to be a bottleneck that effects usability (if you design correctly, and batch requests appropriately).

这篇关于每个线程一个DAO还是线程安全DAO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆