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

1. java database connectivity (jdbc)
1. java database connectivity (jdbc)1. java database connectivity (jdbc)
1. java database connectivity (jdbc)Fad Zulkifli
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentationManav Prasad
 
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 Examplekamal kotecha
 
Hibernate
HibernateHibernate
HibernateAjay K
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentationJohn Slick
 
Spring (1)
Spring (1)Spring (1)
Spring (1)Aneega
 
Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Igor Anishchenko
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginnersRahul Jain
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate FrameworkRaveendra R
 

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 Access Databases Using Spring Framework

Complete java syllabus 7448062045 Yesdo Sddd
Complete java syllabus 7448062045 Yesdo SdddComplete java syllabus 7448062045 Yesdo Sddd
Complete java syllabus 7448062045 Yesdo SdddYesdo Softindia Pvt Ltd
 
Rollin onj Rubyv3
Rollin onj Rubyv3Rollin onj Rubyv3
Rollin onj Rubyv3Oracle
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2Haroon Idrees
 
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.pptkingkolju
 
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 TopLinkBill Lyons
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance JdbcSam Pattsin
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.pptDrMeenakshiS
 
Jdbc Lecture5
Jdbc Lecture5Jdbc Lecture5
Jdbc Lecture5phanleson
 
Advance java session 5
Advance java session 5Advance java session 5
Advance java session 5Smita B Kumar
 
Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise SpringEmprovise
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)Maher Abdo
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Luzan Baral
 

Similar to Access Databases Using Spring Framework (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

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 SimulatorGuo Albert
 
TOEIC 準備心得
TOEIC 準備心得TOEIC 準備心得
TOEIC 準備心得Guo Albert
 
DBM專案環境建置
DBM專案環境建置DBM專案環境建置
DBM專案環境建置Guo Albert
 
JPA Optimistic Locking With @Version
JPA Optimistic Locking With @VersionJPA Optimistic Locking With @Version
JPA Optimistic Locking With @VersionGuo Albert
 
OCEJPA Study Notes
OCEJPA Study NotesOCEJPA Study Notes
OCEJPA Study NotesGuo Albert
 
OCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation TipsOCEJPA(1Z0-898) Preparation Tips
OCEJPA(1Z0-898) Preparation TipsGuo Albert
 
JPA lifecycle events practice
JPA lifecycle events practiceJPA lifecycle events practice
JPA lifecycle events practiceGuo Albert
 
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 libraryGuo Albert
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errorsGuo 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 TableGuo 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 SystemGuo Albert
 
A Short Intorduction to JasperReports
A Short Intorduction to JasperReportsA Short Intorduction to JasperReports
A Short Intorduction to JasperReportsGuo 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 ImplementationGuo 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 objectGuo 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 generationGuo Albert
 
Nig project setup quickly tutorial
Nig project setup quickly tutorialNig project setup quickly tutorial
Nig project setup quickly tutorialGuo Albert
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplateGuo 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

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.pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
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 ...EduSkills OECD
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
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.pptxheathfieldcps1
 
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).pdfSoniaTolstoy
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
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 ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
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
 
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
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Access Databases Using Spring Framework

  • 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