An Introduction to Distributed Applications and Ecommerce

Download Report

Transcript An Introduction to Distributed Applications and Ecommerce

Clients and Server
Clients and servers
A server provides a service such as
dispensing files.
A client calls on a service.
The distinction is not hard and fast a
server may act as a client to another
server.
A server acting as a client
In an ecommerce application a Web
server might call on the service of a
database server in order to access some
data such as catalogue records
Some servers
File servers
Database servers
Groupware servers
Web servers
Mail servers
Object servers
Print servers
Web servers
In ecommerce terms the most
important type of server
Deal with in detail later
Stores HTML files and dispenses them
to clients
Processes forms details
Communicates with other servers, for
example database servers
Database servers
Next to web servers the most important
type of server for ecommerce
Explained in more detail later
Stores relational databases
Responds to queries in language called
SQL
Tiered archtiectures
An example of separation of concerns.
Most popular model has three layers
Developed for maintenance reasons
Three-tier model
Clients
Business
objects
Presentation layer
Processing layer
Database
Data layer
Three tiers
Presentation layer contains HCI for
client
Processing layer contains business
objects
Data layer contains some stored data
Rationale
HCI can go on the client and does not
require to be transmitted over network
Business objects reflect domain entities
Business objects shield the
implementation of data
Business objects
Reflect entities in application, for example in
a sales site: catalogue, product and customer
All application programming done on business
objects
Details of underlying data hidden to the
application programmer, for example the
programmer should be unaware of the
database technology
Database Servers
Database servers
Have been in existence for some time.
Described here as they play a major
part in ecommerce systems
Virtually all based on the relational
model
Relational tables
Key
ItemId
Item
NoInStock
Aw222
Washer A
300
Ntr444
Nut A
2009
Edt666
Spanner S
802
Bt555qw
Bolt B
200
Each row contains
associated data
SQL
Structured Query Language
In existence for many years
Used to create, modify and retrieve
data from relational tables
No standardised
An example
Select EmployeeName, Salary From Employees WHERE Salary>3500
Name of table
Names of columns
Condition
Typical configuration
Clients
Web server
Database
server
Functions of a database server
Interpret SQL statements and execute
them
Optimise queries
To prevent concurrency errors
To detect deadlock
To administer security
To administer backup
Stored procedures
Snippets of ‘compiled’ code which are
equivalent to subroutines
Stored at server
Efficient alternative to sending large
numbers of SQL statements over
communication media
Stored procedures: for and
against
Plus
More efficient in
processing time
Reduces network
traffic
Keeps database
code close to the
database
Minus
They are nonstandard
Optimisation needs
to be repeated as
access strategies to
a database change
Referential integrity
Tables in a relational database are
consistent with each other
Associated with business rules
Two ways of implementing it:
declarative referential integrity and
trigger-based referential integrity
Implementation
Declarative referential integrity is
maintained by declarations in database
schemas
Trigger based referential integrity is
achieved by embedding code
Pros and cons
Trigger based
Non-standard
Triggers found
scattered
throughout a system
Some
implementations
have upper limit to
number of triggers
Declaration based
Self-documenting
Standard
Relational middleware
Clients
SQL
API
Driver
Stacks
Server
software
Database
The components
The API provides programmer facilities
The driver communicates SQL
statements to the server software
The stack contains protocol software
Server software carries out the main
functions, for example executing SQL
queries
Distributed databases
Databases spread around servers in a
distributed system
Databases distributed for performance
reasons: keeping data close to clients
Distributed for reliability using replicated data
Distributed because of legacy: many systems
have evolved from separate systems
Problems with distributed data
Keeping replicated data up-to-date
Ensuring concurrent access keeps a
database in its correct state
Security is a big headache
Reliability is a problem
Clock synchronisation is a problem
Types of distribution
Downloading: periodically writing data
to a remote database
Data replication: keeping identical sets
of data in step
Fragmentation: splitting data into subtables.
Fragmentation
Horizontal fragmentation, splitting
tables lengthways - split tables have the
same columns as original table
Vertical fragmentation, where the split
tables are associated with a subset of
the columns of the original table
Java as a medium for database
development (i)
Driver, used for database drivers
Statement, an SQL statement
PreparedStatement, ‘compiled SQL
statements’
CallableStatement, stored
procedure
Java as a medium for database
development (ii)
Connection, facilities for connecting
to a database
ResultSet, collection of data retrieved
from a query
DatabaseMetaData, data about a
database
DriverManager, manages
connections to a database
Developing Java code to
access a database
Load a driver
Establish a database connection
Associate an SQL statement with this
connection
Execute the statement
Process a result set
Close the connection
Loading the driver
//Set the name of the file that is to be accessed
//and the name of the driver
String fileURL = “...”;
String driverName = “...”;
try
{
// Load in the driver programmatically
Class.forName(driverName);
}
catch (ClassNotFoundException cfn)
{
//Problem with driver, display error message and
//return to operating system with status value 1
System.out.println(“Problem loading driver”);
System.exit(1);
}
Establishing a connection
try
{
//Establish a connection to the database, second
//argument is the name of the user and the third
//argument is a password (blank)
Connection con =
DriverManager.getConnection(fileURL, “Darrel”,””);
Create and execute an SQL
statement
// Create a statement object
Statement selectStatement =
con.createStatement();
// Execute the SQL select statement
ResultSet rs =
selectStatement.executeQuery
(“SELECT name, salary FROM
employees WHERE salary >35000");
Process the result set
String employeeName;
int employeeSalary;
while(rs.next())
{
employeeName = rs.getString(1);
employeeSalary = rs.getInt(2);
System.out.println(“Name = “+ employeeName +
“Salary = “+ employeeSalary);
}
Close down connections
//Close down the database connection, result set
//and the SELECT statement
selectStatement.close();
con.close();
rs.close();
Meta data
Data about data
Can be data about a database, a result
set or a driver
Java contains classes which enable such
data to be easily extracted
An example
Obtaining data about the driver:
its name and version number
Connection c;
// Code to establish a connection
DatabaseMetaData dmd = c.getMetaData();
System.out.println(“Driver is ”+ dmd.getDriverName() +
“ Version number = “+dmd.getDriverVersion());
Three tier with relational
database
Clients
Business
objects
Mapping
Relational
databases
Mappings
Classes usually mapped to tables
Instance variables to columns
Relationships to common data in tables