Spring Data Access By, Srinivas Reddy.S
Introduction to Spring JDBC
Multiple ways to connect to DB JDBC IBATIS Hibernate JPA JCA
Layered architecture client DB
DAO – Advantages and Best practices Advantages: Application is independent of the Data access techniques and database dependency. Offers loose coupling with the other layers of application. Allows to unit test the service layer using mock objects with out connecting to database.
DAO Layer Service Layer DAO Interfaces DAO Implementations DB
DAO – Advantages and Best practices Best practices to be followed: Always DAO classes should adhere to interfaces. Interfaces should expose the functionality that are needed by the Service layers. Keep the database related configurations outside the code as configurable items in an xml or properties file.
Problems with JDBC Poor Exception hierarchy – SQLException – DataTruncation – SQLWarning – BatchUpdateException Issues with JDBC Exceptions: Least explantive Defined as checked exceptions which mandate us to write the catch blocks
Spring Exception hirerarchy CannotAcquireLockException CannotSerializeTransactionException CleanupFailureDataAccessException ConcurrencyFailureException DataAccessException DataAccessResourceFailureException DataIntegrityViolationException DataRetrievalFailureException DeadlockLoserDataAccessException
Spring Exception hirerarchy EmptyResultDataAccessException IncorrectResultSizeDataAccessException IncorrectUpdateSemanticsDataAccessException InvalidDataAccessApiUsageException InvalidDataAccessResourceUsageException OptimisticLockingFailureException PermissionDeniedDataAccessException PessimisticLockingFailureException TypeMismatchDataAccessException UncategorizedDataAccessException
Spring Exception Hierarchy - Advantages More elaborate and detailed exceptions Exceptions not specific to an API. Can be used with any ORM and data access API. Declared as unchecked exceptions – No catch blocks needed.
Problems with JDBC – Data access Create a connection Create a statement Start the transaction Execute the statement Iterate the result set and get the values Commit the transaction/roll back when exception occurs Close the statement Close the connection
Spring JDBC – Template pattern Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithms structure.
Data access templates in Spring JdbcTemplate NamedParameterJdbcTemplate SimpleJdbcTemplate HibernateTemplate JpaTemplate SqlMapClientTemplate JdoTemplate CciTemplate
JDBCTemplate Dependencies Data source JDBC Template DAO Impl
Spring JDBC Configuration steps 1.Choose a data source type: 1.Data sources by JDBC API. DriverManagerDataSource SingleConnectionDataSource 2.Data sources looked up by JNDI. 3.Data sources that pool connections. – Using Apache commons DBCP – org.apache.commons.dbcp.BasicDataSource
Spring JDBC Configuration steps 2.Configure the Data Source
Spring JDBC Configuration steps 3. Configure the JdbcTemplate and add data source as dependency
Spring JDBC Configuration steps Register the Dao Impl class and add the JdbcTemplate as Dependency
JDBCTemplate Dependencies Data source JDBC Template DAO Impl
Thank you Follow Java9s on Twitter facebook: