which scope should a DAO typically have
-
03-07-2019 - |
Question
its out of question that a dao will not hold any state.
however, for easiest access to the class, is it better to use prototype( = new every time) or singleton?
simple object creation is cheap for dao's.. it typically only holds a sessionfactory, accessing the object from a list of singletons may be equally expensive.
clarfication: the focus of this question is, if there is a common convention to the scoping of daos.
Solution
If your question is about architecture, I'd go with scoping DAOs to the Unit of Work or Transaction you are doing. This reduces the potential for cross-transaction pollution and threading-issues.
If your question is about performance, then the answer lies within a profiler, which gives you accurate numbers for your particular workload.
OTHER TIPS
Or use prototype in combination with a pool as described here:
OR here:
EDIT: obviously, I assume that spring is used here. Otherwise I apologize
i have come to the conclusion that there is no "perfect" way of deciding this. singleton scope is most likely the wrong way of doing it in a web app, since you will have different sessions - one per request. so in a webapp - request scope may be the correct answer, but only if you are using it exclusively in requests and not in background tasks. prototype scope is viable - but only if you are not holding complex data there.