Given the following scenario

  • A canned enterprise application that maintains its own connection pool
  • A homegrown client application to the enterprise app. This app is built using Spring framework, with the DAO pattern

While I may have a simplistic view of this, I think the following line of thinking is sound:

  • Having a fixed pool of DAO objects, holding on to connection objects from the pool. Clearly, the pool should be capable of scaling up (or down depending on need) and the connection objects must outnumber the DAOs by a healthy margin. Good

  • Instantiating brand new DAOs for every request to access the enterprise app; each DAO will attempt to grab a connection from the pool and release it when it's done. Bad

Since these are service objects, there will be no (mutable) state held by the objects (reduced risk of concurrency issues)

I also think that with #1, there should be little to no resource contention, while in #2, there'll almost always be a DAO waiting to be serviced. Is my thinking correct and what could go wrong?

  • I wouldn't worry about any performance effects of DAO instantiation: programmers.stackexchange.com/q/149563/34183 Commented Jun 9, 2014 at 18:48
  • @MikePartridge - My concern is not the object instantiation per se; it's the resource contention that results that worries me. On instantiation, each object will attempt to grab a connection from the pool. Uncurtailed instantiation will result in quick exhaustion of the pool. Pre-instantiating will ensure controlled access to pooled resources
    – kolossus
    Commented Jun 9, 2014 at 20:38
  • For #1, how would you handle more requests than DAOs in the pool? Commented Jun 10, 2014 at 14:36
  • @MikePartridge - The same way any other pool handles it: queueing requests. Requests can wait within a specified timeout for a DAO to be available. The difference between that and actually pooling the resource in my case is that I don't have control over the timeout of the resource; hence why I'm trying to throttle it at the DAO level
    – kolossus
    Commented Jun 10, 2014 at 14:39
  • 1
    You don't have control over the timeout when using a database connection pool? What pool implementation are you using? Commented Jun 10, 2014 at 14:41

2 Answers 2


Entity Framework and Linq to SQL both instantiate DAO's as needed.

DAO's in Entity Framework (they're called Data Contexts) are deliberately designed to be lightweight to instantiate, so you create a new one each time a data access is required. Sometimes folks try to cache these Data Contexts, but that is a design error, and it can cause concurrency problems (DataContext is not thread-safe). In general, you create one DataContext object for each Unit of Work.

Entity Framework is most commonly used with SQL Server; SQL Server maintains a pool of "recently-used connections" that can be reused, so that the process of opening a new connection is also as fast as possible.

Further Reading
Linq to SQL DataContext Lifetime Management
Building N-Tier Applications with Entity Framework ... for various approaches.

  • The way I read the linked material, these frameworks need to instantiate new context objects because by definition, they are context sensitive and consequently not reusable. The Linq resource also observes most other ORMs are stateless in their data connectivity and management object. The Linq-SQL pattern is not the norm. My example is far from hypothetical and there's no state being manipulated in the DAOs. In the same way a servlet container only ever creates a single instance of a servlet, it should be safe to reuse durable DAOs
    – kolossus
    Commented Apr 10, 2014 at 20:32
  • @kolossus: This is just a counter-example. You didn't state in your question that this was an actual problem, nor did you provide any relevant detail with respect to your actual problem, so it's not unreasonable to the casual observer to conclude that you're talking about a hypothetical. All it takes is a single counter-example to refute a premise. Commented Apr 10, 2014 at 20:40
  • An oversight on my part, sorry about that; It's a multithreaded webservice that connects to a popular ECM. Currently, each request to that service creates a new DAO, which in turn tries to get a new connection. My thinking is that a single DAO (or pool of DAOs), given the fact that there's no mutable state anywhere along the chain of command. What do you think?
    – kolossus
    Commented Apr 11, 2014 at 11:01
  • 1
    I think that running a profiler would provide a great deal of insight. Commented Apr 11, 2014 at 17:56

In addition to the practical insight from Robert Harvey's answer, I think it's important to keep in mind that big picture, both schemes are doing equivalent things. There will be some sort of queuing involved, regardless of which scheme you choose. The difference will be where that logic lives. If there is resource contention with providing connections to DAO objects, there would have been resource contention with allocating a DAO object to its consumer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.