Basic Idea:
The Term DAO stands for Data Access Objects. DAOs do basic function of interacting with the database.
They contain database read/write logic and expose their functionality to the rest of the application via interfaces as shown in the figure below.
The philosophy of coding to interfaces has a couple of things to note..
- Your service layer that invokes DAO is testable now as you can provide a mock implementation of your DAOs Interface without event connecting to the database.
- Your service layer accesses the database in a database-agnostic manner. It does not need to know what database is being invoked by the DAO class. All it cares about is the interface methods.
Spring's data access Exception hierarchy
Plain vanilla JDBC forces you to handle SQLException. SQLException means that something went wrong while you were trying to access the database. It is tell what went wrong and how to handle it. Some common reasons of SQLException being thrown are
- Application is not able to connect to the database.
- Query being executed has syntax errors.
- Table/Columns referred-to in the query doesn't exist.
- An attempt to insert/Update values in database that violate a database constraint.
Hey Bharat.. great work!
ReplyDelete