Data Management Design Based on Chapter 18 of

Data Management Design Based on Chapter 18 of Bennett, Mc. Robb and Farmer: Object Oriented Systems Analysis and Design Using UML, (2 nd Edition), Mc. Graw Hill, 2002. 03/12/2001 © Bennett, Mc. Robb and Farmer 2002 1

In This Lecture You Will Learn: The different ways of storing persistent objects n The differences between object and relational databases n How to design data management objects n How to extend sequence diagrams to include data management objects n © Bennett, Mc. Robb and Farmer 2002 2

Persistence Some objects are transient, exist in memory and are discarded when an application terminates n Some objects must exist from one execution of an application to another or be shared among different instances of applications. Such objects are n persistent objects © Bennett, Mc. Robb and Farmer 2002 3

Persistence Mechanisms n n Files hold data, typically on magnetic media such as disks and tapes Database management systems (DBMS) hold tables of data (relational DBMS) or objects (object DBMS) DBMS use files to store data or objects, but they hide the physical processes for storing data beneath a layer of abstraction Objects can also be serialized directly to files © Bennett, Mc. Robb and Farmer 2002 4

Persistence Architecture n n n The choice of the architecture for persistence is a system design issue The design of storage for specific classes and associations within the framework of that architecture is a class design issue The overall design may be constrained by having to operate with existing systems or using existing DBMS © Bennett, Mc. Robb and Farmer 2002 5

Persistence Design Questions Can files be used for some storage? n Is it truly an O-O system or just an interface to a relational database using Java or C++? n Will the system use an existing DBMS? n Will it use a relational DBMS? n Will it use an object DBMS? n © Bennett, Mc. Robb and Farmer 2002 6

Persistence Design Questions What is the logical layering of the system? n What is the physical layering of the system? n Is the system distributed? Does this include distributed data storage? n What protocols will be used to communicate within the system? n © Bennett, Mc. Robb and Farmer 2002 7

File Systems Files and record structures n Fixed length (padded) n Variable length (delimited) n Header and detail n Tagged data (XML) <Author> 1234567890123456789012345 ”Simon”, ”Bennett”, ”Leicester”, ”GB”, 1, ”Simon”, ”Bennett” <Forename>Simon</Forename> Simon 213, ” 22 -01 -2002” 2, 1, ” 0077098641”, 2002 Bennett Leice <Surname>Bennett</Surname> ster 2, 2, ” 0077096738”, 2001 GB 21322012002 </Author> © Bennett, Mc. Robb and Farmer 2002 8

File Systems File organization n Serial—new records appended n Sequential—records ordered in file, usually according to a numeric key n Random—uses an algorithm to convert a key to an address in the file © Bennett, Mc. Robb and Farmer 2002 9

File Systems File access methods n Serial—to read serial and sequential files n Index-sequential—using indexes to find records in a sequential file and improve access time n Direct—using relative or hashed addressing to move directly to the required record in the file © Bennett, Mc. Robb and Farmer 2002 10

Searching for Hamer using index-sequential file access Index-sequential Access © Bennett, Mc. Robb and Farmer 2002 11

Direct Access © Bennett, Mc. Robb and Farmer 2002 12

Improving Access Creating a linked list in random files to make it possible to read records in sequential order n Adding a secondary index keyed on a field that is not the key on which the main access method is based n Indexes can be inverted files or use other techniques such as B-trees n © Bennett, Mc. Robb and Farmer 2002 13

File Types Master files n Transaction files n Index files n Temporary file or work files n Backup files n Parameter files n © Bennett, Mc. Robb and Farmer 2002 14

File Example n n Using files in Java to handle localization of prompts and messages Use the java. util. Locale class to hold information about the current locale – – – language_country_variant fr_FR_EURO fr_CA en_UK en_AU © Bennett, Mc. Robb and Farmer 2002 15

