spring jdbc(1)
TRANSCRIPT
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 1/23
Tania Tritean
Presented by
Introduction to
Spring JDBC, DAO
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 2/23
• JDBC Advantages & Disadvantages
• DAO Pattern
• Spring JDBC
• Spring DAO•
Transactions• Transactions with Spring AOP
• Q & A
Agenda
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 3/23
JDBC Advantages
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 4/23
JDBC Disadvantages
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 5/23
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.
DAO Pattern
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 6/23
Get to the subject!!!!!!!
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 7/23
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)
Do the work for each iteration
Process any exception
Handle transactions
Close the connection
Spring JDBC
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 8/23
Define connection parameters /DataSource
Data sourceimplementation
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 9/23
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
Specify statement - JdbcTemplate
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 10/23
Examples
You can compare with:basic jdbc
What don’t you like here?
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 11/23
NamedParameterJdbcTemplate
adds support for programming JDBC statements using named parameters
Example:
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);
}
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 12/23
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);
}
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 13/23
takes advantage of JAVA 5 features
Varargs
Autoboxing
combines the most commonly used features of JdbcTemplate andNamedParameterJdbcTemplate
SimpleJdbcTemplate
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 14/23
Provides consistency in:
Exception Hierarchy
Why is this important?
translation from technology-specific exceptions such as Hibernate-relatedexceptions or JDBC-related exceptions by having its own hierarchy of data access-related exceptions
Abstract classes
Why is this important?
There is one abstract class each for various data persistence technologies. Theseabstract classes have methods that can be used to set the required configurableproperties that are specific to each technology.
DAO module of the Spring Framework
JdbcDaoSupport Abstraction for accessing data using JDBC
You provide the DataSource instance
JdbcDaoSupport provides the JdbcTemplate instance, created from thesupplied DataSource instanceto each technology.
it is provided as a convenience only
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 15/23
Definition:
All or Nothing / Commit or Rollback
Transactions
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 16/23
What can happen with multiple teams(transactions) trying to use same resources?
Conflicts can appear
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 17/23
T2 reads data that is being modified by T1 that has not yet committed.
Real example:
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 beencommitted.
A rollback can appear!
(maybe salary can not be modified – stupid example)
Conflict 1: Dirty Reads
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 18/23
a query returns data that would be different if the query were repeated withinthe same transaction.
Non-repeatable reads can occur when other transactions are modifying datathat a transaction is reading.
Real example:
1. Cheating on an exam! – response changed
2.
Empno has a salary = 00050
transaction A begins.
SELECT * FROM employee WHERE empno = '000090'
Transaction B begins.
UPDATE employee SET salary = 30100 WHERE empno = '000090'
Transaction B commit
Transaction A commit
Transaction A result will be? ………. Not up to date
Conflict 2: Non-Repeatable Reads
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 19/23
Records that appear in a set being read by another transaction.
can occur when other transactions insert rows that would satisfy the WHERE
clause of another transaction's statement. Real example:
1. Cheating on an exam! – multiple choice question
2.
Transaction A begins.
SELECT * FROM employee WHERE salary > 30000
Transaction B begins.
INSERT INTO employee (empno, firstnme, midinit, lastname, job, salary) VALUES('000350', 'NICK', 'A','GREEN','LEGAL COUNSEL',35000)
Transaction B commit
Transaction A commit
Result of Transaction A will be? ........... It will miss one row
Conflict 3: Phantom Reads
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 20/23
Isolation levels = the solution
TRANSACTION_SERIALIZABLE All problems fixed, but no concurrency any more
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
Solving Conflicts
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 21/23
Provides a consistent programming model across different transaction APIssuch as JTA, JDBC, Hibernate, iBATIS Database Layer and JDO.
Provides a simpler, easier to use, API for programmatic transactionmanagement than most of these transaction APIs
Integrates with the Spring data access abstraction
Supports Spring declarative transaction management
Transactions with SPRING
i i h
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 22/23
You have to
Define a transactionManager
Define Interceptor for adding transaction(use of AOP)
Eg. : <bean id=" transactionManager " class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="riskService" class=" org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean=" transactionManager " />
</property>
<property name="target">
<ref bean="riskServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
Transactions with SPRING AOP
7/31/2019 Spring JDBC(1)
http://slidepdf.com/reader/full/spring-jdbc1 23/23