Persistency Patterns. Repository and DAO

Similar documents
Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

Introduction to Databases

JDBC drivers are divided into four types or levels. The different types of jdbc drivers are:

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

JDBC 3.0. Java Database Connectivity. 1 Java

JAVA AND DATABASES. Summer 2018

Three-Tier Architecture

SQL: Programming Midterm in class next Thursday (October 5)

Unit 3 - Java Data Base Connectivity

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

SQL in a Server Environment

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

JDBC - INTERVIEW QUESTIONS

Java Database Connectivity

CSCC43H: Introduction to Databases. Lecture 9

Introduction to JDBC. Lecture 12

UNIT III - JDBC Two Marks

Database connectivity (II)

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

Database Application Development

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

3) execute() Usage: when you cannot determine whether SQL is an update or query return true if row is returned, use getresultset() to get the

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Databases 2012 Embedded SQL

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

INTRODUCTION TO JDBC - Revised spring

Instructor: Jinze Liu. Fall 2008

Top 50 JDBC Interview Questions and Answers

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

INTRODUCTION TO JDBC - Revised Spring

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Java Database Connectivity

O ne of the most important features of JavaServer

SQL DML and DB Applications, JDBC

UNIT-3 Java Database Client/Server

Self-test Database application programming with JDBC

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

COMP102: Introduction to Databases, 23

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

Java E-Commerce Martin Cooke,

SNS COLLEGE OF ENGINEERING, Coimbatore

Logging and Recovery. 444 Section, April 23, 2009

Java Database Connectivity (JDBC) 25.1 What is JDBC?

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

DATABASE DESIGN I - 1DL300

Secure Programming. CS3524 Distributed Systems Lecture 16

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

Unit 2 JDBC Programming

Chapter 3 DB-Gateways

Lecture 2. Introduction to JDBC

PERSİSTENCE OBJECT RELATİON MAPPİNG

COURSE 6 PROGRAMMING III OOP. JAVA LANGUAGE

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

BUSINESS INTELLIGENCE LABORATORY. Data Access: Relational Data Bases. Business Informatics Degree

Chapter 4 Application Programs and Object-Relational Capabilities

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

Chapter 3 DB-Gateways

Database Applications (15-415)

Chapter 3 DB-Gateways

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

Object Persistence Design Guidelines

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

Databases and JDBC. by Vlad Costel Ungureanu for Learn Stuff

JDBC Java Database Connectivity is a Java feature that lets you connect

20.1 Tips for Lab Ex. 6

Best Practices for Boosting Java Application Performance and Availability on IBM DB2

Chapter 16: Databases

access to a JCA connection in WebSphere Application Server

Database Application Development Part 2 - Chapter

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

Database Application Development

Acknowledgments About the Authors

Java Database Connectivity

JDBC TM RowSet Implementations Tutorial

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Lightweight J2EE Framework

Java Database Connectivity

JDBC [Java DataBase Connectivity]

Database in Applica.on Development. Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Enterprise JavaBeans. Layer:08. Persistence

Database Application Development

JDBC Programming: Intro

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

WebSphere Connection Pooling. by Deb Erickson Shawn Lauzon Melissa Modjeski

CSE 308. Database Issues. Goals. Separate the application code from the database

Enterprise Systems. Lecture 02: JDBC. Behzad BORDBAR

Transcription:

Persistency Patterns Repository and DAO 1

Repository pattern Basically, the Repository pattern just means putting a façade over your persistence system so that you can shield the rest of your application code from having to know how persistence works. Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. 2

Definition from: Martin Fowler. Patterns of Enterprise Architecture. A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers. 3

Repository solution For each type of object that needs global access, create an object that can provide the illusion of an in-memory collection of all objects of that type. Set up access through a well-known global interface. Provide methods to add and remove objects, which will encapsulate the actual insertion or removal of data in the data store. Provide methods that select objects based on some criteria and return fully instantiated objects or collections of objects whose attribute values meet the criteria, thereby encapsulating the actual storage and query technology. Provide REPOSITORIES only for AGGREGATE roots that actually need direct access. Keep the client focused on the model, delegating all object storage and access to the REPOSITORIES. 4

5

Repository advantages Encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers. Decouples application and domain design from persistence technology, multiple database strategies, or even multiple data sources. Allows easy substitution of a dummy implementation, for use in testing (typically using an in-memory collection -mock). 6

Example SaleWindow (from View) Product (from Domain) - ID : Integer - name : String SaleController (from Controller) View Domain Controller ProductRepository (from Persistancy) Persistancy + add(p : Product) + delete(p : Product) + findbyid(id) : Product + save(p : Person) + findbyname(name : String) : Collection<Person> ProductRepositoryMock (from Persistancy) ProductRepositoryFile (from Persistancy) ProductRepositoryDB (from Persistancy) 7

