P. 1
Spring JDBC(1)

Spring JDBC(1)

|Views: 5|Likes:
Published by Andrei Chirila

More info:

Published by: Andrei Chirila on Jun 13, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less





Introduction to Spring JDBC, DAO

Presented by

Tania Tritean


• • • • • • •

JDBC Advantages & Disadvantages DAO Pattern Spring JDBC Spring DAO Transactions Transactions with Spring AOP Q&A

JDBC Advantages .

JDBC Disadvantages .

encapsulate all access to the data source. .DAO Pattern Used to:    abstract 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 .

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 .Specify statement .

Examples You can compare with: basic jdbc What don’t you like here? .

namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource). namedParameters).queryForInt(sql. public void setDataSource(DataSource dataSource) { this. firstName). return namedParameterJdbcTemplate. } public int countOfActorsByFirstName(String firstName) { String sql = "select count(0) from T_ACTOR where first_name = :first_name". } .NamedParameterJdbcTemplate   adds support for programming JDBC statements using named parameters Example: private NamedParameterJdbcTemplate namedParameterJdbcTemplate. SqlParameterSource namedParameters = new MapSqlParameterSource("first_name".

queryForInt(sql. } . return this.namedParameterJdbcTemplate. SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor).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". namedParameters).

SimpleJdbcTemplate  takes advantage of JAVA 5 features   Varargs Autoboxing  combines the most commonly used features of JdbcTemplate and NamedParameterJdbcTemplate .

These abstract classes have methods that can be used to set the required configurable properties that are specific to each technology. it is provided as a convenience only .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. created from the supplied DataSource instanceto each technology. JdbcDaoSupport  Abstraction for accessing data using JDBC    You provide the DataSource instance JdbcDaoSupport provides the JdbcTemplate instance.

Transactions  Definition: All or Nothing / Commit or Rollback .

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

Those updates have not yet been committed. UPDATE employee SET salary = 31650 WHERE empno = '000090' Transaction B begins. Cheating on an exam! – removing the response 2.Conflict 1: Dirty Reads   T2 reads data that is being modified by T1 that has not yet committed. A rollback can appear! (maybe salary can not be modified – stupid example) . Transaction A begins. Real example: 1. SELECT * FROM employee (Transaction B sees data updated by transaction A.

Real example: Cheating on an exam! – response changed Transaction B begins. SELECT * FROM employee WHERE empno = '000090' a query returns data that would be different if the query were repeated within the same transaction.Conflict 2: Non-Repeatable Reads    1. Empno has a salary = 00050 transaction A begins. Not up to date . Non-repeatable reads can occur when other transactions are modifying data that a transaction is reading. UPDATE employee SET salary = 30100 WHERE empno = '000090' Transaction B commit Transaction A commit Transaction A result will be? ………. 2.

Conflict 3: Phantom Reads   Records that appear in a set being read by another transaction. It will miss one row .... 'NICK'. 'A'.35000) Real example: Cheating on an exam! – multiple choice question Transaction B commit Transaction A commit Result of Transaction A will be? . SELECT * FROM employee WHERE salary > 30000 Transaction B begins...'LEGAL COUNSEL'. firstnme. can occur when other transactions insert rows that would satisfy the WHERE clause of another transaction's statement. INSERT INTO employee (empno.  1. Transaction A begins. 2. midinit.. lastname.'GREEN'... salary) VALUES ('000350'. job...

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 .

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    . JDBC.Transactions with SPRING  Provides a consistent programming model across different transaction APIs such as JTA. iBATIS Database Layer and JDO. Hibernate. Provides a simpler.

: <bean id="transactionManager" class="org.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) .Transactions with SPRING AOP You have to   Eg.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="riskService" class="org.datasource.interceptor.springframework.springframework.jdbc.transaction.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->