SlideShare a Scribd company logo
1 of 31
Download to read offline
Database Access Using the
Spring Framework
What this presentation will be

   Brief overview of wiring applications using
    Spring
   Using Spring’s JDBC framework
   Spring’s transaction support
What this presentation will not cover

   In-depth coverage of Spring
   Creating web applications using Spring
Overview of wiring with Spring

   Discuss…
Concerns when accessing database

   Managing transactions
   Managing resources
       Connection pooling
       Cleaning up resources
Some typical JDBC code



      Code Example
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
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
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
So what do this mean in a JDBC context?

       DAO Template             Your code

   1. Prepare Resources   3. Execute Statement
   2. Start Transaction



   5. Commit/Rollback     4. Execute Statement
   6. Clean up resource
   7. Handle exceptions
But before we get too far ahead…
…let’s see how we
   Obtain/create a DataSource
   Wire a DataSource to our XxxDao classes
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>
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>
Working with DataSources



      Code Example
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
Some Spring JDBC callback interfaces

   PreparedStatementCreator
       Has one method –
        createPreparedStatement(Connection)
       Responsible for creating a PreparedStatement
       Does not need to handle SQLExceptions
Some Spring JDBC callback interfaces

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

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

   RowMapper
       Has one method – mapRow(ResultSet rs,
        int rowNum)
       Maps a single row to an Object
Using JdbcTemplate

   Central class for Spring JDBC framework
   Uses callbacks “under the covers”
   All you will need for most JDBC operations
Using JdbcTemplate



      Code Example
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
Using JdbcTemplate



      Code Example
Hibernate intro

   Open source ORM tool
   Very mature (version 3.2 on horizon)
   Feature-complete
       Caching
       Eager-fetching
       Lazy-loading
       Proxying
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
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
Using JdbcTemplate



      Code Example
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
Spring Proxy Overview



                    depends on
  SomeClient                        DaoInterface




         is wired                   delegates
                        ProxyBean                  DaoImpl
Using JdbcTemplate



      Code Example
Coming in Spring 2.0

   Support for AspectJ pointcut language
   JPA support?
   SimpleJdbcTemplate
       Support generics
       Support variable argument methods

More Related Content

What's hot

Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
backdoor
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
John Slick
 

What's hot (19)

Hibernate
HibernateHibernate
Hibernate
 
Jdbc connectivity in java
Jdbc connectivity in javaJdbc connectivity in java
Jdbc connectivity in java
 
1. java database connectivity (jdbc)
1. java database connectivity (jdbc)1. java database connectivity (jdbc)
1. java database connectivity (jdbc)
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
Hibernate
HibernateHibernate
Hibernate
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)
 
Hibernate in Nutshell
Hibernate in NutshellHibernate in Nutshell
Hibernate in Nutshell
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginners
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 

Similar to Spring db-access mod03

JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
kingkolju
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance Jdbc
Sam Pattsin
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 
Jdbc Lecture5
Jdbc Lecture5Jdbc Lecture5
Jdbc Lecture5
phanleson
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
Maher Abdo
 

Similar to Spring db-access mod03 (20)

Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 
Complete java syllabus 7448062045 Yesdo Sddd
Complete java syllabus 7448062045 Yesdo SdddComplete java syllabus 7448062045 Yesdo Sddd
Complete java syllabus 7448062045 Yesdo Sddd
 
Jdbc
JdbcJdbc
Jdbc
 
Rollin onj Rubyv3
Rollin onj Rubyv3Rollin onj Rubyv3
Rollin onj Rubyv3
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
 
JDBC
JDBCJDBC
JDBC
 
JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
EJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLinkEJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLink
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance Jdbc
 
Jdbc
Jdbc   Jdbc
Jdbc
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Jdbc Lecture5
Jdbc Lecture5Jdbc Lecture5
Jdbc Lecture5
 
Advance java session 5
Advance java session 5Advance java session 5
Advance java session 5
 
Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise Spring
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 

More from Guo Albert

JPA Optimistic Locking With @Version
JPA Optimistic Locking With @VersionJPA Optimistic Locking With @Version
JPA Optimistic Locking With @Version
Guo Albert
 
OCEJPA Study Notes
OCEJPA Study NotesOCEJPA Study Notes
OCEJPA Study Notes
Guo Albert
 
OCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation TipsOCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation Tips
Guo Albert
 
JPA lifecycle events practice
JPA lifecycle events practiceJPA lifecycle events practice
JPA lifecycle events practice
Guo Albert
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errors
Guo Albert
 
NIG系統報表開發指南
NIG系統報表開發指南NIG系統報表開發指南
NIG系統報表開發指南
Guo Albert
 
Ease Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History TableEase Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History Table
Guo Albert
 
NIG 系統開發指引
NIG 系統開發指引NIG 系統開發指引
NIG 系統開發指引
Guo Albert
 
NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟
Guo Albert
 
Form Bean Creation Process for NIG System
Form Bean Creation Process for NIG SystemForm Bean Creation Process for NIG System
Form Bean Creation Process for NIG System
Guo Albert
 
A Short Intorduction to JasperReports
A Short Intorduction to JasperReportsA Short Intorduction to JasperReports
A Short Intorduction to JasperReports
Guo Albert
 
Apply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationApply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report Implementation
Guo Albert
 
Utilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy objectUtilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy object
Guo Albert
 
