Sie sind auf Seite 1von 23

Introduction to Spring JDBC, DAO

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.

Get to the subject!!!!!!!

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)

Do the work for each iteration


Process any exception Handle transactions Close the connection

Define connection parameters / DataSource


Data source implementation

Specify statement - JdbcTemplate

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

You can compare with: basic jdbc

What dont you like here?

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

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

takes advantage of JAVA 5 features Varargs Autoboxing

combines the most commonly used features of JdbcTemplate and NamedParameterJdbcTemplate

DAO module of the Spring Framework


Provides consistency in: Exception Hierarchy
Why is this important? translation from technology-specific exceptions such as Hibernate-related exceptions or JDBC-related exceptions by having its own hierarchy of data accessrelated exceptions Why is this important?

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:

All or Nothing / Commit or Rollback

What can happen with multiple teams (transactions) trying to use same resources?

Conflicts can appear

Conflict 1: Dirty Reads


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 been committed. A rollback can appear! (maybe salary can not be modified stupid example)

Conflict 2: Non-Repeatable Reads


1. 2. Empno has a salary = 00050 transaction A begins. SELECT * FROM employee WHERE empno = '000090' a query returns data that would be different if the query were repeated within the same transaction. Non-repeatable reads can occur when other transactions are modifying data that a transaction is reading. Real example: Cheating on an exam! response changed

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 3: Phantom Reads


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. 1. 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) Real example: Cheating on an exam! multiple choice question

Transaction B commit Transaction A commit

Result of Transaction A will be? ........... It will miss one row

Solving Conflicts

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

Transactions with SPRING

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

Transactions with SPRING AOP


You have to 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>

Define a transactionManager Define Interceptor for adding transaction(use of AOP)

Das könnte Ihnen auch gefallen