Documente Academic
Documente Profesional
Documente Cultură
Presented by
Tania Tritean
Agenda
JDBC Advantages & Disadvantages DAO Pattern Spring JDBC Spring DAO Transactions Transactions with Spring AOP Q&A
JDBC Advantages
JDBC Disadvantages
DAO Pattern
Used to:
abstract all access to the data source. encapsulate all access to the data source. manages the connection with the data source to obtain and store data.
Spring JDBC
Used to: Define connection parameters Open the connection Specify the statement Prepare and execute the statement Set up the loop to iterate through the results (if any)
JDBCTemplate:
central class in the JDBC core package handles the creation and release of resources handles exceptions You have just to: Provide the query Implement a Callback interface
Used to process the result of the query
Examples
NamedParameterJdbcTemplate
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; public void setDataSource(DataSource dataSource) { this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); } public int countOfActorsByFirstName(String firstName) { String sql = "select count(0) from T_ACTOR where first_name = :first_name"; SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName); return namedParameterJdbcTemplate.queryForInt(sql, namedParameters); }
SqlParameterSource
MapSqlParameterSource
BeanPropertySqlParameterSource: Example: public int countOfActors(Actor exampleActor) { // notice how the named parameters match the properties of the above 'Actor String sql = "select count(0) from T_ACTOR where first_name = :firstName and last_name = :lastName"; SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor); return this.namedParameterJdbcTemplate.queryForInt(sql, namedParameters); }
SimpleJdbcTemplate
Abstract classes
There is one abstract class each for various data persistence technologies. These abstract classes have methods that can be used to set the required configurable properties that are specific to each technology.
JdbcDaoSupport
Abstraction for accessing data using JDBC You provide the DataSource instance JdbcDaoSupport provides the JdbcTemplate instance, created from the supplied DataSource instanceto each technology. it is provided as a convenience only
Transactions
Definition:
What can happen with multiple teams (transactions) trying to use same resources?
1. Cheating on an exam! removing the response 2. Transaction A begins. UPDATE employee SET salary = 31650 WHERE empno = '000090' Transaction B begins. SELECT * FROM employee
(Transaction B sees data updated by transaction A. Those updates have not yet been committed. A rollback can appear! (maybe salary can not be modified stupid example)
Transaction B begins.
UPDATE employee SET salary = 30100 WHERE empno = '000090' Transaction B commit Transaction A commit
Solving Conflicts
TRANSACTION_REPEATABLE_READ
prevent only dirty reads and non-repeatable reads, but NOT phantoms
TRANSACTION_READ_COMMITTED
prevent only dirty reads Default!
TRANSACTION_READ_UNCOMMITTED
anything can happen
Provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, iBATIS Database Layer and JDO. Provides a simpler, easier to use, API for programmatic transaction management than most of these transaction APIs Integrates with the Spring data access abstraction Supports Spring declarative transaction management