Repository and Bridge 8

Abstract type A REPOSITORY "contains" all instances of a specific type, but this does not mean that you need one REPOSITORY for each class. The type could be an abstract superclass of a hierarchy. The type could be an interface whose implementers are not even hierarchically related. Or it could be a specific concrete class. This face with constraints imposed by the lack of such polymorphism in the database technology. 9

General: findbyid Specific: findbyspecificcriteria 10

Take advantage of the decoupling from the client You have more freedom to change the implementation of a REPOSITORY than you would if the client were calling the mechanisms directly. You can take advantage of this to optimize for performance, by varying the query technique or by caching objects in memory, freely switching persistence strategies at any time. You can facilitate testing of the client code and the domain objects by providing an easily manipulated, dummy in-memory strategy. 11

Leave transaction control to the client Although the REPOSITORY will insert into and delete from the database, it will ordinarily not commit anything. It is tempting to commit after saving, for example, but the client presumably has the context to correctly initiate and commit units of work. Transaction management will be simpler if the REPOSITORY doesn t have initiative. 12

Relation between Repositories and Factories The client of a REPOSITORY should be given the illusion that the objects are in memory. The object may have to be reconstituted, but it is the same conceptual object, still in the middle of its life cycle. A FACTORY handles the beginning of an object's life; a REPOSITORY helps manage the middle and the end. The reconstitution of a stored object is not the creation of a new conceptual object. In this DD design, FACTORIES and REPOSITORIES have distinct responsibilities. The FACTORY makes new objects; the REPOSITORY finds old objects. 13

Data Access Object Problem You want to encapsulate data access and manipulation in a separate layer. Forces You want to implement data access mechanisms to access and manipulate data in a persistent storage. You want to decouple the persistent storage implementation from the rest of your application. You want to provide a uniform data access API for a persistent mechanism to various types of data sources, such as RDBMS, LDAP, OODB, XML repositories, flat files, and so on. You want to organize data access logic and encapsulate proprietary features to facilitate maintainability and portability. Solution Use a Data Access Object to abstract and encapsulate all access to the persistent store. The Data Access Object manages the connection with the data source to obtain and store data. 14

Class Diagram 15

Sequence Diagram 16

Related Patterns Transfer Object Data access objects in their most basic form use transfer objects to transport data to and from their clients. Transfer Object are used in other strategies of Data Access Objects. The RowSet Wrapper List strategy returns a list as a transfer object. Factory Method [GoF] and Abstract Factory [GoF] The Data Access Object Factory strategies use the Factory Method pattern to implement the concrete factories and its products (DAOs). For added flexibility, the Abstract Factory pattern is used as described in the Data Access Object Factory strategy. Broker [POSA1] The DAO pattern is related to the Broker pattern, which describes approaches for decoupling clients and servers in distributed systems. The DAO pattern more specifically applies this pattern to decoupling the resource tier from clients in another tier, such as the business or presentation tier. Transfer Object Assembler The Transfer Object Assembler uses the Data Access Object to obtain data to build the composite transfer object it needs to assemble and send as the model data to the client. Value List Handler The value list handler needs a list of results to act upon. Such a list is often obtained using a Data Access Object, which can return a set of results. While the value list handler has an option of obtaining a RowSet from the Data Access Object, it might be better to obtain and use a RowSet Wrapper List instead. 17

JDBC Java Database Connectivity (JDBC) API contains a set of classes that allows the access to the data. Any type of sources could be accessed: relational data bases, spreadsheets, or files. JDBC provides a set of interfaces that could be used for building specialized tools. Packages: java.sql contains classes and interfaces for data accessing and processing. javax.sql contains classes and interfaces for adding new functions for the server side. 18

Establishing a connection Two possibilities: Class DriverManager: This class imposes the loading of a driver specific to the data base and then the connection is created using an URL. Interface DataSource: It is recommended for complex applications since it is allowed to configure the data source in a transparent way. 19

Connection creation Using the interface DataSource InitialContext ic = new InitialContext() //a) DataSource ds = ic.lookup("java:comp/env/jdbc/mydb"); //b) Connection con = ds.getconnection(); DataSource ds = (DataSource)org.apache.derby.jdbc.ClientDataSource() ds.setport(1527); ds.sethost("localhost"); ds.setuser("app") ds.setpassword("app"); Connection con = ds.getconnection(); 20

