spring db-access mod03
TRANSCRIPT
![Page 1: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/1.jpg)
Database Access Using the
Spring Framework
![Page 2: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/2.jpg)
What this presentation will be
Brief overview of wiring applications using
Spring
Using Spring’s JDBC framework
Spring’s transaction support
![Page 3: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/3.jpg)
What this presentation will not cover
In-depth coverage of Spring
Creating web applications using Spring
![Page 4: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/4.jpg)
Overview of wiring with Spring
Discuss…
![Page 5: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/5.jpg)
Concerns when accessing database
Managing transactions
Managing resources
Connection pooling
Cleaning up resources
![Page 6: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/6.jpg)
Some typical JDBC code
Code Example
![Page 7: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/7.jpg)
The problem with traditional JDBC code
It’s redundant
It repeats itself quite a bit
The exact same code appears in lots of
places
You find yourself doing the same things over
and over again
It’s redundant
![Page 8: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/8.jpg)
The problem with redundant code
Violates the DRY principle
Maintenance nightmare since JDBC code is
inherently messy
JDBC code is critical, so it is important to get
it right, once
![Page 9: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/9.jpg)
Spring’s solution – The Template Pattern
Template Pattern – “Define the skeleton of an
algorithm in an operation, deferring some
steps to subclasses. Template Method lets
subclasses redefine certain steps of an
algorithm without changing the algorithm's
structure.”
Uses “callbacks” for implementation specific
tasks
![Page 10: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/10.jpg)
Your codeDAO Template
So what do this mean in a JDBC context?
1. Prepare Resources
2. Start Transaction
5. Commit/Rollback
6. Clean up resource
7. Handle exceptions
3. Execute Statement
4. Execute Statement
![Page 11: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/11.jpg)
But before we get too far ahead…
…let’s see how we
Obtain/create a DataSource
Wire a DataSource to our XxxDao classes
![Page 12: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/12.jpg)
Working with DataSources
Getting a DataSource from JNDI
<bean id=“dataSource”
class=“org.springframework.jndi.JndiObjectFactoryBean”>
<property name=“jndiName”
value=“java:comp/env/jdbc/MyDataSourceName”/>
</bean>
![Page 13: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/13.jpg)
Working with DataSources
Creating a connection pool
<bean id=“dataSource” class=“org.apache.commons.dbcp.BasicDataSource”>
<property name="url“ value=“jdbc:mysql://localhost/demo”/>>
<property name="driverClassName“ value=“com.mysql.jdbc.Driver”/>
<property name="username” value=“test”/>
<property name=“password” value=“password”/>
</bean>
![Page 14: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/14.jpg)
Working with DataSources
Code Example
![Page 15: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/15.jpg)
Overview of Spring JDBC features
As mentioned, provides framework so that
Spring manages:
Resources
Transactions
Exceptions
Consistent exception hierarchy
Subclass RuntimeException
Specific and meaningful (no vendor error codes!)
Extensable
![Page 16: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/16.jpg)
Some Spring JDBC callback interfaces
PreparedStatementCreator
Has one method –createPreparedStatement(Connection)
Responsible for creating a PreparedStatement
Does not need to handle SQLExceptions
![Page 17: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/17.jpg)
Some Spring JDBC callback interfaces
SQLProvider
Has one method – getSql()
Typically implemented by PreparedStatementCreator implementers
Useful for debugging
![Page 18: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/18.jpg)
Some Spring JDBC callback interfaces
RowCallbackHandler
Has one method – processRow(ResultSet)
Called for each row in ResultSet
Typically stateful
![Page 19: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/19.jpg)
Some Spring JDBC callback interfaces
RowMapper
Has one method – mapRow(ResultSet rs,
int rowNum)
Maps a single row to an Object
![Page 20: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/20.jpg)
Using JdbcTemplate
Central class for Spring JDBC framework
Uses callbacks “under the covers”
All you will need for most JDBC operations
![Page 21: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/21.jpg)
Using JdbcTemplate
Code Example
![Page 22: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/22.jpg)
Spring Incrementers
Used to increment primary key value for
newly persisted object
Implements DataFieldMaxValueIncrementer
Supports
Oracle sequences
DB2 sequences
PostgreSQL sequences
MySQL for non-auto-increment columns
![Page 23: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/23.jpg)
Using JdbcTemplate
Code Example
![Page 24: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/24.jpg)
Hibernate intro
Open source ORM tool
Very mature (version 3.2 on horizon)
Feature-complete
Caching
Eager-fetching
Lazy-loading
Proxying
![Page 25: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/25.jpg)
Using Hibernate
Configure classes to be mapped through
Manually created configuration files
XDoclet generated configuration files
Annotations (JPA and Hibernate)
Configure global properties through hibernate.properties file
Hibernate class analogies
DataSource : SessionFactory
Connection : Session
![Page 26: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/26.jpg)
Using Spring with Hibernate
Use Spring to configure Hibernate
Configure Hibernate mappings
Configure Hibernate properties
Wire dependant object to SessionFactory
Use HibernateTemplate as abstraction to Hibernate API Manages obtaining Session from SessionFactory
Handles/converts exceptions
Manages transactions
![Page 27: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/27.jpg)
Using JdbcTemplate
Code Example
![Page 28: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/28.jpg)
Spring Transaction Management
Supports programmatic (yuck!) and declarative
(yeah!) transactions
Declarative transaction management achieved via
Spring’s AOP
Declarative transactions can be defined in Spring
configuration file or in annotations
Supports many transaction properties
Propagation
Isolation level
Rollback conditions
![Page 29: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/29.jpg)
Spring Proxy Overview
SomeClient DaoInterface
DaoImplProxyBeanis wired
depends on
delegates
![Page 30: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/30.jpg)
Using JdbcTemplate
Code Example
![Page 31: Spring db-access mod03](https://reader033.vdocuments.net/reader033/viewer/2022060108/554f5c2eb4c905c8088b46f8/html5/thumbnails/31.jpg)
Coming in Spring 2.0
Support for AspectJ pointcut language
JPA support?
SimpleJdbcTemplate
Support generics
Support variable argument methods