Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Chapter 10: Designing Databases
Database System Concepts and Architecture
Spring, Hibernate and Web Services 13 th September 2014.
.NET Database Technologies: Open-Source Frameworks.
Distributed Application Development B. Ramamurthy.
Object-Oriented Enterprise Application Development J2EE Blueprints.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Session-02.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
JDBC Session 5 Tonight: Data Access Patterns 1.J2EE Architecture & Design Patterns 2.The Data Access Tier 3.Data Access Patterns –DataAccessObject (DAO)
Maven for building Java applications By Nalin De Zoysa
Database Programming in Java Corresponds with Chapter 32, 33.
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Introduction to SQL Steve Perry
Data File Access API : Under the Hood Simon Horwith CTO Etrilogy Ltd.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
1 Another group of Patterns Architectural Patterns.
PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 4th Edition Copyright © 2009 John Wiley & Sons, Inc. All rights.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
Object-to-Relational Mapping: The Crossing Chasms Pattern and Implementation Considerations Use of Meta Data in the Java Persistence Layer Presented by.
COMP 321 Week 5. Overview SQL Injection Core J2EE Patterns Lab 5-2 Introduction Exam Review.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Lecture # 3 & 4 Chapter # 2 Database System Concepts and Architecture Muhammad Emran Database Systems 1.
1 Mapping to Relational Databases Presented by Ramona Su.
Domain and Persistence Patterns. Fundamental Pattern Types Design Patterns Business Logic Patterns.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
3-Tier Client/Server Internet Example. TIER 1 - User interface and navigation Labeled Tier 1 in the following graphic, this layer comprises the entire.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
آرمان حسين‌زاده آذر  Access to data varies depending on the source of the data.  Access to persistent storage, such as to a database, varies greatly.
Presentation & Business Tier Design Patterns Pearce.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Persistent State Service 1  Concept  Persistence is the ability of an object state to survive termination of the process in which the object executes.
The Data Access Object Pattern (Structural – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Object storage and object interoperability
Advanced Object-oriented Design Patterns Creational Design Patterns.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Basics of JDBC Session 14.
SEA Side – Extreme Programming 1 SEA Side Software Engineering Annotations Architectural Patterns Professor Sara Stoecklin Director of Software Engineering-
PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 5th Edition Copyright © 2015 John Wiley & Sons, Inc. All rights.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Spring JDBC Dima Ionut Daniel. Contents What is Spring JDBC? Overview Spring JDBC Core SQL Exceptions Database Connection Batch Operations Handling BLOB/CLOB.
In the Name Of Almighty Allah. Java Application Connection To Mysql Created by Hasibullah (Sahibzada) Kabul Computer Science Faculty Afghanistan.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
1 Copyright © 2008, Oracle. All rights reserved. Repository Basics.
Data Access Patterns Problems with data access from OO programs: 1.The object-relational impedance mismatch 2.Decoupling domain logic from database technology.
DEPTT. OF COMP. SC & APPLICATIONS
ODBC, OCCI and JDBC overview
POOL persistency framework for LHC
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Database System Concepts and Architecture.
Data Model.
Java Database Connectivity
Entity Beans B.Ramamurthy 2/17/2019 BR.
Developing and testing enterprise Java applications
Presentation transcript:

Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling domain logic from database technology

Problems in Data Access (2) Access to data varies depending on the source of the data. type of storage (relational databases, object-oriented databases, flat files, etc) particular vendor implementation for a type. When business components need to access a data source, they can use the appropriate API to achieve connectivity and manipulate the data source. Problem: coupling between the components and the data source implementation: appears when including the connectivity and data access code provided by different API’s within the business components (domain objects) Such code dependencies in components make it difficult to migrate the application from one type of data source to another Components need to be transparent to the actual persistent store or data source implementation to provide easy migration to different vendor products, different storage types, and different data source types.

Bibliography – Sun: Core J2EE Pattern Catalog htmlhttp:// html – For further reading, only if interested: Clifton Nock, Data Access Patterns: Database Interactions in Object Oriented Applications, Addison Wesley, 2003 Martin Fowler, Patterns of Enterprise Application Architecture

The Data Access Object Pattern Intent: Abstract and Encapsulate all access to the data source Sun Developer Network - Core J2EE Patterns

DAO – Participants and responsibilities BusinessObject: represents the data client. It is the object that requires access to the data source to obtain and store data. DataAccessObject: the primary object of this pattern. It abstracts the underlying data access implementation for the BusinessObject to enable transparent access to the data source. The BusinessObject also delegates data load and store operations to the DataAccessObject. DataSource: represents a data source implementation. A data source could be a database such as an RDBMS, OODBMS, XML repository, flat file system, etc. Transfer Object: used as a data carrier. The DataAccessObject may use a Transfer Object to return data to the client. The DataAccessObject may also receive the data from the client in a Transfer Object to update the data in the data source.

Where to get DAO’s from ? Strategies to get DAO’s: –Automatic DAO Code Generation Strategy –Factory for Data Access Objects Strategy

Automatic DAO Code Generation Strategy Since each BusinessObject corresponds to a specific DAO, it is possible to establish relationships between the BusinessObject, DAO, and underlying implementations (such as the tables in an RDBMS). Once the relationships are established, it is possible to write a simple application-specific code-generation utility that generates the code for all DAOs required by the application. The metadata to generate the DAO: – can come from a developer-defined descriptor file. – or, alternatively, the code generator can automatically introspect the database and provide the necessary DAOs to access the database. If the requirements for DAOs are sufficiently complex, consider using third-party tools that provide object-to-relational mapping for RDBMS databases. – Examples: Java Persistence API (JPA): Hibernate

Factory for Data Access Objects Strategy The DAO pattern can be made highly flexible by adopting the Abstract Factory [GoF] and the Factory Method [GoF] patterns Use Factory Method: When the underlying storage is not subject to change from one implementation to another, use the Factory Method pattern to produce a number of DAOs needed by the application. Use Abstract Factory: When the underlying storage is subject to change from one implementation to another, this strategy may be implemented using the Abstract Factory pattern. In this case, this strategy provides an abstract DAO factory object (Abstract Factory) that can construct various types of concrete DAO factories, each factory supporting a different type of persistent storage implementation. Once you obtain the concrete DAO factory for a specific implementation, you use it to produce DAOs supported and implemented in that implementation.

DAO with Factory Method

DAO with Abstract Factory

Consequences Enables Transparency Enables Easier Migration. Reduces Code Complexity in Business Objects: Adds Extra Layer Needs Class Hierarchy Design

Example

Example- Using Factory Method

Example- Using Abstract Factory

Abstract class DAO Factory // Abstract class DAO Factory public abstract class DAOFactory { // List of DAO types supported by the factory public static final int CLOUDSCAPE = 1; public static final int ORACLE = 2; public static final int SYBASE = 3;... // There will be a method for each DAO that can be created. //The concrete factories will have to implement these methods. public abstract CustomerDAO getCustomerDAO(); public abstract AccountDAO getAccountDAO(); public abstract OrderDAO getOrderDAO();... public static DAOFactory getDAOFactory( int whichFactory) { switch (whichFactory) { case CLOUDSCAPE: return new CloudscapeDAOFactory(); case ORACLE : return new OracleDAOFactory();... default : return null; }

Cloudscape concrete DAO Factory implementation import java.sql.*; public class CloudscapeDAOFactory extends DAOFactory { public static final String DRIVER= "COM.cloudscape.core.RmiJdbcDriver"; public static final String DBURL= "jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB"; // method to create Cloudscape connections public static Connection createConnection() { // Use DRIVER and DBURL to create a connection // Recommend connection pool implementation/usage } public CustomerDAO getCustomerDAO() { // CloudscapeCustomerDAO implements CustomerDAO return new CloudscapeCustomerDAO(); } public AccountDAO getAccountDAO() { // CloudscapeAccountDAO implements AccountDAO return new CloudscapeAccountDAO(); } public OrderDAO getOrderDAO() { // CloudscapeOrderDAO implements OrderDAO return new CloudscapeOrderDAO(); }... }

Interface that all CustomerDAOs must support // Interface that all CustomerDAOs must support public interface CustomerDAO { public int insertCustomer(...); public boolean deleteCustomer(...); public Customer findCustomer(...); public boolean updateCustomer(...); public RowSet selectCustomersRS(...); public Collection selectCustomersTO(...);... }

CloudscapeCustomerDAO implementation // CloudscapeCustomerDAO implementation of the CustomerDAO interface. // This class can contain all Cloudscape specific code and SQL statements. import java.sql.*; public class CloudscapeCustomerDAO implements CustomerDAO { public CloudscapeCustomerDAO() { // initialization } // The following methods can use CloudscapeDAOFactory.createConnection() // to get a connection as required public int insertCustomer(...) { // Implement insert customer here. // Return newly created customer number or a -1 on error } public boolean deleteCustomer(...) { // Implement delete customer here // Return true on success, false on failure } public Customer findCustomer(...) { // Implement find a customer here using supplied argument values as search criteria // Return a Transfer Object if found, return null on error or if not found }

Customer Transfer Object public class Customer implements java.io.Serializable { // member variables int CustomerNumber; String name; String streetAddress; String city;... // getter and setter methods }

Client Code // Create a DAO CustomerDAO custDAO = cloudscapeFactory.getCustomerDAO(); // create a new customer int newCustNo = custDAO.insertCustomer(...); // Find a customer object. Get the Transfer Object. Customer cust = custDAO.findCustomer(...); // modify the values in the Transfer Object. cust.setAddress(...); cust.set (...); // update the customer object using the DAO custDAO.updateCustomer(cust); // delete a customer object custDAO.deleteCustomer(...); // select all customers in the same city Customer criteria=new Customer(); criteria.setCity("New York"); Collection customersList = custDAO.selectCustomersTO(criteria); // returns customersList - collection of Customer // Transfer Objects. iterate through this collection to // get values....

Related Pattern: Data Mapper [Martin Fowler] The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other.

DAO and other patterns DAO is a general and complex pattern Martin Fowler, Patterns of Enterprise Application Architecture – Table Data Gateway – Row Data Gateway – Active Record/Active DomainObject

Related Pattern: Table Data Gateway [Martin Fowler] A Table Data Gateway holds all the SQL for accessing a single table or view: selects, inserts, updates, and deletes. Other code calls its methods for all interaction with the database. A Table Data Gateway has one instance per table.

Simpler Pattern: Row Data Gateway Row Data Gateway gives you objects that look exactly like the record in your record structure but can be accessed with the regular mechanisms of your programming language. All details of data source access are hidden behind this interface. A Row Data Gateway has one instance per row returned by a query.

Simplest Pattern: Active Record/Active DomainObject An object that wraps a row in a database table, encapsulates the database access, and adds domain logic on that data. Characteristics: DomainObjects are dependent on the persistence mechanism Use only if the DomainModel is simple enough and persistence mechanism does not change