Professional Documents
Culture Documents
Agenda
Problems with plain JDBC interaction Spring way of handling database access
Database Connectivity
Any non trivial enterprise application usually have a persistence mechanism and usually its a SQL compliant database. Spring does not provide any native support for database access. Spring provides integration to various data access framework which includes JDBC Hibernate iBatis Apache Object Relationship Bridge JPA Toplink
Database Connectivity
In spite of SQL standards there are variation in the dialect support by different vendors. Database resources need to be managed Executing and managing connections to database is a non trivial process. Developer has to manage transaction boundaries.
Database Connectivity
Curse of SQLException Unable to connect to database. Query being performed has syntax error. Tables/Columns do not exist Violates database constraint Cant do much about it.
DAO
Spring promotes DAO pattern for data access. This promotes exposing functionality through interfaces
Service Object
DAO interface
DAO Implementation
DAO
Programming against DAO interface will shield us from implementation details of underlying data access code
public interface employeeDao{ public void saveEmployee(Employee employee); }
DAO
No matter which data access technology is used it has a fixed part and a programmer defined part. Spring handles the data access using templates and callbacks. The fixed part like opening closing connection is handled by template and the variable part like how to handle the result is handled by callbacks.
Spring comes with several data access template for different persistence mechanism. Some of which are: JdbcTemplate SimpleJdbcTemplate HibernateTemplate JpaTemplate
JdbcTemplate
JdbcTemplate class simplifies working with JDBC It will automatically handle resource management, exception handling and transaction management It is a thread safe class so you can use a single instance that many classes can use. It also gives access to Connection with getConnection method
JdbcTemplate
public class EmployeeJdbcDao implements EmployeeDao{ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate template){ jdbcTemplate = tempalte; }
JdbcTemplate
public void saveEmployee(Employee employee) { jdbcTemplate.update ("insert into Employee (name) values (?), employee.getName()); }
JdbcTemplate
JdbcTemplate
Configure dataSource
<bean id="dataSource" class="org.springframework.jdbc.datasource. DriverManagerDataSource> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url value="jdbc:hsqldb:hsql://localhost" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean>
Collection can be fetched and list of proper objects will get created
@Override public List<Employee> getEmployees() { return jdbcTemplate.query ("Select name as name, salary as salary from Employee", new ParameterizedRowMapper<Employee>(){ public Employee mapRow(ResultSet rs,int rowNum) throws SQLException { Employee employee = new Employee(); employee.setName(rs.getString("name")); employee.setSalary(rs.getInt("salary")); return employee;
Accessing Datasource
<bean id =datasource class=org.springframework.jndi.JndiObjectFactoryBean> <property name=jndiName value=/jdbc/ds/> <property name=resourceRef value=true/> </bean>
Exception Handling
Handling of Exceptions is a pain as we have seen Spring Exceptions are unchecked so no need to catch them. Spring throws finer exception which gives better insight into problem The root of Spring exception hierarchy is DataAccessException
Conclusion
Problems with plain JDBC interaction Spring way of handling database access