1 Persistence. 2 Persistence Usage Scalability –disk cheaper than memory Fault recovery –last known state maintained through recovery Parallel processing.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Serialization Flatten your object for automated storage or network.
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Transaction.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
The child gets it all..  Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java I/O Writing and Reading Objects to File Serialization.
Data Persistence CS 340. Persistence Strategies  There are many strategies a program can use for persisting its in-memory object model  Approach #1.
Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Partitioning Patterns How to partition complex actors and concepts into multiple classes. Layered Initialization Filter Composite.
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.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Outline  Basic IO  File class  The Stream Zoo  Serialization  Regular Expressions.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Introduction.  Administration  Simple DBMS  CMPT 454 Topics John Edgar2.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
Files and Serialization. Files Used to transfer data to and from secondary storage.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Operating Systems {week 11a}
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Chapter 17 Binary I/O.
Object Writing in files
Memento Design Pattern
Chapter 17 Binary I/O 1.
Accessing Files in Java
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Client server programming
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Behavioral and Structural Patterns
Object To Relation Mapping
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Podcast Ch23f Title: Serialization
Making Text for the Web part 6
System Models Bina Ramamurthy 9/7/2019 B.Ramamurthy.
Presentation transcript:

1 Persistence

2 Persistence Usage Scalability –disk cheaper than memory Fault recovery –last known state maintained through recovery Parallel processing –multiple processors working on shared data source Queryable storage –locate objects for access Checkpointing –save current state, potentially as a blob Pass by Value –pass objects from one process to another so that method invocations on the passed object will result in a local method call in the process it was passed to

3 Persistence Types Basic –No Persistence –Simple Persistence –Object Serialization Object/RDBMS –Using Blobs –Horizontal Partitioning –Vertical Partitioning –Unification OODBMS

4 No Persistence: Client Must Manage State Account id_ : String balance_ : double ownerId_ : String : Client : AccountDataOutput Stream 1: get id_ ( ) 3: get balance_ ( ) 5: get ownerId_ ( ) 2: writeByes() 4: writeInt() 6: writeBytes Used when legacy classes not designed with persistence built in

5 No Persistence: Implementation does not address persistence of state public class Account { public String id_; public double balance_; public String ownerId_; public Account(String id, double balance, String ownerId) { id_ = id; balance_ = balance; ownerId_ = ownerId; } public print(java.util.PrintStream) {... } }

6 Client Code Must Implement Persistence void save() throws IOException { System.out.println("Client saving accounts"); DataOutputStream ostream = new DataOutputStream( new FileOutputStream(stateFile_)); ostream.writeInt(accounts_.length); for(int i=0; i<accounts_.length; i++) { ostream.writeInt(accounts_[i].id_.length()); ostream.writeBytes(accounts_[i].id_); ostream.writeDouble(accounts_[i].balance_); ostream.writeInt(accounts_[i].ownerId_.length()); ostream.writeBytes(accounts_[i].ownerId_); } ostream.close(); }

7 Client Code Must Implement Persistence void restore() throws IOException { DataInputStream istream = new DataInputStream( new FileInputStream(stateFile_)); accounts_ = new Account[istream.readInt()]; for(int i=0; i<accounts_.length; i++) { int len = istream.readInt(); byte buffer[] = new byte[len]; istream.readFully(buffer); String id = new String(buffer); double balance = istream.readDouble(); len = istream.readInt(); buffer = new byte[len]; istream.readFully(buffer); String ownerId = new String(buffer); accounts_[i] = new Account(id,balance,ownerId); } istream.close(); }

8 Simple Persistence Class takes over responsibility for state persistence Slight improvement over no persistence : Client : Account:DataOutput Stream 1: writeExternal (DataOutputStream) 2: writeBytes() 3: writeDouble() 4: writeBytes() Account id_ : String balance_ : double ownerId_ : String readExternal (in : DataInputStream) writeExternal (out : DataOutput)

