Database Access Using the Spring Framework - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Database Access Using the Spring Framework

Description:

Currently a Software Technical Specialist for Countrywide ... Eager-fetching. Lazy-loading. Proxying. Using Hibernate. Configure classes to be mapped through ... – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 34
Provided by: springd
Category:

less

Transcript and Presenter's Notes

Title: Database Access Using the Spring Framework


1
Database Access Using the Spring Framework
  • Ryan Breidenbach
  • Countrywide Financial Corporation

2
What this presentation will be
  • Brief overview of wiring applications using
    Spring
  • Using Springs JDBC framework
  • Integrating Spring with Hibernate
  • Springs transaction support

3
What this presentation will not cover
  • In-depth coverage of Spring
  • Creating web applications using Spring
  • How to use Hibernate

4
Who am I
  • Currently a Software Technical Specialist for
    Countrywide
  • Developing Java applications for nearly seven
    years, mostly at Michaels Stores
  • Co-author of Spring in Action with Craig Walls

5
Overview of wiring with Spring
  • Discuss

6
Concerns when accessing database
  • Managing transactions
  • Managing resources
  • Connection pooling
  • Cleaning up resources

7
Some typical JDBC code
  • Code Example

8
The problem with traditional JDBC code
  • Its 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
  • Its redundant

9
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

10
Springs 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

11
So what do this mean in a JDBC context?
Your code
DAO Template
  • Prepare Resources
  • Start Transaction
  • Execute Statement
  • Commit/Rollback
  • Clean up resource
  • Handle exceptions
  • Execute Statement

12
But before we get too far aheadlets see how we
  • Obtain/create a DataSource
  • Wire a DataSource to our XxxDao classes

13
Working with DataSources
  • Getting a DataSource from JNDI
  • ltbean iddataSource
  • classorg.springframework.jndi.JndiObjectFa
    ctoryBeangt
  • ltproperty namejndiName
  • valuejavacomp/env/jdbc/MyDataSource
    Name/gt
  • lt/beangt

14
Working with DataSources
  • Creating a connection pool
  • ltbean iddataSource classorg.apache.commons.db
    cp.BasicDataSourcegt
  • ltproperty name"url valuejdbcmysql//localho
    st/demo/gtgt
  • ltproperty name"driverClassName
    valuecom.mysql.jdbc.Driver/gt
  • ltproperty name"username valuetest/gt
  • ltproperty namepassword valuepassword/gt
  • lt/beangt

15
Working with DataSources
  • Code Example

16
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

17
Some Spring JDBC callback interfaces
  • PreparedStatementCreator
  • Has one method createPreparedStatement(Connectio
    n)
  • Responsible for creating a PreparedStatement
  • Does not need to handle SQLExceptions

18
Some Spring JDBC callback interfaces
  • SQLProvider
  • Has one method getSql()
  • Typically implemented by PreparedStatementCreator
    implementers
  • Useful for debugging

19
Some Spring JDBC callback interfaces
  • RowCallbackHandler
  • Has one method processRow(ResultSet)
  • Called for each row in ResultSet
  • Typically stateful

20
Some Spring JDBC callback interfaces
  • RowMapper
  • Has one method mapRow(ResultSet rs, int rowNum)
  • Maps a single row to an Object

21
Using JdbcTemplate
  • Central class for Spring JDBC framework
  • Uses callbacks under the covers
  • All you will need for most JDBC operations

22
Using JdbcTemplate
  • Code Example

23
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

24
Using JdbcTemplate
  • Code Example

25
Hibernate intro
  • Open source ORM tool
  • Very mature (version 3.2 on horizon)
  • Feature-complete
  • Caching
  • Eager-fetching
  • Lazy-loading
  • Proxying

26
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

27
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

28
Using JdbcTemplate
  • Code Example

29
Spring Transaction Management
  • Supports programmatic (yuck!) and declarative
    (yeah!) transactions
  • Declarative transaction management achieved via
    Springs AOP
  • Declarative transactions can be defined in Spring
    configuration file or in annotations
  • Supports many transaction properties
  • Propagation
  • Isolation level
  • Rollback conditions

30
Spring Proxy Overview
depends on
is wired
delegates
31
Using JdbcTemplate
  • Code Example

32
Coming in Spring 2.0
  • Support for AspectJ pointcut language
  • JPA support?
  • SimpleJdbcTemplate
  • Support generics
  • Support variable argument methods

33
Questions Answers
  • ryan_breidenbach_at_yahoo.com
Write a Comment
User Comments (0)
About PowerShow.com