spring jdbc(1)

23
Tania Tritean Presented by Introduction to Spring JDBC, DAO

Upload: andrei-chirila

Post on 05-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spring JDBC(1)

7/31/2019 Spring JDBC(1)

http://slidepdf.com/reader/full/spring-jdbc1 1/23

Tania Tritean

Presented by

Introduction to

Spring JDBC, DAO

Page 2: Spring JDBC(1)

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

Page 3: Spring JDBC(1)

7/31/2019 Spring JDBC(1)

http://slidepdf.com/reader/full/spring-jdbc1 3/23

 

JDBC Advantages

Page 4: Spring JDBC(1)

7/31/2019 Spring JDBC(1)

http://slidepdf.com/reader/full/spring-jdbc1 4/23

JDBC Disadvantages

Page 5: Spring JDBC(1)

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

Page 6: Spring JDBC(1)

7/31/2019 Spring JDBC(1)

http://slidepdf.com/reader/full/spring-jdbc1 6/23

Get to the subject!!!!!!!

Page 7: Spring JDBC(1)

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

Page 8: Spring JDBC(1)

7/31/2019 Spring JDBC(1)

http://slidepdf.com/reader/full/spring-jdbc1 8/23

Define connection parameters /DataSource

Data sourceimplementation

Page 9: Spring JDBC(1)

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 

Page 10: Spring JDBC(1)

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? 

Page 11: Spring JDBC(1)

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);

}

Page 12: Spring JDBC(1)

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);

}

Page 13: Spring JDBC(1)

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

Page 14: Spring JDBC(1)

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

Page 15: Spring JDBC(1)

7/31/2019 Spring JDBC(1)

http://slidepdf.com/reader/full/spring-jdbc1 15/23

 

Definition:

All or Nothing / Commit or Rollback

Transactions

Page 16: Spring JDBC(1)

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

Page 17: Spring JDBC(1)

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

Page 18: Spring JDBC(1)

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

Page 19: Spring JDBC(1)

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

Page 20: Spring JDBC(1)

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

Page 21: Spring JDBC(1)

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

Page 22: Spring JDBC(1)

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

Page 23: Spring JDBC(1)

7/31/2019 Spring JDBC(1)

http://slidepdf.com/reader/full/spring-jdbc1 23/23