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.

Was it helpful?

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top