BCD Session 16

Embed Size (px)

Citation preview

  • 8/10/2019 BCD Session 16

    1/35

    Slide 1 of 35Ver. 1.0

    Business Component Development Using EJB Technologies

    In this session, you will learn to:

    Understand transaction management

    Describe the transaction demarcation task

    Implement CMT

    Interact programmatically with an ongoing CMT transactionImplement BMT

    Apply transactions to messaging

    Objectives

  • 8/10/2019 BCD Session 16

    2/35

    Slide 2 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    3/35

    Slide 3 of 35Ver. 1.0

    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 anyone of the individual action fails, the transaction as a whole

    must fail.

  • 8/10/2019 BCD Session 16

    4/35

    Slide 4 of 35Ver. 1.0

    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).

  • 8/10/2019 BCD Session 16

    5/35

    Slide 5 of 35Ver. 1.0

    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 sameresource, 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 canread 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.

  • 8/10/2019 BCD Session 16

    6/35

    Slide 6 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    7/35Slide 7 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    8/35Slide 8 of 35Ver. 1.0

    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)

  • 8/10/2019 BCD Session 16

    9/35Slide 9 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    10/35Slide 10 of 35Ver. 1.0

    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

  • 8/10/2019 BCD Session 16

    11/35Slide 11 of 35Ver. 1.0

    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

  • 8/10/2019 BCD Session 16

    12/35Slide 12 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    13/35Slide 13 of 35Ver. 1.0

    Business Component Development Using EJB Technologies

    CMT Attribute Usage Restrictions

    The following table lists the CMT transaction attributes and

    enterprise bean types.

    Transaction Attr ibu te Stateless

    Session

    Beans

    Stateful

    Session

    Beans

    Stateful Session B eans

    Implement ing

    SessionSynchronizat ioninterface

    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

  • 8/10/2019 BCD Session 16

    14/35Slide 14 of 35Ver. 1.0

    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)

  • 8/10/2019 BCD Session 16

    15/35Slide 15 of 35Ver. 1.0

    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.MANDATORY)

    14 public void setSalary(int empId, double salary){...}

    15 }

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

    TransactionManagement (CONTAINER) annotation as shownin the following code:1 @Stateless

    2 @TransactionManagement(CONTAINER)

    3 public PayrollBean implements Payroll {

    4 //..

    5 }

  • 8/10/2019 BCD Session 16

    16/35Slide 16 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    17/35Slide 17 of 35Ver. 1.0

    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

    methodB

    methodC

    getRollbackOnly

    setRollbackOnly

    getRollbackOnly

  • 8/10/2019 BCD Session 16

    18/35Slide 18 of 35Ver. 1.0

    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

  • 8/10/2019 BCD Session 16

    19/35Slide 19 of 35Ver. 1.0

    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.

    Container Stateful

    Session BeanDatabase

    afterBegin

    beforeCompletion

    afterCompletion

    Begin transaction

    Credit account B

    Debit account A

    Commit or rollback

    SessionSynchronization

    afterBegin

    beforeCompletion

    afterCompletion

  • 8/10/2019 BCD Session 16

    20/35Slide 20 of 35Ver. 1.0

    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 @Stateless

    2 @TransactionManagement(BEAN)

    3 public class StockBean implements Stock {

    4 //..

    5 }

    Code in the enterprise beans business methods to use the

    methods of the javax.transaction.UserTransaction interface

    to demarcate the transaction.

  • 8/10/2019 BCD Session 16

    21/35Slide 21 of 35Ver. 1.0

    Business Component Development Using EJB Technologies

    Using BMT (Contd.)

    The following figure shows the essential steps needed to

    implement BMT code.

    Bean EJBContent Database

    begin

    getStatus

    setRollbackOnly

    getUserTransaction

    setTransactionTimeout

    UserTransaction

    commit

    or

    rollback

    Begin transaction

    Commit transaction

    or

    Rollback transaction

  • 8/10/2019 BCD Session 16

    22/35Slide 22 of 35Ver. 1.0

    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

  • 8/10/2019 BCD Session 16

    23/35

    Slide 23 of 35Ver. 1.0

    Business Component Development Using EJB Technologies

    Using BMT (Contd.)

    15 public void updateStock(String symbol, double price)

    {

    16 Connection con = null;

    17 PreparedStatement prepStmt = null;

    18 try {

    19 con = ds1.getConnection();20 ut.begin();

    21 prepStmt = con.prepareStatement(

    22 UPDATE Stock set price = ? + where symbol = ?);

    23 prepStmt.setDouble(1, price);

    24 prepStmt.setString(2, symbol);

    25 int rowCount = prepStmt.executeUpdate();

    26 ut.commit();

    27 } catch (Exception ex) {

    28 try {

    29 ut.rollback();

    30 } catch (SystemException syex) {

  • 8/10/2019 BCD Session 16

    24/35

    Slide 24 of 35Ver. 1.0

    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 }

  • 8/10/2019 BCD Session 16

    25/35

    Slide 25 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    26/35

    Slide 26 of 35Ver. 1.0

    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 1No transaction

    Policy 2Begin and end a BMT transaction

  • 8/10/2019 BCD Session 16

    27/35

    Slide 27 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    28/35

    Slide 28 of 35Ver. 1.0

    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 1No transaction

    Policy 2Begin and end a BMT transaction

    Policy 3Begin, but not end, a BMT transaction

    Policy 4Continue an open BMT transaction, but not end it

    Policy 5Continue and end an open BMT transaction

  • 8/10/2019 BCD Session 16

    29/35

    Slide 29 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    30/35

    Slide 30 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    31/35

    Slide 31 of 35Ver. 1.0

    Business Component Development Using EJB Technologies

    Applying Transactions to Message-Driven Beans

    The following figure shows the transaction settings

    applicable to message-driven beans.

  • 8/10/2019 BCD Session 16

    32/35

    Slide 32 of 35Ver. 1.0

    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.

  • 8/10/2019 BCD Session 16

    33/35

    Slide 33 of 35Ver. 1.0

    Business Component Development Using EJB Technologies

    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.

    Summary

    i l i h l i

  • 8/10/2019 BCD Session 16

    34/35

    Slide 34 of 35Ver. 1.0

    Business Component Development Using EJB Technologies

    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 transactionaldemarcation 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.

    Summary (Contd.)

    B i C D l U i EJB T h l i

  • 8/10/2019 BCD Session 16

    35/35

    Business Component Development Using EJB Technologies

    Many message-oriented systems use asynchronous,

    bidirectional communication.

    Message-driven beans using CMT must use the REQUIRED or

    NOT_SUPPORTED transaction attributes.

    Summary (Contd.)