Browse Java Design Patterns & Enterprise Application Architecture

DAO Best Practices and Anti-Patterns

Apply DAO in Java with focused APIs, clear exception boundaries, and disciplined transaction ownership while avoiding cargo-cult persistence layers.

DAO works well when it is small, intentional, and persistence-focused. It fails when it grows into a vague data layer that owns too much or too little.

Good DAO Practices

Keep The API Focused

Expose methods that reflect application use cases:

  • findOutstandingInvoices()
  • save(CustomerRecord)
  • deleteExpiredSessions()

Avoid generic “do everything” persistence interfaces unless the application truly benefits from them.

Translate Low-Level Failures

Higher layers should not need to reason about every SQLException, ORM exception, or vendor-specific failure type. Wrap or translate those errors into persistence-boundary exceptions that preserve the cause but present a stable interface upward.

Keep Mapping Logic Close To Persistence

Whether you use JDBC, JPA projections, or mappers, keep the mapping near the DAO instead of scattering it across services.

Be Clear About Transactions

DAO methods should fit the application’s transaction model. They should not silently create unpredictable transactional behavior in random places.

Common DAO Anti-Patterns

Generic CRUD Layer Everywhere

A generic base DAO can look efficient, but it often produces vague APIs and pushes important query meaning back into services.

Business Logic In The DAO

If the DAO starts deciding discounts, eligibility, or workflow state, it has crossed the persistence boundary.

Leaking Framework Types Upward

Returning ORM sessions, lazy proxy assumptions, or storage-specific abstractions from DAO code weakens the layer immediately.

One DAO Per Table With No Real Design Benefit

If the DAO layer mirrors the schema mechanically and adds no naming, query concentration, or boundary clarity, it is probably ceremony.

A Useful Review Standard

Ask whether the DAO layer makes the codebase easier to change in these ways:

  • query changes stay localized
  • services stay focused on policy
  • tests can substitute persistence behavior simply
  • framework details do not leak broadly

If the answer is no, the layer needs redesign, not defense.

Design Review Questions

When reviewing DAO quality in Java, ask:

  • Is the API specific enough to the application’s needs?
  • Are persistence exceptions handled consistently?
  • Does the DAO hide framework noise instead of re-exporting it?
  • Is any business policy embedded where it does not belong?

DAO is a good pattern when it clarifies persistence boundaries. It becomes an anti-pattern when it survives only as enterprise nostalgia.

Revised on Thursday, April 23, 2026