SOEN 6011Software Engineering ProcessesSection SS Fall 2007Dr Greg Butlerhttp://www.cs.concordia.ca/~gregb/home/soen6011-f2007.html
Week 10Deliverable 3
ConcurrencyACIDUnitOfWorkIdentity MapLazy Loading
Deliverable 3Read Java threads tutorial (if necessary)You need thread-safe Java code
Do SOEN 344 Tutorials 3 and 4UnitOfWorkIdentity Map, Lazy Loading
Deliverable 3Everyone use same players, eitherAliceBobAllow your players to play multiple games at the same time from separate browsersStore statistics in DBDecide whether to store Game state in DB
Deliverable 3 Fowler Session State PatternsProblem: where to store session state
Client session statestateless server stores state on the clientServer session stateKeep state on server in serialized formDatabase session stateStore state as committed data in DB
Client, Web server, Db server
Deliverable 3Should your code affect anyone elses? or their database content? or their serialized session state? or their client-side stored session state? or your other client-side stored session state?
Concurrency ProblemsEssential problems:Lost updatesInconsistent reads.Another point of view:CorrectnessLiveness
Offline concurrency the term we use is offline concurrency, that is, concurrency control for data thats manipulated during multiple database transactions.
Concurrency ControlTwo main approaches:Optimistic concurrency control.Pessimistic concurrency control.
First let us examine the concept of transaction.
TransactionKey concept for handling concurrency issues.2 or more parties dialog / exchange messages.
A transaction:Well defined start and end.Overall system and resources are in a consistent state at start and end of transaction.All or nothing
We want transactions to be A.C.I.D. Atomic.For a transaction to succeed all of its steps must succeed.All or nothingConsistentA systems resources must be in a consistent state at the start and the end of the transaction.IsolatedEffects of a trans. must be invisible to other open trans.s until it is committed successfully.Durable:Results of committed trans should be permanent.
Concurrency Control (Fowler Ch 16)Optimistic concurrency control:Prevents conflicts between concurrent business transactions by detecting a conflict and rolling back the transaction.Pessimistic concurrency controlPrevents conflicts between concurrent business transactions by allowing only one business transaction at a time to access the data.Eg, file locking on most OSs.
Optimistic Offline Lock
Pessimistic Offline Lock
Execution ContextsRequestSessionTransactionA transaction that spans multiple requests is terms a long transaction.
Some Basic Concepts related to WEA User Goalsperformsissueshas
Can you see an advantage in using the Front Controller pattern?
Client, Web server, Db server
Concurrency: StrategiesAvoid it if you can.Use reference implementations: e.g. DBMS.
Understand the issues and limit:Number of threads.Data sharing (our focus)
Concurrent Data ManipulationWhere can this happen?In memory.In the database.Both of these places are subject to the problems ofInconsistent reads.Lost updates.
Database Management Systems (DBMS)Well developed (tried-and-true) technology.
Hence we will try to push the concurrency down into the DBMS as much as possible.
What does this suggest about your choice of Session State Pattern?
Design solutions regarding in memory data sharing Java: synchronize primitive can sometimes be used:E.g. a Singletons getUniqueInstance() is declared as synchronized. (What could happen if it wasnt?)Careful choice ofDeclaration and use of static fields.Object instantiation.Javas ThreadLocal.
Data Source PatternsFowler: Chapter 10.Data Mapper, TDG
Fowler, Chapter 11UnitOfWorkIdentity MapLazy Loading
Unit of WorkMaintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems
Caches DB access
Minimizes (slow) DB calls
After commit UoW alters DB
Unit of Work
Identity MapEnsures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.
Example:Data Mapper & Identity MapProblem: Each person has many buddys (also Person objects) so may load many copies of the same object
Solution: Identity Map
Identity Map (p.195): Ex. Find Alice
A Problem of O/R MappingsWhen are objects loaded, saved and changes committed?Load A first.Load B first, then ALoad B, then A, change B,
O/R = object / relationalFowler calls this a behavioral problem.ABC*
A Solution: Lazy LoadingComes under various forms.Virtual Proxy
This is particularly useful for large compound objects where only some of the data may be needed (for the request).
Lazy LoadingAn object that does not contain all the data you need buts knows how to get itEg, a customer with many orders, though generally you only want the customer name and address (for the request).Load orders only when required.
Virtual ProxyExtract an interface from Person.Result: IPerson.Create PersonProxyOne one field (the primary key): name.When another field value is requested, it fetches the real person from the DB and then returns the value by delegation.On subsequent calls, it simply delegates.