23
Database Management 3-tier application architecture A technical view

Database Management 3-tier application architecture A technical view

Embed Size (px)

Citation preview

Page 1: Database Management 3-tier application architecture A technical view

Database Management

3-tier application architectureA technical view

Page 2: Database Management 3-tier application architecture A technical view

Themes

• Architecture : The large scale structure of a system, especially a computer system

• Communications : The interaction between components

• Database: enabling applications with database driven applications

Page 3: Database Management 3-tier application architecture A technical view

Displaying a Web page in a Browser

• Click on a link on a page– <a href= http://www.cems.uwe.ac.uk/~cjwallac/UFIE8K/workplan.html>Workplan</a>

• URL is decoded: protocol:://host/file• Host is decoded using DNS to get the host IP address

164.11….• Page requested from HTTP server on the host (apache)• Page is returned

• HTML codes determine how text is rendered• If page contains links e.g. <img src=red.gif>, browser must

repeat the same process on each

Page 4: Database Management 3-tier application architecture A technical view

Dynamic Web page needed when:

• consistent look and feel on each page of a large site

• data is derived from a database

• depends on real time

• pages depend on user choice

• business transactions e.g. e-commerce…

Page 5: Database Management 3-tier application architecture A technical view

3-tier architecture

• A Presentation layer using Browser technology

• An Application layer using a web application server platform + application programs

• A Persistence layer using a relational database or other data store technology

Page 6: Database Management 3-tier application architecture A technical view

Bus timetableBUS (Level 1 Diagram)

usersa

information need

presentation layer

1

application layer

2

HTTP requests

persistance layer

3

SQL requests tables

HTML files

information

service providers

b timetablesBrowser such as IE6, FoxFire

Apache or MS IIS server +PHP

MySQL RDBMS

Page 7: Database Management 3-tier application architecture A technical view

Presentation layer arch

• Issue request to remote server • accept the returned HTML (or JPEG..) file • render (i.e. create a 2-d image ) the HTML• allow plug-ins to handle new file types• execute client-side scripts in JavaScript• support interaction between client-side scripts

and the web page• accept user input via a variety of controls on a

form

Page 8: Database Management 3-tier application architecture A technical view

Persistence layer arch

• support application interaction with the database using standard languages e.g SQL queries and table returns

• define and modify the data structures (e.g. tables) themselves ( the Database Schema)

• insert, update and delete data• maintain data persistently, with backup and recovery• handle transactions to support concurrent access to the

database via locking et• optimise access by compilation of queries, indexing,

replication of tables etc.

Page 9: Database Management 3-tier application architecture A technical view

Application Layer arch

• accepting requests from the Presentation layer• interpreting the requests according to business rules and

past transactions from this client• requesting the appropriate data from the Persistence

layer using SQL • Handling the returned result codes and tables• computing derived data• creating the HTML (or GIF, MIDI..) for the page • providing run-time support for:

– thousands of concurrent users – compilation to machine code– multi-threading [ allow multiple processes to run concurrently]– caching [holding results in a temporary store to reduce re-

calculation]

Page 10: Database Management 3-tier application architecture A technical view

Web Application Platforms

• Server side includes - files with a .shtml extension • Cgi using Perl or other scripting language • PHP like SSI, a scripting language which is embedded in

an HTML page. • Microsoft’s ASP (Active Server Pages ) • J2EE

– JSP for page scripting– Java + class library– Multiple runtime vendors Tomcat, BEA

