Upload
adam-mcginnis
View
229
Download
1
Tags:
Embed Size (px)
Citation preview
Clients and Server
Clients and serversA 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 serversFile serversDatabase serversGroupware serversWeb serversMail serversObject serversPrint servers
Web serversIn ecommerce terms the most important type of serverDeal with in detail laterStores HTML files and dispenses them to clientsProcesses forms detailsCommunicates with other servers, for example database servers
Database serversNext to web servers the most important type of server for ecommerceExplained in more detail laterStores relational databasesResponds to queries in language called SQL
Tiered archtiecturesAn example of separation of concerns.Most popular model has three layersDeveloped for maintenance reasons
Three-tier model
Business objects
Business objects DatabaseDatabase
Clients
Presentation layer Processing layer Data layer
Three tiersPresentation layer contains HCI for clientProcessing layer contains business objectsData layer contains some stored data
RationaleHCI can go on the client and does not require to be transmitted over networkBusiness objects reflect domain entitiesBusiness objects shield the implementation of data
Business objectsReflect entities in application, for example in a sales site: catalogue, product and customerAll application programming done on business objectsDetails of underlying data hidden to the application programmer, for example the programmer should be unaware of the database technology
Database Servers
Database serversHave been in existence for some time.Described here as they play a major part in ecommerce systemsVirtually all based on the relational model
Relational tables
ItemId Item NoInStock
Aw222 Washer A 300
Ntr444 Nut A 2009
Edt666 Spanner S 802
Bt555qw Bolt B 200
KeyEach row contains associated data
SQLStructured Query LanguageIn existence for many yearsUsed to create, modify and retrieve data from relational tablesNo standardised
An example
Select EmployeeName, Salary From Employees WHERE Salary>3500
Names of columns
Name of table
Condition
Typical configuration
Web server Databaseserver
Clients
Functions of a database server
Interpret SQL statements and execute themOptimise queriesTo prevent concurrency errorsTo detect deadlockTo administer securityTo administer backup
Stored proceduresSnippets of ‘compiled’ code which are equivalent to subroutinesStored at serverEfficient alternative to sending large numbers of SQL statements over communication media
Stored procedures: for and against
PlusMore efficient in processing timeReduces network trafficKeeps database code close to the database
MinusThey are non-standardOptimisation needs to be repeated as access strategies to a database change
Referential integrityTables in a relational database are consistent with each otherAssociated with business rulesTwo ways of implementing it: declarative referential integrity and trigger-based referential integrity
ImplementationDeclarative referential integrity is maintained by declarations in database schemasTrigger based referential integrity is achieved by embedding code
Pros and consTrigger based
Non-standardTriggers found scattered throughout a systemSome implementations have upper limit to number of triggers
Declaration basedSelf-documentingStandard
Relational middleware
Serversoftware
Serversoftware
Clients
DatabaseDatabase
SQL API
Driver
Stacks
The componentsThe API provides programmer facilitiesThe driver communicates SQL statements to the server softwareThe stack contains protocol softwareServer software carries out the main functions, for example executing SQL queries
Distributed databases
Databases spread around servers in a distributed systemDatabases distributed for performance reasons: keeping data close to clientsDistributed for reliability using replicated dataDistributed because of legacy: many systems have evolved from separate systems
Problems with distributed data
Keeping replicated data up-to-dateEnsuring concurrent access keeps a database in its correct stateSecurity is a big headacheReliability is a problemClock synchronisation is a problem
Types of distributionDownloading: periodically writing data to a remote databaseData replication: keeping identical sets of data in stepFragmentation: splitting data into sub-tables.
FragmentationHorizontal fragmentation, splitting tables lengthways - split tables have the same columns as original tableVertical 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 driversStatement, an SQL statementPreparedStatement, ‘compiled SQL statements’CallableStatement, stored procedure
Java as a medium for database development (ii)Connection, facilities for connecting to a databaseResultSet, collection of data retrieved from a queryDatabaseMetaData, data about a databaseDriverManager, manages connections to a database
Developing Java code to access a database
Load a driverEstablish a database connectionAssociate an SQL statement with this connectionExecute the statementProcess a result setClose the connection
Loading the driver //Set the name of the file that is to be accessed
//and the name of the driverString 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 objectStatement selectStatement = con.createStatement();// Execute the SQL select statementResultSet 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 statementselectStatement.close();con.close();rs.close();
Meta dataData about dataCan be data about a database, a result set or a driverJava contains classes which enable such data to be easily extracted
An example
Connection c;// Code to establish a connectionDatabaseMetaData dmd = c.getMetaData();System.out.println(“Driver is ”+ dmd.getDriverName() +
“ Version number = “+dmd.getDriverVersion());
Obtaining data about the driver: its name and version number
Three tier with relational database
Clients
Businessobjects
Relationaldatabases
Mapping
Mappings
Classes usually mapped to tablesInstance variables to columnsRelationships to common data in tables