You are on page 1of 35

Business Component Development Using EJB Technologies

Objectives

In this session, you will learn to:


Understand transaction management
Describe the transaction demarcation task
Implement CMT
Interact programmatically with an ongoing CMT transaction
Implement BMT
Apply transactions to messaging

Ver. 1.0

Slide 1 of 35

Business Component Development Using EJB Technologies


Introducing Transaction Management

A transaction can be defined as an indivisible unit of work


comprised of several operations, all or none of which must
be performed in order to maintain data integrity.
A transactional unit is one in which the transaction
fundamental are satisfied.
The four transaction fundamentals are Atomicity,
Consistency, Isolation, and Durability (ACID).
Transaction management is one of the most crucial
requirements for enterprise application development.

Ver. 1.0

Slide 2 of 35

Business Component Development Using EJB Technologies


Atomicity

Atomicity ensures that the individual actions are bundled


together and appear as one single unit of work.
The atomic property of the transaction states that all the
individual actions that constitute a transaction must succeed
for the transaction to succeed, and on the contrary if any
one of the individual action fails, the transaction as a whole
must fail.

Ver. 1.0

Slide 3 of 35

Business Component Development Using EJB Technologies


Consistency

A transaction that changes or updates the data in the


database must ensure that the data remains in the
consistent state, whether the transaction succeeded or
failed.
The data in the data store must be either in the old state
(the state before the transaction was performed, if the
transaction failed) or in the new state (the state after the
transaction completes successfully).

Ver. 1.0

Slide 4 of 35

Business Component Development Using EJB Technologies


Isolation

The isolation property of the transaction states how the


concurrent transactions that act on the same resource
behave.
The isolation property determines the degree to which
effects of multiple transactions, acting on the same
resource, are isolated from each other.
Isolation allows multiple transactions to read or write to a
database without knowing about other transactions as each
transaction is isolated from the other.
Isolation of read commit ensures that the transaction can
read only the committed data. This level of isolation is
implemented by locking.
Locking allows the data store to handle concurrent
transactions and ensures that the integrity is maintained.
Ver. 1.0

Slide 5 of 35

Business Component Development Using EJB Technologies


Durability

This property of transaction guarantees that updates to the


resource (database records) survive failures.
The resource should be recoverable in any kind of failures,
such as system crashing, network crashing, and power
failure.
Transaction logs are maintained to recover the resources
from the different types of failures.

Ver. 1.0

Slide 6 of 35

Business Component Development Using EJB Technologies


Transaction Demarcation Task

The transaction demarcation policy determines where


transactions begin and end, that is, which groups of
operations form a transaction.
The following figure shows two calls made by a client on the
same method of an enterprise bean.

Ver. 1.0

Slide 7 of 35

Business Component Development Using EJB Technologies


Guidelines for Selecting a Transaction Demarcation Policy

The following guidelines should be used to derive the


transaction demarcation policies:
Minimize the duration of a transaction
Group all methods required for a use case into a single
transaction
Isolate access to shared resources in a separate transaction (If
possible)

Ver. 1.0

Slide 8 of 35

Business Component Development Using EJB Technologies


Implementation Options for Transaction Demarcation

To implement the transactional policy associated with


enterprise bean methods, either use BMT or CMT.
The following figure shows the selection of these options to
implement the transactional policy associated with bean
methods.

Ver. 1.0

Slide 9 of 35

Business Component Development Using EJB Technologies


Overview of CMT

To Implement CMT:
Select the most suitable policy
Specify the selected policy by using:
Transaction attribute metadata annotations
DD Elements

Mark the EJB by:


Using transaction attribute metadata annotations
Including an entry in the DD

Ver. 1.0

Slide 10 of 35

Business Component Development Using EJB Technologies


Overview of BMT

To implement BMT you must:


Code the required transaction policy in the enterprise bean
method
Mark the EJB as using BMT by:
Annotating the EJB
Including an entry in the DD

Ver. 1.0

Slide 11 of 35

Business Component Development Using EJB Technologies


Using CMT

The EJB specification refers to the six CMT transactional


demarcation policies as attributes.
The following figure shows the transaction demarcation
policies implemented by each CMT transaction attribute.

Ver. 1.0

Slide 12 of 35

Business Component Development Using EJB Technologies


CMT Attribute Usage Restrictions

The following table lists the CMT transaction attributes and


enterprise bean types.

Ver. 1.0

Transaction Attribute

Stateless
Session
Beans

Stateful
Session
Beans

Stateful Session Beans


Implementing
SessionSynchronization
interface

Message- Driven
Bean

NOT_SUPPORTED

Yes

Yes

No

Yes

REQUIRED

Yes

Yes

Yes

Yes

REQUIRES_NEW

Yes

Yes

Yes

No

SUPPORTS

Yes

Yes

No

No

MANDATORY