Using the class DriverManager Two steps: Load the corresponding driver into the memory: ( variants ) Class.forName(<DriverClassName>); Class.forName creates an instance of the driver and registers it to the DriverManager. It is not necessary to create an instance of the class. Ex: Class.forName( com.mysql.jdbc.driver ); Connection creation 21

22

23

Connection creation using the class DriverManager The interface Driver is used for managing the drivers for a JDBC client. When the client ask a connection and gives an URL, the class DriverManager is responsible to find the driver that could recognize the URL and to use it for establishing the connection. URL Syntax for a connection: jdbc:subprotocol:<database_identifier>[propertieslist] Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES"); String url = "jdbc:mysql:fred"; Connection conn = DriverManager.getConnection(url, <user>, <passwd>); 24

Class Connection Represents a session with a particular data base. Any SQL statement is executed and the results are sent using the context of a connection. Methods: close(), isclosed():boolean createstatement( ):Statement //overloaded preparecall( ):CallableStatement //overloaded preparestatement( ):PreparedStatement //overloaded rollback() setautocommit(boolean) //tranzaction getautocommit():boolean commit() 25

Class Statement Is used for executing a static SQL statement and for returning the result. Methods: execute(sql:string, ):boolean //for any statement getresultset():resultset //SQL getupdatecount():int executequery(sql:string, ):ResultSet //pentru SELECT executeupdate(sql:string, ):int //INSERT,UPDATE,DELETE cancel() close() 26

Example 27

Example //insert String insert="insert into books (title, authors, isbn, year) values ('Nuvele', 'Mihai Eminescu', '4567567', 2008)"; Statement stmt=null; try { stmt=conn.createstatement(); stmt.executeupdate(insert); } catch (SQLException e) { System.out.println("Insert error "+e); }finally{ } //delete if (stmt!=null) stmt.close(); String delstring= delete from books where isbn= tj234 Statement stmt=conn.createstatement(); stmt.executeupdate(delstring); stmt.close(); 28

ResultSet Contains a table that represents a result of a SELECT statement. An object of ResultSet type contains a cursor that indicates the current line in the table. Initially the cursor is set before the first line. The method next moves the cursor on the next line. When there are no other lines the result is equal to false. Some configurations are possible: the table could be modified, the iteration way, etc.) These could be done using the method createstatement( ): Statement stmt = con.createstatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executequery("select name, address FROM users"); // rs could be iterated, can be modified, //and it is not notified about the updates made by other users. 29

Methods: absolute(row:int) relative(n:int) ResultSet afterlast(), beforefirst(), first(), last(), next():boolean getrow():int getint(columnindex columnlabel):int getfloat( ), getstring( ), getobject( ), etc updateint(columnindex,columnlabel, newvalue) updatefloat( ), updatestring( ), etc updaterow() refreshrow() rowdeleted(), rowinserted(), rowupdated() Implicitly an object of type ResultSet is unidirectional, with forward iteration, and unmutrable. 30

ResultSet Statement stmt=conn.createstatement(); ResultSet rs=stmt.executequery( select * from books ); while(rs.next()){ } System.out.println( Book + rs.getstring( title )+ +rs.getstring( author )+ ); rs.close(); +rs.getint( year ) stmt.close(); 31

Class PreparedStatement An instance of type PreparedStatement receives a SQL statement when it is created. The SQL statement is sent to the SGBD of the data base, where it is compiled. When a statement associated to a PreparedStatement is sent, SGBD directly executes the SQL statement without any other verification. It is more efficient than Statement. The paramenters marked using the character?. PreparedStatement prestmt = con.preparestatement( select * from books WHERE year=?"); The value of a parameter is sent using setter methods (setxyz, XYZ represents the parameter type). prestmt.setint(1, 2008); ResultSet rs=prestmt.executequery(); 32

Transactions Implicitly each SQL statement is treated as being a transaction and it is registered/operated immediately after the execution. The implicit behavior could be modified using the method: setautocommit(false) of the class Connection. Methods: Commit Rollback setsavepoint 33

Example con.setautocommit(false); PreparedStatement updatesales = con.preparestatement( "UPDATE COFFEES SET SALES =? WHERE COF_NAME LIKE?"); updatesales.setint(1, 50); updatesales.setstring(2, Black"); updatesales.executeupdate(); PreparedStatement updatetotal = con.preparestatement( "UPDATE COFFEES SET TOTAL = TOTAL +? WHERE COF_NAME LIKE?"); updatetotal.setint(1, 50); updatetotal.setstring(2, Black"); updatetotal.executeupdate(); con.commit(); con.setautocommit(true); 34

Identity map pattern Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them. 35