1 chapter 10 protecting data integrity in a multiuser environment

46
1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

Upload: india-obey

Post on 01-Apr-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

1Chapter 10Chapter 10

Protecting Data Integrity in a

Multiuser Environment

Protecting Data Integrity in a

Multiuser Environment

Page 2: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Chapter ObjectivesChapter Objectives

• Develop code that implements database transactions

• Develop code that commits and rolls back transactions

• Identify the types of problems that occur when multiple users access a database concurrently

Page 3: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Chapter ObjectivesChapter Objectives

• Implement database, recordset, page-level, and record-level locking strategies

• Develop error-handling procedures that support concurrent use of a database

• Replicate and synchronize replicas and partial replicas of a database

Page 4: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Using Transactions to Protect Against System Failure

Using Transactions to Protect Against System Failure

• Transaction Logical unit of work Each unit of work comprises one or more

physical database operations

• When a transaction completes successfully, changes that the transaction made to the database are committed to the database

Page 5: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Using Transactions to Protect Against System Failure

Using Transactions to Protect Against System Failure

• Whenever a transaction aborts, changes that it made to the database before aborting are not committed to the database Changes are rolled back

Database fields that contain changes return to their original state

Page 6: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Transaction Processing in VBA

Transaction Processing in VBA

• Three methods of the Connection object to define the beginning or end of a transaction in VBA: BeginTrans

Denotes the beginning of the transaction CommitTrans

Denotes the end of a successful transaction Rollback Trans

Denotes the end of an unsuccessful transaction

Page 7: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Additional Transaction Features and Caveats

Additional Transaction Features and Caveats

• UseTransaction property When set to Yes, a transaction is automatically

defined for the queryTherefore, BeginTrans and CommitTrans methods

are unnecessary

• FailOnError property When set to Yes, the query results are

automatically rolled back when the first error occurs

Page 8: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Additional Transaction Features and Caveats

Additional Transaction Features and Caveats

Figure 10-1 Setting the Use Transaction and Fail On Error properties in query Design view

Page 9: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Additional Transaction Features and Caveats

Additional Transaction Features and Caveats

• Access supports a maximum of five levels of nested transactions

• When one transaction is nested inside another, commitments of transactions are temporary until the final CommitTrans method executes If that method does not execute or the

corresponding RollbackTrans method executes, all nested transactions are rolled back

Page 10: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Additional Transaction Features and Caveats

Additional Transaction Features and Caveats

• Connection object Controls a transaction

• Recordsets that use a particular connection are rolled back when the RollbackTrans method is applied to the Connection object

• When data are updated through bound forms, Access developers have little control over the definition of a transaction Access controls the updates

Page 11: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Solving Concurrent Processing ProblemsSolving Concurrent Processing Problems

• Concurrent transactions Transactions that overlap

• Inconsistent analysis Type of concurrency issue

• Locking mechanisms Prevent more than one transaction at a time

from modifying a data object

Page 12: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Solving Concurrent Processing ProblemsSolving Concurrent Processing Problems

• Granularity Level of locking

• Write lock Allows other users to read but not update the

locked records Prevents the lost update problem, but not

prevent the inconsistent analysis problem

Page 13: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Solving Concurrent Processing ProblemsSolving Concurrent Processing Problems

• Read lock Prevents other users from reading or writing

locked records Prevents both the inconsistent analysis problem

and the lost update problem

• Pessimistic locking Locks a page or record as soon as the first field

in the record is changed and releases a lock after the record is written

Page 14: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Solving Concurrent Processing ProblemsSolving Concurrent Processing Problems

• Optimistic locking Locks a page or record when the Update method is

encountered in VBA and releases the lock after record is written

• Isolation level Indicates the degree to which the transaction should be

independent of other transactions

• Dirty read Prevented by specifying that transaction should not

read changes made by another transaction until the other transaction is complete

Page 15: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Locking Records at the Database Level

Locking Records at the Database Level

• You can lock the entire database through the Open window

Figure 10-2 Opening a database in Exclusive made

Page 16: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Locking Records at the Database Level

Locking Records at the Database Level

• You can lock databases through startup command lines and VBA code

Figure 10-3 Record locking defaults within Advanced tab of Options dialog box

Page 17: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Locking Records at the Database Level

Locking Records at the Database Level

Figure 10-4 Opening a database in Exclusive mode through a shortcut

Page 18: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Locking Records at the Database Level

Locking Records at the Database Level

• Mode property of the Connection object Used to specify whether other users can access

the database When set to the intrinsic constant

adModeShareExclusive, other users cannot open a connection to the database

Page 19: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Locking Records at the Database Level

Locking Records at the Database Level

Table 10-1 Legal values for the Mode property

Page 20: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Setting the Locking Characteristics of Bound Forms

Setting the Locking Characteristics of Bound Forms

• RecordsLocks property of a form and Advanced tab of the Options dialog box are used to set locking characteristics of bound forms

Table 10-2 Setting the locking characteristics of bound forms

Page 21: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Setting the Locking Characteristics of Bound Forms

Setting the Locking Characteristics of Bound Forms

• Recordset-level locks Specified by setting the RecordLocks property to All

Records in the form property sheet or to 1 in VBA

• Edited Record option Locks a record (or page) as soon as a user begins to

change the value of a field

• No Locks option Does not lock the record until all of the changes have

been made

Page 22: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 Record-Level Locking Techniques in VBA

Record-Level Locking Techniques in VBA

• Locktype property Used to adjust locking strategy of recordsets

other than those associated with bound forms adLockPessimistic

Employs pessimistic locking

adLockOptimisticEmploys optimistic locking

Page 23: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10The CursorType PropertyThe CursorType Property

• CursorType property of a recordset object Indicates how to respond when others make changes

