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

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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 3JemimahLaneBuaron
 
“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
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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 .pdfchloefrazer622
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
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
 
“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...
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
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
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

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