objektorienteret netværkskommuniation(itonk1) persistence

46
Objektorienteret netværkskommuniation(ITONK1) Persistence

Upload: constance-hodge

Post on 26-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Objektorienteret netværkskommuniation(ITONK1) Persistence

Objektorienteret netværkskommuniation(ITONK1)

Persistence

Page 2: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 2

Goal with todays lesson

• After these 2-3x35 minutes you will be:– Comfortable with the expression “persistence”, and how

it relates to Distributed Systems– Knowledgeable about different strategies for obtaining

persistence for Distributed Systems– Ready to explore the subject further– You will not:

• Be an expert on persistence, as this is a huge area in itself

Page 3: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 3

Outline

• Plenum – experience with persistent datastorage• Continuing exercise• Principles of Persistence• Datastore Technology for Persistence

– Files– Relational Databases

• Exemplified OR-mapping with EJB

– Object Databases

Page 4: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 4

Experience with Persistent datastorage

• To establish a picture of your knowledge – Which types of persistent datastorage do you know?– What experience do you have in using it with (oo)

programming languages?– What problems did you face – and how did you solve

them?

Page 5: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 5

Exercise: comparing strategies for persistence

• This will be a continuing discussion• Select one or more entity classes from your own required

assignment 1 an discuss how to solve the persistent requirements of the assignment using:– File persistence– Relational Database Management System (toolkit or emb. JDBC)– Object database system (ODBMS, e.g. JDO)

• Using information obtained from the examples from OOMI-2 and the following slides

• Feel free to use the classroom PC for searching for more information

• We will stop after each section (files, RDBMS, ODBMS), and allow for 5 min. group work

• We will end with a follow-up discussion

Page 6: Objektorienteret netværkskommuniation(ITONK1) Persistence

Principles of Persistence

Page 7: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 7

What is Persistence – a definition

• Persistence is the ability of an object to survive the lifetime of the process in which it resides.

• Persistence is relevant for stateful server objects.• What is State?

– State = object instance attributes – private & public– Not methods

• We remember the activation/deactivation discussion?– The state needs to be retained between object

deactivation and object activation– Why?

Page 8: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 8

How to achieve Persistence?

• Storing object state on persistent datastore before deactivation

• Upon activation, load object state from persistent datastore– E.g. RMI activation

• Persistent storage can be obtained by?– File system

• embedded systems on disk-storage, Flash-RAM and others– Relational Database

• All from embedded, to desktop and enterprise servers (most widely used is SQL servers)– Object-Database

• Has been emerging technology for years, but no widespread support yet. JDO for Java is a promising technology however

– Others?

Page 9: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 9

Transparency of Persistence

• Persistence should be transparent to users and designers of client objects

Client Objects Server Objects Datastore Objects

Client Interface

Persistence Interface

Decouple Datastore technology from client-objects

Page 10: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 10

Persistence Concepts

Datastore

Sessions

StorageObjects

StorageHomes

Storage HomeIncarnation

Storage ObjectIncarnation

RDBMSServer

RDBMSTable

Object= row in

RDBMS table

C++/Java object

C++/Java object

C++/Java object

Page 11: Objektorienteret netværkskommuniation(ITONK1) Persistence

Datastore Technology for Persistence

Page 12: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 12

Datastore Technology

• Persistence can be implemented using • Files

– CORBA Externalization– Java Serialization– Structured Storage in COM

• Relational Databases– Object Relational Mapping

• “Homegrown”, CMP, JDO, Hibernate, Torque, LLBLGen Pro

– JDBC/ODBC• Oracle, IBM DB2, Microsoft SQL Server & Access, MySQL

• Object Databases– http://www.odmg.org/

Page 13: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 13

How to obtain persistence

• Roll your own persistence – SOAP, RMI

• For Java – use JDBC for RDBMS, serialization for filesystems• CORBA externalization for filesystems• COM serialization or structured storage• Possible to construct your own framework (as ROAD)

• Use a standard service– CORBA Persistence service (PSS)– COM Persistence service– Enterprise Java Beans– JDO Toolkit– Hibernate– LLBLGen Pro– May still be necessary to obtain persistence manually

Page 14: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 14

Externalization in CORBA

• Technique to – write composite objects into a byte stream– load composite objects from a byte stream

• Byte stream can then be written to/read from the file system

• Supported by several CORBA products

Page 15: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 15

Java Serialization

• Transforms Java objects into stream• Follows all referenced Java objects• Stream can be written onto a file to achieve

persistence• Objects must implement Serializable• Attributes that need not be made persistent can