Apply my eclipse to do entity class generation
Apply my eclipse to do entity class generationApply my eclipse to do entity class generation
Apply my eclipse to do entity class generation
Guo Albert
 
Nig project setup quickly tutorial
Nig project setup quickly tutorialNig project setup quickly tutorial
Nig project setup quickly tutorial
Guo Albert
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
Guo Albert
 

More from Guo Albert (20)

AWS IAM (Identity and Access Management) Policy Simulator
AWS IAM (Identity and Access Management) Policy SimulatorAWS IAM (Identity and Access Management) Policy Simulator
AWS IAM (Identity and Access Management) Policy Simulator
 
TOEIC 準備心得
TOEIC 準備心得TOEIC 準備心得
TOEIC 準備心得
 
DBM專案環境建置
DBM專案環境建置DBM專案環境建置
DBM專案環境建置
 
JPA Optimistic Locking With @Version
JPA Optimistic Locking With @VersionJPA Optimistic Locking With @Version
JPA Optimistic Locking With @Version
 
OCEJPA Study Notes
OCEJPA Study NotesOCEJPA Study Notes
OCEJPA Study Notes
 
OCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation TipsOCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation Tips
 
JPA lifecycle events practice
JPA lifecycle events practiceJPA lifecycle events practice
JPA lifecycle events practice
 
XDate - a modern java-script date library
XDate -  a modern java-script date libraryXDate -  a modern java-script date library
XDate - a modern java-script date library
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errors
 
NIG系統報表開發指南
NIG系統報表開發指南NIG系統報表開發指南
NIG系統報表開發指南
 
Ease Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History TableEase Your Effort of Putting Data into History Table
Ease Your Effort of Putting Data into History Table
 
NIG 系統開發指引
NIG 系統開發指引NIG 系統開發指引
NIG 系統開發指引
 
NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟NIG系統開發文件閱讀步驟
NIG系統開發文件閱讀步驟
 
Form Bean Creation Process for NIG System
Form Bean Creation Process for NIG SystemForm Bean Creation Process for NIG System
Form Bean Creation Process for NIG System
 
A Short Intorduction to JasperReports
A Short Intorduction to JasperReportsA Short Intorduction to JasperReports
A Short Intorduction to JasperReports
 
Apply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report ImplementationApply Template Method Pattern in Report Implementation
Apply Template Method Pattern in Report Implementation
 
Utilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy objectUtilize Commons BeansUtils to do copy object
Utilize Commons BeansUtils to do copy object
 
Apply my eclipse to do entity class generation
Apply my eclipse to do entity class generationApply my eclipse to do entity class generation
Apply my eclipse to do entity class generation
 
Nig project setup quickly tutorial
Nig project setup quickly tutorialNig project setup quickly tutorial
Nig project setup quickly tutorial
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

Spring db-access mod03

  • 1. Database Access Using the Spring Framework
  • 2. What this presentation will be  Brief overview of wiring applications using Spring  Using Spring’s JDBC framework  Spring’s transaction support
  • 3. What this presentation will not cover  In-depth coverage of Spring  Creating web applications using Spring
  • 4. Overview of wiring with Spring  Discuss…
  • 5. Concerns when accessing database  Managing transactions  Managing resources  Connection pooling  Cleaning up resources
  • 6. Some typical JDBC code Code Example
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. So what do this mean in a JDBC context? DAO Template Your code 1. Prepare Resources 3. Execute Statement 2. Start Transaction 5. Commit/Rollback 4. Execute Statement 6. Clean up resource 7. Handle exceptions
  • 11. But before we get too far ahead… …let’s see how we  Obtain/create a DataSource  Wire a DataSource to our XxxDao classes
  • 12. 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>
  • 13. 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>
  • 14. Working with DataSources Code Example
  • 15. 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
  • 16. Some Spring JDBC callback interfaces  PreparedStatementCreator  Has one method – createPreparedStatement(Connection)  Responsible for creating a PreparedStatement  Does not need to handle SQLExceptions
  • 17. Some Spring JDBC callback interfaces  SQLProvider  Has one method – getSql()  Typically implemented by PreparedStatementCreator implementers  Useful for debugging
  • 18. Some Spring JDBC callback interfaces  RowCallbackHandler  Has one method – processRow(ResultSet)  Called for each row in ResultSet  Typically stateful
  • 19. Some Spring JDBC callback interfaces  RowMapper  Has one method – mapRow(ResultSet rs, int rowNum)  Maps a single row to an Object
  • 20. Using JdbcTemplate  Central class for Spring JDBC framework  Uses callbacks “under the covers”  All you will need for most JDBC operations
  • 21. Using JdbcTemplate Code Example
  • 22. 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
  • 23. Using JdbcTemplate Code Example
  • 24. Hibernate intro  Open source ORM tool  Very mature (version 3.2 on horizon)  Feature-complete  Caching  Eager-fetching  Lazy-loading  Proxying
  • 25. 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
  • 26. 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
  • 27. Using JdbcTemplate Code Example
  • 28. 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
  • 29. Spring Proxy Overview depends on SomeClient DaoInterface is wired delegates ProxyBean DaoImpl
  • 30. Using JdbcTemplate Code Example
  • 31. Coming in Spring 2.0  Support for AspectJ pointcut language  JPA support?  SimpleJdbcTemplate  Support generics  Support variable argument methods