• Values of CursorType relevant for Jet Engine adOpenStatic indicates that once recordset is

created, it should not reflect changes, new records, or deletions made by other users

adOpenKeyset indicates that the transaction cannot view the results of another transaction until the other transaction is committed

Page 24: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10The IsolationLevel PropertyThe IsolationLevel Property

• IsolationLevel property Used to control how Access behaves in a

multiple-user environment Indicates how you should respond when others

make changes Property of a Connection object

Page 25: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10The IsolationLevel PropertyThe IsolationLevel Property

• Specifies when recordsets can view changes by users If it is set to adXactReadUncommitted:

Recordset can view changes by others before they are committed in a transaction

A dirty read is possible adXactReadCommitted

Indicates that the transaction cannot view the results of another transaction until the other transaction is committed

Page 26: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10The IsolationLevel PropertyThe IsolationLevel Property

Table 10-3 IsolationLevel values and descriptions

Page 27: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Problems with LockingProblems with Locking

• Deadlock Situation where each transaction is waiting for the other

to release a locked resource and neither transaction can finish

• Update Retry Interval The time, in milliseconds, that Access waits before

trying to obtain the record again

• Number of Update Retries Number of times Access will attempt to obtain a record

Page 28: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Handling Concurrency ErrorsHandling Concurrency Errors

• If an error handler detects a locked database, recordset, or record, the error-handling routine typically waits and then repeats the statement through the use of the Resume statement

• SQLState property of an Error object Returns a value that identifies the error according

to the ANSI SQL standard

Page 29: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Handling Concurrency ErrorsHandling Concurrency Errors

Table 10-4 Commonly used values of SQL State

Page 30: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Handling Concurrency ErrorsHandling Concurrency Errors

• Number property of an Error object Returns a long integer that is determined by the

database provider and the connection

• Status property of a recordset object May be consulted to gather information about

the success or failure of an operation

Page 31: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Handling Concurrency ErrorsHandling Concurrency Errors

Table 10-5 Values of the recordset Status property

Page 32: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Handling Concurrency ErrorsHandling Concurrency Errors

Table 10-5 Values of the recordset Status property (continued)

Page 33: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Database ReplicationDatabase Replication

• Database replication Creates copies of all or part of a database Each copy cab reside on a different computer

• Synchronization A process used to periodically distribute

updates from one copy to other copies of the database

Page 34: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 An Overview of Replication and Synchronization Steps

An Overview of Replication and Synchronization Steps

• Design Master Copy of the database that can propagate structural

changes

• Two issues must be resolved during the process of creating the Design Master: The design of the database should be analyzed to

determine whether it adequately supports replication You must determine which objects will be replicated

Page 35: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 An Overview of Replication and Synchronization Steps

An Overview of Replication and Synchronization Steps

• Design Master replica set Consists of databases that share the same

Design Master database

• Types of replicas: Complete replica

Contains all of the data from replicable tables Partial replica

Contains only the rows that meet a particular criterion

Page 36: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 An Overview of Replication and Synchronization Steps

An Overview of Replication and Synchronization Steps

• Types of views replicas can have: Global replicas

Can be used to create other replicas and can be synchronized with any replica

Can create and then serve as a synchronization hub for local replicas

Local replicasCan create only other local replicas and must be

synchronized with the global replica that was used to create it

Page 37: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 An Overview of Replication and Synchronization Steps

An Overview of Replication and Synchronization Steps

• Replicas created from a particular global replica, directly or transitively through a local replica, define a replica set

Figure 10-6 Replica sets

Page 38: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10 An Overview of Replication and Synchronization Steps

An Overview of Replication and Synchronization Steps

• During synchronization, a conflict occurs when: The same record and column in two different

replicas have been updated Two records share the same primary key and when

one replica deletes a record that has a referential integrity relationship with a record in another replica

Page 39: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Replication Within VBAReplication Within VBA

• Microsoft Jet and Replication Object Library (JRO) Contains object that support methods that

perform activities similar to the activities carried out when you replicate and synchronize through the menus

Must be checked in the References window before you can use the replication objects

Page 40: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Replication Within VBAReplication Within VBA

Table 10-6 Replication objects and methods

Page 41: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Replication Within VBAReplication Within VBA

• You can create partial replicas by using the CreateReplica, Append, and PopulatePartial methods Partial replicas are not populated through the

CreateReplica method PopulatePartial method adds data to the replica

Page 42: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Chapter SummaryChapter Summary

• Features such as transactions, locking, and replication help Access developers prepare an application to function in complex environments

• Notion of a transaction Controls how concurrent access occurs

• Transaction Logical grouping of database operations that define a

unit of work

Page 43: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Chapter SummaryChapter Summary

• One way that transactions control database processing is by defining when changes affected by the transaction may actually be applied to the database

• CommitTrans and RollbackTrans connection methods Handle problems with committing a

transaction’s effects to a database

Page 44: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Chapter SummaryChapter Summary

• In Access, to control problems caused by the actions of multiple users, transactions use a locking scheme Pessimistic locking

Locks a record as soon as it is edited

Optimistic lockingLocks a record when it is about to be updated

Page 45: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Chapter SummaryChapter Summary

• Replication Used when more than one site needs access to

an application, but network access is not available or desired

ReplicaCopy of an application

Partial replicaFilters a table, so that the records in the replica are a

subset of the original table’s records

Page 46: 1 Chapter 10 Protecting Data Integrity in a Multiuser Environment

10Chapter SummaryChapter Summary

• Synchronization of replicas Process where updates made in one replica are

propagated to other replicas

• Replication difficulties occur when two replicas modify the same column and record

• A conflict resolution function is used to determine whether the correct value was placed in the database