Yes

Yes

Yes

No

NEVER

Yes

Yes

No

No

Slide 13 of 35

Business Component Development Using EJB Technologies


Implementing CMT

The following rules are used for CMT:


1. Select the CMT transaction attribute for each method.
2. Apply the transactional attribute to the method using the
TransactionAttribute annotation as given in the following code:
1 import javax.ejb.TransactionAttribute;
2 import javax.ejb.TransactionAttributeType;
3
4 @Stateless public PayrollBean implements Payroll
{
5 @TransactionAttribute(TransactionAttributeType.
MANDATORY)
6 public void setBenefitsDeduction(int empId,
double deduction) {...}
7
8 @TransactionAttribute(TransactionAttributeType.
REQUIRED)
Ver. 1.0

Slide 14 of 35

Business Component Development Using EJB Technologies


Implementing CMT (Contd.)
9 public double getBenefitsDeduction(int empId)
{...}
10
11 public double getSalary(int empid) {...}
12
13
@TransactionAttribute(TransactionAttributeType.M
ANDATORY)
14 public void setSalary(int empId, double salary)
{...}
15 }

3. Optionally annotate the EJB as using CMT by using the


TransactionManagement (CONTAINER) annotation as shown
in the following code:
1
2
3
4
5
Ver. 1.0

@Stateless
@TransactionManagement(CONTAINER)
public PayrollBean implements Payroll {
//..
}
Slide 15 of 35

Business Component Development Using EJB Technologies


Interacting Programmatically With an Ongoing CMT Transaction

To perform the following tasks, the EJB specification


provides APIs for programmatically interacting with ongoing
CMT transactions:
Get or set the roll back status of a CMT transaction.
Enable a stateful session bean to monitor a CMT transaction.

Ver. 1.0

Slide 16 of 35

Business Component Development Using EJB Technologies


Getting or Setting the Roll Back Status During CMT

The container uses a rollback flag to monitor the state of a


transaction.
The following figure shows an enterprise bean using the
EJBContext methods.
Client

Bean

EJBContent

methodA
getRollbackOnly
methodB

setRollbackOnly

methodC
getRollbackOnly

Ver. 1.0

Slide 17 of 35

Business Component Development Using EJB Technologies


Enabling a Stateful Session Bean Instance to Monitor a CMT Transaction

The Problem: Surviving Transaction Rollback


The Solution: Use the SessionSynchronization Interface

Ver. 1.0

Slide 18 of 35

Business Component Development Using EJB Technologies


Enabling a Stateful Session Bean Instance to Monitor a CMT Transaction (Contd.)

SessionSynchronization interface provides the session bean


with important feedback concerning transaction demarcation
events.
The following figure shows the relationship among the
methods of the SessionSynchronization callbacks and the
transaction demarcation events.
<<interface>>
SessionSynchronization
afterBegin
beforeCompletion
afterCompletion

Container

Stateful
Session Bean

Database

Begin transaction
afterBegin

Debit account A
beforeCompletion

Credit account B

Commit or rollback
afterCompletion

Ver. 1.0

Slide 19 of 35

Business Component Development Using EJB Technologies


Using BMT

Annotate the enterprise bean as using the


TransactionManagement (BEAN) annotation as shown in
the following code:
1
2
3
4
5

@Stateless
@TransactionManagement(BEAN)
public class StockBean implements Stock {
//..
}

Code in the enterprise beans business methods to use the


methods of the javax.transaction.UserTransaction interface
to demarcate the transaction.

Ver. 1.0

Slide 20 of 35

Business Component Development Using EJB Technologies


Using BMT (Contd.)

The following figure shows the essential steps needed to


implement BMT code.
Bean

Database

EJBContent

getUserTransaction
UserTransaction
begin
getStatus

Begin transaction

setTransactionTimeout
setRollbackOnly

commit
or

Commit transaction

rollback

or
Rollback transaction

Ver. 1.0

Slide 21 of 35

Business Component Development Using EJB Technologies


Using BMT (Contd.)

The following code shows an example of BMT


