Session 9 Tp9
-
Upload
phanleson -
Category
Technology
-
view
1.076 -
download
5
description
Transcript of Session 9 Tp9
ACCP2005 / EJB 2.0/Session 9/ 1 of 39
Transaction Management
Session 9
ACCP2005 / EJB 2.0 / Session 9/ 2 of 39
Describe the benefits of transactions Define the ACID properties Differentiate between transaction models Define Transaction Isolation Describe the Distributed Transactions List the Programmatic Transactions in EJB Describe Transaction attributes
Session Objectives
ACCP2005 / EJB 2.0 / Session 9/ 3 of 39
Review of Session 8 (1)
The information determined by a bean regarding its status during runtime contains information about:
The home object of the bean. The transaction that involves the bean. For instance, the bean need not go
through a step if the transaction being performed is going to fail. Security to authorize the client. The bean is capable of performing this by
querying its environment. It can thus ensure that the client has security access levels to perform an operation.
The various environment properties that were responsible for the deployment of the bean.
The main objective of the context is to encapsulate the bean’s domain in a compact object.
The session bean context is called the session context and the entity bean context is known as the entity context.
When a bean has to make a call to another bean, the getEJBObject() method is used. Further, this method is also used when a reference has to be passed to a bean.
Authentication ensures that the identity of the client is true. The username and password are checked against a database, which contains a permanent client profile.
ACCP2005 / EJB 2.0 / Session 9/ 4 of 39
Review of Session 8 (2)
The process of granting permissions to the correct user is called authorization.
When declarative authorization is performed, the container performs the validations. In such a case, the focus is entirely on business logic.
In programmatic authorization, security checks are coded into the bean. The bean will contain business logic along with security checks.
Security roles can be defined as the collection of clients. The isCallerRole() method ensures that the current caller falls into the
security role. The getCallerPrincipal() method gets back the current caller’s security identity.
Security contexts sum up the security state of the caller and perform their functions behind the scenes.
EJB does not have specific rules for the way containers deal with security.
The EJB object handle can be defined as a long-lived proxy for an EJB object. In case the client disconnects himself from the EJB server or container, the EJB object handle can be used to reconnect to get back the conversational state with the particular bean.
In the new EJB 2.0 specifications, the JAAS architecture makes security more portable and robust.
ACCP2005 / EJB 2.0 / Session 9/ 5 of 39
Common Problems
Atomic Operations
Network or Machine Failure
Multiple users sharing
data
Transaction Problems
ACCP2005 / EJB 2.0 / Session 9/ 6 of 39
Atomic Operations
Operation 1
Operation 3
Operation Total
Operation 2+ +
Operations that perform multiple, discrete tasks and yet have to execute as one contiguous, atomic operation.
Operation1, Operation2 and Operation3 should succeed individually for Operation Total to succeed.
If any one process fails, then the entire process would fail.
ACCP2005 / EJB 2.0 / Session 9/ 7 of 39
Network or Machine Failure
Across a multi-deployment, if a network crash occurs during a critical operation, it could lead to major implications such as loss of data.
Network Crash
Loss of data
ACCP2005 / EJB 2.0 / Session 9/ 8 of 39
Multiple Users Sharing Data
Database
If multiple users modify the same data simultaneously, then data could be corrupted. The database could also contain data partially supplied by one tool and partially supplied by another tool.
ACCP2005 / EJB 2.0 / Session 9/ 9 of 39
Transaction Definition
Transaction: Series of operations that are executed as one large atomic operation. (all or none)
Series of OperationsCommitted Transaction
All or None
ACCP2005 / EJB 2.0 / Session 9/ 10 of 39
Terminologies in Transactions
Transactional Object- An application component which is involved in transactions. It could be an enterprise bean, server component or a CORBA component.
Transaction Manager- Operates behind the scenes,performing all tasks.
Resource- A storage from which one can read and write to.
Resource manager- Manages the resources.
ACCP2005 / EJB 2.0 / Session 9/ 11 of 39
The ACID properties
Operations in transactions
Atomicity Consistency Isolation Durability
ACCP2005 / EJB 2.0 / Session 9/ 12 of 39
Atomicity
Operation 3
Operation 2
Operation 1
Fails Fails
Fails
Entire Transaction Fails
ACCP2005 / EJB 2.0 / Session 9/ 13 of 39
Consistency
Transaction Consistency ensures that the state is consistent
Consistency ensures that a transaction leaves the system’s state as consistent.
ACCP2005 / EJB 2.0 / Session 9/ 14 of 39
Isolation
Isolation isolates one transaction from another. This means that multiple transactions can be read and written to a database without one transaction knowing about the other.
ACCP2005 / EJB 2.0 / Session 9/ 15 of 39
Durability
Durability ensures that updates to managed resources survive failures. These can be machines crashing, network crashing or power failures.
Durability
Network Failure Survived Failure
ACCP2005 / EJB 2.0 / Session 9/ 16 of 39
Transactional Models
Transactions can be performed through various methods. There is a separate complexity and feature involved with
each transactional model.
Transactional models
NestedTransactions
Flat Transactions
ACCP2005 / EJB 2.0 / Session 9/ 17 of 39
Flat Transactions
Operation 1
Operation 4
Operation 3
Operation 2 Executed as one unit of work
ACCP2005 / EJB 2.0 / Session 9/ 18 of 39
Nested Transactions
Transaction1
Transaction2
Allows units of work to be embedded in other units of work.
When one particular unit of work nested within another unit of work performs a rollback, the entire transaction does not roll back.
However, only if the embedded unit gets executed, does the embedding unit get executed.
Transaction3
ACCP2005 / EJB 2.0 / Session 9/ 19 of 39
Programmatic and Declarative Transactions
A transaction ends with either a commit or an abort
The important questions to note are, who begins a transaction and who issues a commit or abort, and when do these steps occur.
This process is called demarcating transactional boundaries.
The two ways to demarcate transactional boundaries are:
Programmatically Declaratively
ACCP2005 / EJB 2.0 / Session 9/ 20 of 39
Transaction Isolation Levels
TRANSACTION_READ_UNCOMMITED mode
TRANSACTION_READ_COMMITTED mode
TRANSACTION_SERIALIZABLE mode
TRANSACTION_REPEATABLE_READ mode
Transaction Levels
ACCP2005 / EJB 2.0 / Session 9/ 21 of 39
Isolation Problems in EJB-I
The Dirty Read problem: When data from an uncommitted database is read it is called the Dirty Read problem
The Unrepeatable Read Problem: When data is read the second time and upon reading it changes are found, it is called the unrepeatable read problem
The Phantom Problem: When new set of data appears in the database between two read operations, it is called the phantom problem
ACCP2005 / EJB 2.0 / Session 9/ 22 of 39
Isolation Problems in EJB-II
The Dirty Read Problem
The Phantom Problem
The Unrepeatable Read Problem
Transactional Isolation Problems
TRANSACTION_READ_COMMITTED
TRANSACTION_REPEATABLE_READ
TRANSACTION_SERIALIZABLE
ACCP2005 / EJB 2.0 / Session 9/ 23 of 39
Distributed Transactions
The Transactional Cover
Application server1
Application server2
Distributed flat transactions allow multiple application servers to collaborate under 1 transactional cover.
ACCP2005 / EJB 2.0 / Session 9/ 24 of 39
Durability and Two phase Commit protocol
PHASE I
Before commit Message
RESOURCES
Transactions
Last chance to perform abort statement
Abort
Yes
NoTransaction Continues
Transaction Aborted
Phase IIActual data updates are performed by resource managers
ACCP2005 / EJB 2.0 / Session 9/ 25 of 39
Steps in the Two-Phase Commit Statement
Transaction coordinator
1. Prepare to commit statement
Resource Manager
2. Message is passed to resource managers asking if they are ready to commit
3. Everyone agrees to commit
Once the three steps given in the diagram are completed, the transaction coordinator asks the transaction managers to commit. This is passed on to the resource manager, which makes all resource updates permanent.
TransactionManager
TransactionManager
ACCP2005 / EJB 2.0 / Session 9/ 26 of 39
Transactional Communications Protocol and Transaction
Contexts
The transactional context:- The most important piece of
information sent during the transactional communication.
Object that holds information about the system’s current transactional state, and can also be passed around between parties involved in transactions.
ACCP2005 / EJB 2.0 / Session 9/ 27 of 39
CORBA’s Object Transaction Service
( OTS )
CORBA’s OTS
CosTransactions interface
CosTSPortability interface
ACCP2005 / EJB 2.0 / Session 9/ 28 of 39
CORBA’s Object Transaction Service ( OTS )
System-level vendors need to concentrate on the inner workings of the OTS.
Part of the OTS helps the developer to separate transaction boundaries programmatically.
OTS has been segregated into two sub APIs: Java Transaction Service (JTS) Java Transaction API (JTA)
ACCP2005 / EJB 2.0 / Session 9/ 29 of 39
The Java Transaction Service (JTS)
Java Transactional
Service
Defines interfaces which are used by the transaction and resource managers
Many objects arepassed around and used by the transaction and resource managers
ACCP2005 / EJB 2.0 / Session 9/ 30 of 39
Java Transaction API (JTA)
JTA
Starts a transaction inside a bean
Call other beans which are involved in a transaction
Controls the commit and abort processes
ACCP2005 / EJB 2.0 / Session 9/ 31 of 39
Controlling Transactions from Client Code
The Java Transaction API can be used in the client code to make a call to the beans.
JTAs can be used in case of a workflow bean, which calls several smaller beans.
The JTA UserTransaction interface can be looked up with the Java Naming and Directory Interface (JNDI).
ACCP2005 / EJB 2.0 / Session 9/ 32 of 39
Designing Transactional Conversations in EJB
Uncommitted database
Aborted Transaction
Rollback of the updates is performed
Exception is thrown
The client
ACCP2005 / EJB 2.0 / Session 9/ 33 of 39
Session Synchronization methods
afterBegin(): Called after a transaction begins
beforeCompletion(): Called immediately before a transaction completes
afterCompletion(): Called directly after a transaction gets completed
ACCP2005 / EJB 2.0 / Session 9/ 34 of 39
Transaction Attributes
Specifies how the EJB Container handles transactions for that method on client invocation through the enterprise bean’s home or component interface or when a JMS message arrives.
Enterprise beans have the following values for the transaction attributes:
Required Supports RequiresNew Mandatory Never NotSupported
ACCP2005 / EJB 2.0 / Session 9/ 35 of 39
Container-Managed Transaction Demarcation for Session and Entity
Beans
NotSupported: The Container invokes the bean method whose transaction attribute is set to NotSupported with an unspecified transaction context.
Required: The Container must invoke a method having attribute as Required with a valid transaction context.
Supports: The invocation of a bean method with a transaction attribute of Supports involves two cases:
If the client calls with a transaction context, the Container performs the same steps as described in the Required case.
If the client calls without a transaction context, the Container performs the same steps as described in the NotSupported case.
RequiresNew: As the name suggests, the container must invoke a bean method with the attribute RequiresNew with a new transaction context.
Mandatory: The container has to invoke, mandatorily, a bean method with a Mandatory transaction attribute in the client’s transaction context.
Never: The Container invokes a bean method whose transaction attribute is set to Never without a transaction context.
ACCP2005 / EJB 2.0 / Session 9/ 36 of 39
Container-Managed Transaction for Message-Driven Beans
The Container is responsible for transaction demarcation for the message-driven beans that the bean developer declared with transaction managed transaction demarcation.
Only the NotSupported and Required attributes are useful as there can be no pre-existing transaction context and no client to handle exceptions.
NotSupported: The context generates an unspecified transaction context while invoking a message bean’s method. If the onMessage() method invokes other enterprise beans, the container passes no transaction context with the invocation.
Required: The container invokes a message driven bean method with transaction attribute set to Required with a valid transaction context.
ACCP2005 / EJB 2.0 / Session 9/ 37 of 39
Summary (1)
Transactions address three main problems: Atomic operations Network or machine failure Multiple users sharing data
Atomic operations perform multiple discrete operations, which are collectively executed as one contiguous operation.
Transaction: A series of operations that are executed as one large, atomic operation. They work on the principle of ‘all or none’.
Multiple users can share the same data when transactions are used. They also make sure that the updated data is completely and wholly written, with no confusion about updates from other clients.
ACID stands for: Atomicity Consistency Isolation Durability
ACCP2005 / EJB 2.0 / Session 9/ 38 of 39
Summary (2)
Grouping of many operations into one unit of work is known as atomicity.
Consistency makes sure that a transaction leaves the system’s state as consistent, once the transaction is completed.
The advantage of isolation is that every client feels that he/she is the only client modifying the database.
The two most important transactional models are: Flat transactions Nested transactions
Flat transaction is the easiest transactional model to understand. It is a series of operations performed as a single unit of work.
A nested transaction allows units of work to be embedded in other units of work.
ACCP2005 / EJB 2.0 / Session 9/ 39 of 39
Summary (3)
There are two ways to demarcate transactional boundaries: Programmatically Declaratively
EJB has four transaction isolation levels: The TRANSACTION_READ_UNCOMMITTED mode The TRANSACTION_READ_COMMITTED mode The TRANSACTION_REPEATABLE_READ mode The TRANSACTION_SERIALIZABLE mode
The SessionSynchronization, the afterBegin (), and the beforeCompletion() methods are useful when the stateful session bean caches database data in memory while in a transaction.