be declared as transient in the class definition

Page 16: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 16

Example of using Serialization for RMI

• http://java.sun.com/j2se/1.3/docs/guide/rmi/activation/activation.4.html– Class:

• public MyPersistentClass(ActivationID id, MarshalledObject data) throws RemoteException, ClassNotFoundException, java.io.IOException {

• This activatable class will– Restore its State upon activation (if

persistentObjectStore.ser exist)– Save its State after a new transaction is registered– This could have been done utilizing an RDBMS

Page 17: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 17

private Vector transactions; private File holder;

public MyPersistentClass(ActivationID id, MarshalledObject data) throws RemoteException, ClassNotFoundException, java.io.IOException {

// Register the object with the activation system // then export it on an anonymous port super(id, 0);

// Extract the File object from the MarshalledObject that was // passed to the constructor // holder = (File)data.get(); if (holder.exists()) { // Use the MarshalledObject to restore my state

// this.restoreState();

} else { transactions = new Vector(1,1); transactions.addElement("Initializing transaction vector");

} }

CONSTRUCTOR of MyPersistentClassFind complete example and tutorial at:

http://java.sun.com/j2se/1.4.2/docs/guide/rmi/activation/activation.4.html

Every time a passivated object is called,the constructor is called as the rmid daemon

Instantiates it to activate – and here itis checked whether the File exist – or not.

If it does exist, then its State is restored

Every time a passivated object is called,the constructor is called as the rmid daemon

Instantiates it to activate – and here itis checked whether the File exist – or not.

If it does exist, then its State is restored