9 Simple Persistence: Class Saves Its Own State import java.io.PrintStream; import java.io.IOException; import java.io.DataInputStream; import java.io.DataOutputStream; public class Account { private String id_; private double balance_; private String ownerId_; public Account() { } public Account(String id, double balance, String ownerId) { id_ = id; balance_ = balance; ownerId_ = ownerId; } //...

10 public void writeExternal(DataOutputStream ostream) throws IOException{ ostream.writeInt(id_.length()); ostream.writeBytes(id_); ostream.writeDouble(balance_); ostream.writeInt(ownerId_.length()); ostream.writeBytes(ownerId_); } public void readExternal(DataInputStream istream) throws IOException { int len = istream.readInt(); byte buffer[] = new byte[len]; istream.readFully(buffer); id_ = new String(buffer); balance_ = istream.readDouble(); len = istream.readInt(); buffer = new byte[len]; istream.readFully(buffer); ownerId_ = new String(buffer); } Simple Persistence: Class Saves Its Own State

11 Simple Persistence: Client Simplified Client shielded from details of save/restore void save() throws IOException { System.out.println("Client saving accounts"); DataOutputStream ostream = new DataOutputStream( new FileOutputStream(stateFile_)); ostream.writeInt(accounts_.length); for(int i=0; i<accounts_.length; i++) { accounts_[i].writeExternal(ostream); } ostream.close(); }

12 Simple Persistence: Client must know object’s class Client still must know the class of the object saved/restored void restore() throws IOException { System.out.println("Client restoring accounts"); DataInputStream istream = new DataInputStream( new FileInputStream(stateFile_)); accounts_ = new Account[istream.readInt()]; for(int i=0; i<accounts_.length; i++) { accounts_[i] = new Account(); accounts_[i].readExternal(istream); } istream.close(); }

13 Simple Persistence Problem Objects may have multiple references to them An object may be saved multiple times, once for each reference Multiple clones might be instantiated, one for each persisted copy

14 Simple Persistence Problem Secretary name = Money Penny Manager name= M secretary = Manager name= James secretary = Secretary name = Money Penny Manager name= M secretary = Manager name= James secretary = Secretary name = Money Penny Secretary name = Money Penny When written out, we get multiple copies of aliased objects because object references are not resolved.

15 Serialization Clients do not need to know the type of the object saved –an abstract interface (java.io.Serializable is defined to tag the object) Clients do not need to know the class of the object restored –any abstract super-class or interface of the object is suitable Serialization takes care of object references Classes take responsibility for their persisted elements –delegate to the language –tag elements for no persistence –specialize the save process for alternate storage mechanisms

16 Classes add a Tagging Interface import java.io.PrintStream; import java.io.Serializable; public class Account implements Serializable { private String id_; private double balance_; private Owner owner_; public Account(String id, double balance, Owner owner) { id_ = id; balance_ = balance; owner_ = owner; }

17 Associated Classes add the Tagging Interface import java.io.PrintStream; import java.io.Serializable; public class Owner implements Serializable { private String name_; private String taxId_; public Owner(String name, String taxId) { name_ = name; taxId_ = taxId; } public void print(PrintStream out) { out.println(this + "- name="+name_ + ", taxid=" + taxId_); }

18 Clients have Simple Save and Restore Mechanism void save() throws IOException { System.out.println("Client saving accounts"); ObjectOutputStream ostream = new ObjectOutputStream( new FileOutputStream(stateFile)); ostream.writeObject(accounts); ostream.close(); } void restore() throws IOException, ClassNotFoundException { System.out.println("Client restoring accounts"); ObjectInputStream istream = new ObjectInputStream( new FileInputStream(stateFile)); accounts = (Account[])istream.readObject(); istream.close(); }

19 References to Common Objects are Resolved creating Client with new accounts elements=3 id=1 balance=100.0 name=bob, taxid= id=2 balance=200.0 name=larry, taxid= id=3 balance=300.0 name=bob, taxid= Client saving accounts Client adopting statefile Client restoring accounts elements=3 id=1 balance=100.0 name=bob, taxid= id=2 balance=200.0 name=larry, taxid= id=3 balance=300.0 name=bob, taxid=

20 Protecting Attributes from Serialization public class Account implements java.io.Serializable { private String id_; private double balance_; private Owner owner_; private transient Date dummy_; //only intialized on creation public Account(String id, double balance, Owner owner) { id_ = id; balance_ = balance; owner_ = owner; dummy_ = new Date(); }

21 Protecting Attributes from Serialization creating Client with new accounts elements=3 id=1 balance=100.0 name=bob, taxid= transient dummy=Sun Aug 01 22:23:35 EDT 1999 id=2 balance=200.0 name=larry, taxid= transient dummy=Sun Aug 01 22:23:35 EDT 1999 id=3 balance=300.0 name=bob, taxid= transient dummy=Sun Aug 01 22:23:35 EDT 1999 Client saving accounts Client adopting statefile Client restoring accounts elements=3 id=1 balance=100.0 name=bob, taxid= transient dummy=null id=2 balance=200.0 name=larry, taxid= transient dummy=null id=3 balance=300.0 name=bob, taxid= transient dummy=null

22 Providing Manual Serialization Overrides public class Account implements java.io.Serializable { private String id_; private double balance_; private Owner owner_; private transient Date dummy_; //only intialized on ctor private void writeObject(ObjectOutputStream out) throws IOException { System.out.println("do something to override writeObject"); out.writeObject(id_); out.writeDouble(balance_); out.writeObject(owner_); out.writeObject(dummy_); //write transient var out anyway } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { System.out.println("do something to override readObject"); id_ = (String) in.readObject(); balance_ = in.readDouble(); owner_ = (Owner) in.readObject(); dummy_ = (Date) in.readObject(); //read transient var in anyway }

23 Providing Manual Serialization Overrides creating Client with new accounts elements=3 id=1 balance=100.0 name=bob, taxid= transient dummy=Sun Aug 01 22:39:57 EDT 1999 id=2 balance=200.0 name=larry, taxid= transient dummy=Sun Aug 01 22:39:57 EDT 1999 id=3 balance=300.0 name=bob, taxid= transient dummy=Sun Aug 01 22:39:57 EDT 1999 Client saving accounts do something to override writeObject Client adopting statefile Client restoring accounts do something to override readObject elements=3 id=1 balance=100.0 name=bob, taxid= transient dummy=Sun Aug 01 22:39:57 EDT 1999 id=2 balance=200.0 name=larry, taxid= transient dummy=Sun Aug 01 22:39:57 EDT 1999 id=3 balance=300.0 transient dummy=Sun Aug 01 22:39:57 EDT 1999

24 Object/RDBMS How do we map the following Class Model to an RDBMS InterestBearingAccount rate_ : double termDays_ : int minimumBalance_ : double CheckingAccount checkFee_ double Owner name_ : String taxId_ : String Account id_ : String balance_ : double owner_ 1*

25 Storing the Objects as Blobs void save() throws SQLException, Exception { PreparedStatement pstatement = null; try { pstatement = connection_.prepareStatement("insert into accounts(id, data) values (?, ?)"); for(int i=0; i<accounts_.length; i++) { pstatement.setString(1,accounts_[i].getId()); try { File file = File.createTempFile("tmp","dat"); ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream(file)); ostream.writeObject(accounts_[i]); ostream.close(); FileInputStream istream = new FileInputStream(file); pstatement.setBinaryStream(2, istream, (int)file.length()); //pstatement.setObject(2,accounts_[i]); pstatement.execute(); pstatement.clearParameters(); } finally { if (pstatement != null) pstatement.close(); } }

26 Restoring Objects from Blobs void restore() throws SQLException, Exception { Statement statement = null; ResultSet rs = null; try { statement = connection_.createStatement(); rs = statement.executeQuery("select id, data from accounts";); Vector accounts = new Vector(); while (rs.next()) { String accountNo = rs.getString(1); ObjectInputStream istream = new ObjectInputStream(rs.getBinaryStream(2)); Account account = (Account) istream.readObject(); //Account account = (Account) rs.getObject(2); accounts.add(account); accounts_ = new Account[accounts.size()]; accounts.toArray(accounts_); } finally { if (rs != null) rs.close(); if (statement != null) statement.close(); }

27 Using Blobs Pros –Good encapsulation of object properties Cons –Example still allows for accidental object duplication –Slows database performance can segment object into multiple tables and make use of lazy instantiation –Serialization brittle in the face of software changes/extended time better use as a cache possible use of XML or other stable marshalling forms

28 Horizontal Partitioning Each concrete class is mapped to a table InterestBearingAccount rate_ : double termDays_ : int minimumBalance_ : double CheckingAccount checkFee_ double Owner name_ : String taxId_ : String Account id_ : String balance_ : double owner_ 1*

29 Vertical Partitioning Each class is mapped to a table InterestBearingAccount rate_ : double termDays_ : int minimumBalance_ : double CheckingAccount checkFee_ double Owner name_ : String taxId_ : String Account id_ : String balance_ : double owner_ 1*

30 Unification Each sub-class is mapped to the same table InterestBearingAccount rate_ : double termDays_ : int minimumBalance_ : double CheckingAccount checkFee_ double Owner name_ : String taxId_ : String Account id_ : String balance_ : double owner_ 1*

31 RDBMS Mapping Horizontal Partitioning –entire object within one table –only one table required to activate object –no unnecessary fields in the table –must search over multiple tables for common properties Vertical Partitioning –object spread across different tables –must join several tables to activate object Vertical Partitioning (cont.) –no unnecessary fields in each table –only need to search over parent tables for common properties Unification –entire object within one table –only one table required to activate object –unnecessary fields in the table –all sub-types will be located in a search of the common table

32 Inserting Data Access Objects Application Object Data Access Object Value Object AccountAccountDAO Account Value Owner OwnerDAO Owner Value

33 Roles Application Objects –Encapsulate the business rules –Obtain connections –Demarcate transactions –Not Serializable Value Objects –Simply carry values –Serializable Data Access Objects –Encapsulate interaction with information source (database) –Designed to work with different Application Objects (e.g., no threads) –Not Serializable

34 Value Object package ejava.persistence.dao; public OwnerValue implements Serializable { String name_; String taxId_; public OwnerValue(String name, String taxId) { name_ = name; taxId_ = taxId; } public OwnerValue(OwnerValue rhs) { this(rhs.name_, rhs.taxId_); } public String getName() { return name_; } public void setName(String name) { name_ = name; } public String getTaxId() { return taxId_; } public void setTaxId(String taxId) { taxId_ = taxId; } public String toString() { return "name="+name + ", taxId="+taxId_; }

35 Data Access Object package ejava.persistence.dao; public class OwnerDAO { OwnerValue values_; public void insert(Connection connection, OwnerValue values) { Statement statement = null; try { statement = connection.createStatement(); int rows = statement.executeUpdate( "insert into owner (name, taxid) values (" + values.getName() + ", " + values.getTaxId() + ")"); if (rows != 1)... } finally { if (statement != null) statement.close(); }

36 Application Object package ejava.persistence.dao; /** This class represents the business logic for Owners. */ public class Owner { private OwnerValue values_; private static OwnerDAO ownerDAO_ = new OwnerDAO(); public Owner() { } public OwnerValue getValues() { return new OwnerValue(values_); } public void setValues(OwnerValue values) { values_ = values; } private Connection getConnection() {…} private void closeConnection(Connection connection) {…}

37 Application Object public void create(OwnerValue values) throws SQLException { values_ = values; Connection connection = null; try { connection = getConnection(); ownerDAO_.insert(connection, values_); } finally { closeConnection(connection); }

38 Application Object (cont.) public void remove() throws SQLException { Connection connection = null; try { connection = getConnection(); ownerDAO_.remove(connection, values_); values_ = null; } finally { closeConnection(connection); }