File Example The Java class java. util. Resource. Bundle uses the locale to load a file with localespecific values e. g. UIResources_fr_FR_EURO n Java code to use this: n resources = Resource. Bundle. get. Bundle(”UIResources”, current. Locale); Button cancel. Button = new Button(resources. get. String(”Cancel”); rather than Button cancel. Button = new Button(”Cancel”); © Bennett, Mc. Robb and Farmer 2002 16

Resource File for French is UIResources_fr_FR_EURO n Contains n Cancel = Annuler OK = OK File = Fichier … © Bennett, Mc. Robb and Farmer 2002 17

Database Management Systems (DBMS) n Problems with files: – redundancy—number of files grows with applications, and data is duplicated – inconsistency—data is updated in one application’s files, but not in another’s – maintenance problems—changes to data structures mean changes to many programs – difficulty combining data—business needs may mean users want data from different applications © Bennett, Mc. Robb and Farmer 2002 18

DBMS Corporate database consolidates data for different applications n Each application then has its own view of a subset of the data n Application 1 Database Application 2 © Bennett, Mc. Robb and Farmer 2002 19

DBMS Schema n Ultimately data in databases is stored in files, but their structure is hidden from developers External Schema The view on data used by application programs. Conceptual Schema The logical model of data that is separate from how it is used. Internal Schema The physical storage of data in files and indexes. © Bennett, Mc. Robb and Farmer 2002 20

DBMS Features Data Definition Language (DDL) n Data Manipulation Language (DML) n Integrity Constraints n Transaction Management n Concurrency n Security n Tuning of Storage n © Bennett, Mc. Robb and Farmer 2002 21

Advantages of DBMS n n n Eliminate unnecessary duplication of data Enforce data integrity through constraints Changes to conceptual schema need not affect external schema Changes to internal schema need not affect the conceptual schema Many tools are available to manage the database © Bennett, Mc. Robb and Farmer 2002 22

Disadvantages of DBMS Cost of investing in the DBMS n Running cost, including staff (Database Administrators) to manage the DBMS n Processing overhead in converting data to format required by programs n © Bennett, Mc. Robb and Farmer 2002 23

Types of DBMS n Relational—represent data in tables – tables consist of rows of data organized in columns © Bennett, Mc. Robb and Farmer 2002 24

Types of DBMS n Object—store objects as objects – designed to handle complex nested objects for graphical and multimedia applications n Object-relational—hybrid databases that can store data in tables but can also store objects in tables © Bennett, Mc. Robb and Farmer 2002 25

Relational DBMS To store objects in a relational database, the objects have to be ‘flattened’ into tables n Complex objects have to be taken apart and the parts stored in different tables n When retrieved from the database, the object has to be reassembled from the parts in different tables n © Bennett, Mc. Robb and Farmer 2002 26

Normalization Data from complex structures is ‘flattened’ into tables n Typically normalization is carried out as far as ‘Third Normal Form’ n In an object-oriented system, we may use normalization to convert classes to table schemas n © Bennett, Mc. Robb and Farmer 2002 27

Normalization Example © Bennett, Mc. Robb and Farmer 2002 28

Objects as a Table n To get to First Normal Form, break out the repeating groups © Bennett, Mc. Robb and Farmer 2002 29

First Normal Form © Bennett, Mc. Robb and Farmer 2002 30

Second Normal Form © Bennett, Mc. Robb and Farmer 2002 31

Third Normal Form © Bennett, Mc. Robb and Farmer 2002 32

Alternative Approach n n Classes with simple data structure become tables Object IDs become primary keys Where classes contain another class as an attribute create a table for the embedded class For collections create two tables, one for the objects in the collection, the other to hold Object IDs of the containing objects and the contained objects © Bennett, Mc. Robb and Farmer 2002 33

Alternative Approach n n n One-to-many associations can be treated like collections Many-to-many associations become two separate tables for the objects and a table to hold pairs of Object IDs One-to-one associations are implemented as foreign-key attributes—each class gains an extra attribute for the Object ID of the other © Bennett, Mc. Robb and Farmer 2002 34

Alternative Approach n To implement inheritance – only implement the superclass as a table including all subclass attributes – only implement the subclasses as tables, duplicating superclass attributes in each – implement superclass and subclasses as tables with shared primary keys n Each approach has disadvantages © Bennett, Mc. Robb and Farmer 2002 35

Object DBMS n n ODBMS have the advantage that objects can be stored directly Object Data Management Group (ODMG) standard Not all object databases conform to the standard Object databases are closely linked to programming languages with ways of navigating through the database © Bennett, Mc. Robb and Farmer 2002 36

Sample C++ Object. Store Operation Int. Campaign * Creative. Staff: : find. Int. Campaign ( string campaign. Code ) { Int. Campaign * int. Campaign. Pointer; int. Campaign. Pointer = staff. Int. Campaign. List. get. Value(). query_pick( “Int. Campaign*”, “campaign. Code == this->campaign. Code”, os_database: : of(this)); return int. Campaign. Pointer; } © Bennett, Mc. Robb and Farmer 2002 37

Object DBMS Some will transparently ‘materialize’ objects from the database when they are referred to n Update transactions need to be bracketed with start and finish transaction methods n Operations are still implemented in object-oriented languages n © Bennett, Mc. Robb and Farmer 2002 38

Designing Data Management Classes n Alternatives (two in bold are covered here): – add save and retrieve operations to classes – make save and retrieve class-scope methods – allow all persistent objects to inherit from a Persistent. Object superclass – use collection classes to manage persistence – use broker classes to manage persistence – use a parameterized class to handle persistence for different classes © Bennett, Mc. Robb and Farmer 2002 39

Persistent. Object n Create an abstract superclass and make all persistent classes inherit from it © Bennett, Mc. Robb and Farmer 2002 40

Persistent. Object Materialization as Class Method n Sequence diagram : List. Campaigns Location Campaign Dialog Manager Get. Number. Of find. By. Location( Campaigns( ) Code location. Code, (loc. Code) location. Name, int. Campaign. List[ ]) : Location number. Of. Campaigns( ) © Bennett, Mc. Robb and Farmer 2002 41

Database Broker n Use a broker class responsible for materializing instances of each class from the database Location materializes Location. Broker - instance: Location. Broker - Location. Broker( ) + instance( ): Location. Broker + find. By. Location. Code( String ): Location + iterate. Location( ): Location © Bennett, Mc. Robb and Farmer 2002 42

Database Broker Materializes Instances n Sequence diagram : List. Campaigns : Location. Broker Campaign Dialog Manager Get. Number. Of find. By. Location( Campaigns( ) Code location. Code, (loc. Code) location. Name, int. Campaign. List[ ]) : Location number. Of. Campaigns( ) © Bennett, Mc. Robb and Farmer 2002 43

Inheritance Hierarchy of Database Brokers © Bennett, Mc. Robb and Farmer 2002 44

Relational. Broker and Other Classes oracle: : jdbc: : driver: : Oracle Driver Relational. Broker java: : sql: : Connection Location Broker java: sql: : Statement materializes Location java: : sql: : Result. Set © Bennett, Mc. Robb and Farmer 2002 45

Package Diagram sun. jdbc «import» java. sql «import» Broker Framework Application Brokers © Bennett, Mc. Robb and Farmer 2002 46

Proxy Pattern n n Proxy objects act as placeholders for the real objects, e. g. Int. Campaigns in Locations The Int. Campaign. Proxy has the same interface as Int. Campaign, but no data When a Location requires data about one of its Int. Campaigns, it sends a message to the Proxy The Proxy requests the Broker to materialize the Int. Campaign and passes the message on © Bennett, Mc. Robb and Farmer 2002 47

Proxy Pattern n The Proxy can then replace the reference to itself in the Location with a reference to the real materialized object This approach can be combined with caching of objects The caches can be used by the Broker to check whether an object is already in memory and save materializing it from the database if it is © Bennett, Mc. Robb and Farmer 2002 48

Adding Caches n Six caches – new clean cache – new dirty cache – new deleted cache – old clean cache – old dirty cache – old deleted cache © Bennett, Mc. Robb and Farmer 2002 49

Transaction Commit Cache Actions n Six caches n – new clean cache – new dirty cache – new deleted cache – old clean cache – old dirty cache – old deleted cache Cache actions – write to database – delete from cache – write to database – delete from database © Bennett, Mc. Robb and Farmer 2002 50

Class Diagram with Caches and Proxies 6 Cache «Interface » Int. Campaign. Interface «realize» Location Database. Broker Relational. Broker «realize» «Proxy» Int. Campaign. Proxy runs in 1 Int. Campaign. Broker materializes Int. Campaign © Bennett, Mc. Robb and Farmer 2002 51

Collaboration Diagram 1. 2 in. Memory( ) 1 print. List( ) 1. 1 get. Title( ) : Location : Int. Campaign. Proxy 1. 3 [not in memory] get. Int. Campaign ( objectid ) 1. 6 get. Title( ) : Int. Campaign. Broker 1. 5 [not in cache] retrieve. Int. Campaign ( objectid ) 1. 4 * in. Cache ( objectid ) : Cache © Bennett, Mc. Robb and Farmer 2002 52

Using a Framework Why develop a framework when you can use an existing one? n Object-table mappings n – Toplink – Coco. Base n Products that will map attributes of classes to columns in a relational database table © Bennett, Mc. Robb and Farmer 2002 53

Using a Framework J 2 EE Application Servers n Enterprise Java. Beans (EJBs) can be used – Entity Beans for business objects n Container-Managed Persistence (CMP) is a framework for J 2 EE containers to handle the persistence of instances of entity beans n Use J 2 EE Patterns (Alur et al. , 2001) n © Bennett, Mc. Robb and Farmer 2002 54

Summary In this lecture you have learned about: n The different ways of storing persistent objects n The differences between object and relational databases n How to design data management objects n How to extend sequence diagrams to include data management objects © Bennett, Mc. Robb and Farmer 2002 55

References Silberschatz et al. (1996) n Howe (2001) n Eaglestone and Ridley (1998) n Alur, Crupi and Malks (2001) n (For full bibliographic details, see Bennett, Mc. Robb and Farmer) © Bennett, Mc. Robb and Farmer 2002 56
- Slides: 56