MarshalledObject data = new MarshalledObject (new File( "/home/rmi_tutorial/activation/persistentObjectStore.ser"

Page 18: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 18

public Vector calltheServer(Vector v) throws RemoteException { int limit = v.size(); for (int i = 0; i < limit; i++) { transactions.addElement(v.elementAt(i)); } // Save this object's data out to file // this.saveState(); return transactions; }

private void restoreState() throws IOException, ClassNotFoundException { File f = holder; FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis); transactions = (Vector)ois.readObject(); ois.close(); } private void saveState() { try {

File f = holder; FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(getTransactions()); oos.close();

} catch (Exception e) { throw new RuntimeException("Error saving vector of data");

} }

Using simple Java SerializationTo obtain persistence

Using simple Java SerializationTo obtain persistence

Find complete example and tutorial at: http://java.sun.com/j2se/1.4.2/docs/guide/rmi/activation/activation.4.html

Page 19: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 19

Persistence Interfaces in COM

IPersist

+ GetClassID()

<<Interface>>

IPersistStorage

+ IsDirty()+ InitNew()+ Load()+ Save()+ SaveCompleted()+ HandsOffStorage()

<<Interface>>

IPersistStream

+ IsDirty()+ Load()+ Save()+ GetSizeMax()

<<Interface>>

IPersistFile

+ IsDirty()+ Load()+ Save()+ SaveCompleted()+ GetCurFile()

<<Interface>>

IUnknown<<Interface>>

Page 20: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 20

Problems with File-based Persistence

• Mapping to Files can be inefficient for large composite objects

• File systems only have crude support for concurrency control

• File systems lack support for fault-tolerance (transactions)

• Application specific code needed for every persistent class

Page 21: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 21

Continuing exercise part 1 – file-based

• Select one or more entity classes from your own required assignment 1 an discuss how to solve the persistent requirements of the assignment using:– File persistence– Pro’s and Con’s– How to implement?

Page 22: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 22

Relational Database

• Relational Database Management Systems (RDBMSs) Examples:– Ingres– Oracle– Sybase– DB2– Microsoft SQL Server– Microsoft Access– MySQL– PostGree DB

Page 23: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 23

Mapping to RDBMS’s

• Relational database schemas consist of sets of tables• Types of mapping:

– Define a table for each type– Complex mapping

• In each table create – primary key for object identifier– a column for each attribute of the object

• mapping of middleware atomic types to primitive types supported by RDBMS

• secondary keys for object references

• Resolve inheritance statically– This and other problems ->

• Objects and RDBMS does not map perfectly:– the OR impedance mismatch

Page 24: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 24

Embedding Queries into Programs

• Embedded SQL– Macros to embed queries into programs– RDBMS provides processor to expand macros– API to traverse queries– Not standardized

• Open Database Connectivity (Microsoft)– Standardized API for RDBMS Access available on all

Microsoft Platforms

• Java Database Connectivity (Sun)– Standardized RDBMS Access from Java

Page 25: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 25

Issues with mapping

• Does this mean that we should figure out for ourselfes how to obtain the OR-mapping?– No– Frameworks available– CORBA PSS, COM persistence, EJB for Java, CCM,

Hibernate– JDBC is at a low level– Hibernate & Torque/Turbine project freeware– In the following EJB will be presented, because:

• EJB may be used with Java RMI, Web service & CORBA• CCM is built on the EJB specification• EJB semantics covers most of the issues

Page 26: Objektorienteret netværkskommuniation(ITONK1) Persistence

RDBMS mapping illustrated with EJB’s

Page 27: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 27

Enterprise JavaBeans (EJB’s)• Standard server-side component model for Java

Enterprise Applications– security

– resource pooling

– persistence

– concurrency

– transactional integrity

• Has nothing to do with “JavaBeans”– JavaBeans designed for intra-process purposes

• GUIs, non-visual widgets, entity representations

– Enterprise Java Beans (EJB) designed for inter-process purposes

Page 28: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 28

EJB’s (cont.)

• Restricted to Java (only implementation language)– platform independence– “write once, run anywhere”

• EJB components– platform/implementation independence– write once, run in any Application Server complying with the

EJB spec• J2EE reference implementation• Oracle’s Application Server (OAS)• IBM’s Websphere• BEA’s Weblogic Server and Weblogic Enterprise• Sybase’s EAServer• Open Source – JBoss (see links)

Page 29: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 29

Bean Usage

• Entity beans– model state maintained across all client interactions– represent a row of data in a database

• Session beans– model business process being performed by a single

client involving one or more entity beans– it extends the actions of the client into the server

• simplifies the actions programmed by the client• limits the number of distributed calls required between the client

and the entity beans• limits the number of stubs that have to be loaded by the client

– are not persisted to a database

Maps to domain model (Entity classes)Maps to domain model (Entity classes)

Maps to Use Case model (Control classes)Maps to Use Case model (Control classes)

Page 30: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 30

Persistence Concepts in EJB

Datastore

Sessions

StorageObjects

StorageHomes

Storage HomeIncarnation

Storage ObjectIncarnation

Page 31: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 31

State Management

• State synchronization methods– ejbLoad– ejbStore

• Resource Management methods– ejbActivate– ejbPassivate

Page 32: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 32

Entity Bean Types

• Bean can have total control over loading and storing from database– Bean Managed Persistence (BMP)

• Container can take over this responsibility– Container Managed Persistence (CMP)

• Still need to define an OR mapping in admin tool• This is the same in CORBA CCM / PSS

– Specialized Implementations• Legacy applications such as CICS

• When to choose what?– Well – start out with CMP if possible, and then migrate

code as performance issues pops up during testing

Page 33: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 33

Bean Managed Persistence

• Have to handle all database interaction– except distributed transactions

• ejbLoad() and ejbStore() called when bean instance state must be synchronized with database

• ejbActivate() and ejbPassivate() called when bean is moved between the ready state and pooled state

Page 34: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 34

Implement a BMP Entity Bean

package java.examples.ejb.entity.bean;

import javax.ejb.EntityBean;

import javax.ejb.EntityContext;

public class BookBMP extends BookEJB {

private DataSource dataSource_;

private EntityContext ctx_;

Additional setup of database connections needed – some are done in the configuration tool

Important features: Entity beans always implement the following event handles:•ejbCreate: insert•ejbRemove: delete•ejbLoad: select•ejbStore: update•ejbFindByPrimaryKey: select•And more can be implemented:•ejbFindBooksByAuthor: select

Page 35: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 35

Implement DB Insertion

public String ejbCreate(String id, String title, String author, String topic) {

super.ejbCreate(id, title, author, topic);

Connection conn = null;

PreparedStatement pstatement = null;

try {

conn = dataSource_.getConnection();

pstatement=conn.prepareStatement("insert into Book (id, title, author, topic)"+ ” values (?, ?, ?, ?)");

pstatement.setString(1,id_); pstatement.setString(2,title_);

pstatement.setString(3,author_); pstatement.setString(4,topic_);

pstatement.execute(); return id_;

}

catch(SQLException ex) { throw new EJBException(ex); }

finally { … }

}

id title author topic

42123 EJB SW EJB

43423 EJB2 SW EJB

… … … …

BookBMP

OR-mapping

Page 36: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 36

Implement DB Loadpublic void ejbLoad() { Connection conn = null; PreparedStatement pstatement = null; ResultSet rs = null; try { conn = dataSource_.getConnection(); pstatement = conn.prepareStatement( "select id, title, author, topic from Book " + "where id = ?"); pstatement.setString(1, (String)ctx_.getPrimaryKey()); rs = pstatement.executeQuery(); if (rs.next()) { if (rs.next()) { id_ = rs.getString("id");

title_ = rs.getString("title"); author_ = rs.getString("author");

topic_ = rs.getString("topic"); super.ejbLoad(); } else { throw new EJBException("unable to locate row"); } } catch(SQLException ex) { throw new EJBException(getText(ex)); } finally { … } …

Page 37: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 37

Implement DB Store

public void ejbStore() {

Connection conn = null;

PreparedStatement pstatement = null;

try {

super.ejbStore();

conn = dataSource_.getConnection();

pstatement = conn.prepareStatement(

"update Book set title=?, author=?, topic=? " + "where id = ?");

pstatement.setString(1,title_); pstatement.setString(2,author_);

pstatement.setString(3,topic_); pstatement.setString(4,id_);

pstatement.executeUpdate();

}

catch(SQLException ex) { throw new EJBException(getText(ex)); }

finally { … }

}

Page 38: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 38

Implement DB Remove

public void ejbRemove() {

Connection conn = null;

PreparedStatement pstatement = null;

try {

super.ejbRemove();

conn = dataSource_.getConnection();

pstatement = conn.prepareStatement("delete from Book " + "where id = ?");

pstatement.setString(1, (String)ctx_.getPrimaryKey());

pstatement.executeUpdate();

}

catch(SQLException ex) { throw new EJBException(getText(ex)); }

finally { … }

}

Page 39: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 39

Implement Finders

public String ejbFindByPrimaryKey(String pk) throws FinderException {

Connection conn = null;

PreparedStatement pstatement = null;

ResultSet rs = null;

try {

conn = dataSource_.getConnection();

pstatement = conn.prepareStatement("select id from Book " + "where id = ?");

pstatement.setString(1, pk);

rs = pstatement.executeQuery();

if (rs.next()) {

return rs.getString("id");

}

else { throw new ObjectNotFoundException(pk + " no found"); }

}

catch(SQLException ex) { throw new EJBException(getText(ex)); }

finally {... }

}

Page 40: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 40

Implement Finders (cont.)

public Collection ejbFindAll() throws FinderException {

Connection conn = null;

PreparedStatement pstatement = null;

ResultSet rs = null;

try {

Vector pKeys = new Vector();

conn = dataSource_.getConnection();

pstatement = conn.prepareStatement("select id from Book ");

rs = pstatement.executeQuery();

while (rs.next()) {

pKeys.add(rs.getString("id"));

}

return pKeys;

}

catch(SQLException ex) {throw new EJBException(getText(ex)); }

finally {... }

}

Page 41: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 41

Continuing exercise part 2 – RDBMS based

• Select one or more entity classes from your own required assignment 1 an discuss how to solve the persistent requirements of the assignment using:– RDBMS

• Embedded SQL• If you have experience using toolkits, be free to elaborate on

this

– Pro’s and Con’s– How to implement– Compared to using file externalization

Page 42: Objektorienteret netværkskommuniation(ITONK1) Persistence

Principles of Persistence continuedODBMS

Page 43: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 43

ODBMS

• ODBMSs have been standardized by the Object Database Management Group– Schema definition language (ODL) – subset of CORBA

IDL– Programming language bindings to

• C++ • Java• And many others

– Object Query Language (OQL)– JDO has replaced the Java binding

• Support persistence of OO programming language objects

Page 44: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 44

Mapping to ODBMS’s

• ODL is a superset of OMG/IDL• Programming language bindings of ODBMS’s are

also supported by CORBA• ODBMS objects can be

– clients of CORBA objects– servers for CORBA objects

Page 45: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 45

Continuing exercise part 3 – ODBMS based

• Select one or more entity classes from your own required assignment 1 an discuss how to solve the persistent requirements of the assignment using:– ODBMS

• Use the JDO example from OOMI-2

– Pro’s and Con’s– How to implement?– Compared to using RDBMS and file externalization

Page 46: Objektorienteret netværkskommuniation(ITONK1) Persistence

Ingeniørhøjskolen i ÅrhusSlide 46

Comparison

• File externalization / serialization is not transparent for implementors of server objects

• Persistence in RDBMS’s is – complicated by OR-impedance mismatch– simplified by wide availability of RDBMS’s– Well-known tested technology

• Persistence in ODBMS’s is– simplified by conceptual similarities of

• object models

• programming language bindings

– No widespread support– (personal view) I would not risk high-performance system on this