每个线程一个DAO或线程安全DAO?

时间:2022-03-13 21:03:42

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.

我想知道在多线程应用程序中是否有批准的做法。我应该每个线程有一个DAO还是简单地使一个DAO成为一个线程安全的单例。

2 个解决方案

#1


4  

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).

对于大多数商业风格的应用程序,我个人认为单例方法更容易维护,并且可能更好 - 如果没有其他原因,那么更容易有效地测试。可能不需要具有多个用于数据访问的线程,因为数据访问可能不会成为影响可用性的瓶颈(如果您正确设计,并且适当地批量请求)。

#2


0  

Use the approach that best suits your application architecture, unless:

使用最适合您的应用程序体系结构的方法,除非:

1) Your data access objects are expensive to create, in which case you should lean toward a thread-safe singleton.

1)您的数据访问对象创建起来很昂贵,在这种情况下,您应该倾向于使用线程安全的单例。

2) Your objects maintain mutable state, as in the Active Record pattern. (Immutable DAO configuration state, like timeout thresholds, doesn't count.)

2)您的对象保持可变状态,如Active Record模式。 (不可变的DAO配置状态,如超时阈值,不计算在内。)

#1


4  

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).

对于大多数商业风格的应用程序,我个人认为单例方法更容易维护,并且可能更好 - 如果没有其他原因,那么更容易有效地测试。可能不需要具有多个用于数据访问的线程,因为数据访问可能不会成为影响可用性的瓶颈(如果您正确设计,并且适当地批量请求)。

#2


0  

Use the approach that best suits your application architecture, unless:

使用最适合您的应用程序体系结构的方法,除非:

1) Your data access objects are expensive to create, in which case you should lean toward a thread-safe singleton.

1)您的数据访问对象创建起来很昂贵,在这种情况下,您应该倾向于使用线程安全的单例。

2) Your objects maintain mutable state, as in the Active Record pattern. (Immutable DAO configuration state, like timeout thresholds, doesn't count.)

2)您的对象保持可变状态,如Active Record模式。 (不可变的DAO配置状态,如超时阈值,不计算在内。)