Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007.

Similar presentations


Presentation on theme: "Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007."— Presentation transcript:

1 Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007.

2 2 Spring support The Spring Framework integrates with all popular Java data- access frameworks Hibernate 2 and 3 (LGPL) iBATIS (Apache license, partial ORM implementation) Java Data Objects (JDO) 1 and 2 (specifications with commercial and open source implementations) Oracle TopLink (commercial) Apache ObjectRelationalBridge (OJB) (Apache license) EJB3 persistence (specification with commercial and open- source implementations, also called Java Persistence API, or JPA)

3 3 Effects of Data-Access Leakage Data-access exceptions Database resource management Application design

4 4 Database Resources Data-access code can show three forms of leakage when dealing with database resources: Resource exhaustion Poor performance Inappropriate connection life cycles reusing a Connection object that was created by another party without closing it.

5 5 JdbcTemplate JdbcTemplate is a thread-safe data access broker class for working with JDBC Resource exhaustion: All JDBC resources will be properly closed to avoid resource exhaustion. Poor performance: JdbcTemplate uses PreparedStatement for variable SQL statements. Inappropriate connection life cycles: JdbcTemplate will effectively check if an existing Connection object is available for reuse and will reuse it in a thread-safe way. If no such object can be found, it will obtain a Connection object from the DataSource object and close it again. Uninformative exceptions: JdbcTemplate will always translate SQLException to a more specific exception in an exception hierarchy. This exception will contain the SQL statement that caused the error and the full stack trace of the SQLException.

6 6 JdbcTemplate

7 7 Using Hibernate templates The main interface for interacting with Hibernate is org.hibernate.Session Provides basic data access functionality such as the ability to save, update, delete, and load objects from the database LocalSessionFactoryBean is a Spring factory bean that produces a local Hibernate SessionFactory instance that draws its mapping metadata from one or more XML mapping files

8 8 Configure Hibernate 3

9 9 Choosing a version of Hibernate Spring-Hibernate integration classes needed to import classes from either net.sf.hibernate or org.hibernate, they were presented with the choice of either: Dropping support for Hibernate 2, supporting only Hibernate 3 going forward The Hibernate 2 support is found within the Spring distribution under the org.springframework.orm.hibernate package. Splitting the Spring-Hibernate support code into two strains—one for Hibernate 2 and one for Hibernate 3 Hibernate 3 support under the org.springframework.orm.hibernate3 package

10 10 Using Hibernate templates Spring’s HibernateTemplate provides an abstract layer over a Hibernate Session. HibernateTemplate’s main responsibility is to simplify the work of opening and closing Hibernate Sessions and to convert Hibernate-specific exceptions to one of the Spring ORM exceptions

11 11 Hibernate sessionFactory The sessionFactory property takes a reference to an implementation of org.hibernate.SessionFactory. Here you have a few options, depending on how you use Hibernate to map your objects to database tables.

12 12 Using classic Hibernate mapping files If you are using Hibernate’s classic XML mapping files, you’ll want to use Spring’s LocalSessionFactoryBean Id(INTEGER)Description (varchar(50))

13 13 Connection to Database (HSQL DB) NOTE: For HSQLDB database run the following script to start on network mode in the c:\hsqldb\data directory. java -cp../lib/hsqldb.jar org.hsqldb.Server -database.0 file:springdb -dbname.0 sales

14 14 orderDao

15 15 HibernateDAO

16 16 Client Application

17 17 Thank You

18 18 USA INDIA SRI LANKA UK www.virtusa.com © V I r t u s a C o r p o r a t i o n "Virtusa" is a trademark of the company and a registered trademark in the EU and In India. "Productization" is a service mark of the company and a registered service mark in the United States. "vRule" is a service mark of the company. For more information please contact SalesInquiries@virtusa.com


Download ppt "Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007."

Similar presentations


Ads by Google