• .NET is the latest technology from MS. – range languages (VB.NET, C#, , C++, Cobol... )– compiled to a common intermediate code (MSIL)– Extensive class library– ASP.NET scripting page scripting

Page 11: Database Management 3-tier application architecture A technical view

3-tier Issues• Advantages:

– Re-use of appropriate software – Software can be located on different machines for

convenience and performance – Resolves one-many relationships – many clients –

one application, many applications – one database

• Challenges:– well-defined, standardised interfaces between layers

required. – Software must be built to conform to interface

standards– The designer has to decide where to locate specific

functions

Page 12: Database Management 3-tier application architecture A technical view

Communication

• The ‘glue’ in this architecture is communication between software in the layers

• A single request from a user results in a complex flurry of communications and executions

• The flurry is composed of hundreds of simple interactions

• Sequence diagrams can be useful to provide a simplified description

Page 13: Database Management 3-tier application architecture A technical view

Sequence diagram of SMS bus times requestsmsrequest

user :

078. / mobile phone : Message centre : Clickatell : web03 / web server : smsrequest : mysql :

bus 99()

076.. bus 99()

078 .. 076.. bus 99()

mo.php?text=bus+99&from=078()

mo.php :

text=bus 99, from=078()

text=99&from=078&code=BUS()

select dtime from service, timetable...()

table dtime, destination

Reply: next 99 departures ..

req?text=next+99+dep&to=078()

message centre 2 :

to 078 next 99 dep()

next 99 departures()

read()

Page 14: Database Management 3-tier application architecture A technical view

Other layered architectures

• ANSI/SPARC Database model– The external or user level, characterised by

Views– The Conceptual level, comprising the full

corporate data model at a logical level– The Physical level, where data is stored in

files

Page 15: Database Management 3-tier application architecture A technical view

Channels between Layers

• Layered communications model (Tanenbaum)• Application Layer• Transport Layer• Network Layer• Data Link Layer• Physical Layer

• Presentation <> Middleware– HTTP (The protocol which supports GET and POST

messages) carrying a limited number of types of content (MIME types)

• Middleware <> Data stores– ODBC with SQL request and table returns (whole

table or row at a time)

Page 16: Database Management 3-tier application architecture A technical view

SMS straw poll

• A simple application to tally votes on an issue.

• A simple database to hold details of candidates and votes.

• Votes are texted into the server from a mobile phone, and are processed by cast.php

• At any time, the current status of the poll can be displayed using tally.php

Page 17: Database Management 3-tier application architecture A technical view

Casting a votecast

mobile / 4476.. : SMS network : CEMS SMS server : cast.php : shares/cjwallac :

VOTE G()

text=vote+B&from=4476..()

text=B&from=4476..()

select ..()

(table)

insert ..()

(you voted for Bush)

(to=4476.. message =you voted for Bush)(you voted for Bush)

to the UWE SMS number 44762..

Page 18: Database Management 3-tier application architecture A technical view

The simple database

poll

tcandidatecandid:CHARACTERcandname:VARCHAR

tvotervoteid:CHARACTER

Page 19: Database Management 3-tier application architecture A technical view

The generated SQLCREATE TABLE tcandidate(

candid CHAR(1) NOT NULL,candname VARCHAR(40) NOT NULL,CONSTRAINT pk_tcandidate PRIMARY KEY (candid)

) TYPE=INNODB;

CREATE TABLE tvoter(voteid CHAR(10) NOT NULL,candid CHAR(1) NOT NULL,CONSTRAINT pk_tvoter PRIMARY KEY (voteid)

) TYPE=INNODB;

ALTER TABLE tvoter ADD INDEX (candid), ADD CONSTRAINT fk1_tvoter_to_tcandidate FOREIGN KEY(candid) REFERENCES tcandidate(candid) ON DELETE RESTRICT ON UPDATE RESTRICT;

Page 20: Database Management 3-tier application architecture A technical view

Initialising the database

delete from tcandidate;

insert into tcandidate values('B', 'George Bush');insert into tcandidate values('K', 'John Kenney');

Page 21: Database Management 3-tier application architecture A technical view

The cast script (simplified)<?php/* input text the vote from the originating mobile number*/

// map text to candidate id $query = "select * from tcandidate where '$text' = candid"; $dbresult = mysql_db_query($db,$query,$dblink); $cand = mysql_fetch_object($dbresult); $cid = $cand->candid;

// insert the vote for this mobile $query = "insert into tvote values('$from', '$cid')";

print "Reply: you cast your vote for $cand->candname";

?>

Page 22: Database Management 3-tier application architecture A technical view

The tally script (simplified)<?php// get the total number of votes cast $query = "select count(*) as total from tvote"; $dbresult = mysql_db_query($db,$query,$dblink); $total_tally = mysql_fetch_object($dbresult); $total = $total_tally->total;

// get the total for each candidate $query = "select *, count(*) as tally from tvote natural join tcandidate group by tcandidate.candid"; $dbresult = mysql_db_query($db,$query,$dblink);

//generate the results table while($cand = mysql_fetch_object($dbresult)) { $tally = $cand->tally; $tpc= round(( $tally/ $total ) * 100,2); $tpcr = round($tpc,0); print "$cand->candname has $tpc % <br/>"; } print "$total votes cast";?>

Page 23: Database Management 3-tier application architecture A technical view

Exam application

• Input is a spreadsheet

• Design a suitable normalised data model