implementation:
1 import java.util.*;
2 import javax.ejb.*;
3 import java.sql.*;
4 import javax.sql.*;
5 import javax.naming.*;
6 import javax.transaction.*;
7
8 @Stateless
9 @TransactionManagement(BEAN)
10 public class StockBean implements Stock {
11
12 @Resource javax.Transaction.UserTransaction ut;
13 @Resource javax.sql.DataSource ds1;
14
Ver. 1.0

Slide 22 of 35

Business Component Development Using EJB Technologies


Using BMT (Contd.)
15

public void updateStock(String symbol, double price)


{

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Ver. 1.0

Connection con = null;


PreparedStatement prepStmt = null;
try {
con = ds1.getConnection();
ut.begin();
prepStmt = con.prepareStatement(
UPDATE Stock set price = ? + where symbol = ?);
prepStmt.setDouble(1, price);
prepStmt.setString(2, symbol);
int rowCount = prepStmt.executeUpdate();
ut.commit();
} catch (Exception ex) {
try {
ut.rollback();
} catch (SystemException syex) {
Slide 23 of 35

Business Component Development Using EJB Technologies


Using BMT (Contd.)
31
throw new EJBException
32
(Rollback failed: + syex.getMessage());
33
}
34 throw new EJBException
35
(Transaction failed: + ex.getMessage());
36
} finally {
37
try {
38
prepStmt.close();
39
con.close();
40
} catch (SQLException e) {
41
throw new EJBException
42
(close failed: + e.getMessage());
43
}
44
}
45
}
46 public StockBean() {}
48 }
Ver. 1.0

Slide 24 of 35

Business Component Development Using EJB Technologies


Rules That Restrict BMT Transaction Demarcation Policies

The transaction demarcation policies applied to a bean


using BMT are governed by the following rules:
The container suspends/resumes any client transaction.
An instance that starts a transaction must complete the
transaction before it starts a new transaction.
Any transaction started by a stateless session bean instance
or message-driven bean instance must commit or roll back
before the method returns.
A stateful session bean method is not required to close a
transaction it is participating in before it returns.

Ver. 1.0

Slide 25 of 35

Business Component Development Using EJB Technologies


BMT Transaction Demarcation Polices for Stateless Session Beans and Message-Driven Beans

A stateless session bean or a message-driven bean using


BMT must implement the following transaction demarcation
policies:
Policy 1 No transaction
Policy 2 Begin and end a BMT transaction

Ver. 1.0

Slide 26 of 35

Business Component Development Using EJB Technologies


BMT Transaction Demarcation Polices for Stateless Session Beans and Message-Driven Beans (Contd.)

The following figure shows the BMT transaction


demarcation policies to a stateless session bean.

Ver. 1.0

Slide 27 of 35

Business Component Development Using EJB Technologies


BMT Transaction Demarcation Polices for Stateful Session Beans

Stateful session beans using BMT must implement the


following transaction demarcation policies:
Policy 1 No transaction
Policy 2 Begin and end a BMT transaction
Policy 3 Begin, but not end, a BMT transaction
Policy 4 Continue an open BMT transaction, but not end it
Policy 5 Continue and end an open BMT transaction

Ver. 1.0

Slide 28 of 35

Business Component Development Using EJB Technologies


BMT Transaction Demarcation Polices for Stateful Session Beans (Contd.)

The following figure shows the application of the BMT


transaction demarcation policies to a stateful session bean.

Ver. 1.0

Slide 29 of 35

Business Component Development Using EJB Technologies


Examining Transactions and JMS API Messages

Many message-oriented systems use asynchronous,


bidirectional communication.
The following figure shows that separate transactions are
required for sending and receiving messages.

Ver. 1.0

Slide 30 of 35

Business Component Development Using EJB Technologies


Applying Transactions to Message-Driven Beans

The following figure shows the transaction settings


applicable to message-driven beans.

Ver. 1.0

Slide 31 of 35

Business Component Development Using EJB Technologies


Applying Transactions to Message-Driven Beans (Contd.)

The following figure shows the endless loop caused by


transaction roll back.

Ver. 1.0

Slide 32 of 35

Business Component Development Using EJB Technologies


Summary

In this session, you learned that:


A transaction can be defined as an indivisible unit of work
comprised of several operations, all or none of which must be
performed in order to maintain preserve data integrity.
A transaction is said to be atomic if it consists of a number of
operations that must succeed or fail as a unit.
A transaction that changes or updates the data in the database
must ensure that the data remains in the consistent state,
whether the transaction succeeded or failed.
Isolation allows multiple transactions to read or write to a
database without knowing about other transactions.

Ver. 1.0

Slide 33 of 35

Business Component Development Using EJB Technologies


Summary (Contd.)

The transaction demarcation policy determines where


transactions begin and end, that is, which groups of operations
form a transaction.
To implement the transactional policy associated with
enterprise bean methods, either use BMT or CMT.
The EJB specification refers to the six CMT transactional
demarcation policies as attributes.
The container uses a rollback flag to monitor the state of a
transaction.
SessionSynchronization interface provides the session bean
with important feedback concerning transaction demarcation
events.
Code in the enterprise beans business methods to use the
methods of the javax.transaction.UserTransaction interface to
demarcate the transaction.

Ver. 1.0

Slide 34 of 35

Business Component Development Using EJB Technologies


Summary (Contd.)

Many message-oriented systems use asynchronous,


bidirectional communication.
Message-driven beans using CMT must use the REQUIRED or
NOT_SUPPORTED transaction attributes.

Ver. 1.0

Slide 35 of 35

You might also like