13 EJB Transactions

Enterprise JavaBeans Declarative Transactions Transactions - notes  Transaction types:  flat,  nested,  chained tr...

0 downloads 180 Views 2MB Size
Enterprise JavaBeans Declarative Transactions

Transactions - notes  Transaction types:  flat,  nested,  chained transactions,  sagas  The Enterprise JavaBeans architecture (including EJB

3.1) supports flat transactions only  A flat transaction cannot have any child (nested)

transactions Declarative transactions

2

Transactions and Transaction Managers  There are many transactional resources  Databases  Messaging middleware  As programmers, we want “abstract” transactions:  we do not want to deal with some transaction API specific to only Oracle or DB2  we want to be able to have in one transaction:  

several operations with several databases several operations with several messaging providers

Declarative transactions

3

Transaction Managers  Transaction Managers (TM) provide us such “abstract”

transactions

 TM hides any complexities and API differences of particular

transactional resources  TM provides for programmer unified transaction API

 In Java EE these (unified) APIs are:  Java Transaction API (JTA) 

interfaces between a transaction manager and the other parties involved in a distributed transaction processing system: the application programs, the resource managers, and the application server

 Java Transaction Service (JTS)  provides transaction interoperability using the standard IIOP protocol for transaction propagation between servers Declarative transactions

4

How to use Java Transaction API  Client managed transactions  client calls JTA API  Component managed (bean managed) transactions  client is unaware of transactions, component programmer calls JTA API  Container managed transaction  neither client nor component programmer calls JTA API, container does it automatically  Also known as declarative transactions Declarative transactions

5

Client-managed transactions

Declarative transactions

6

Bean-managed transactions

Declarative transactions

7

Container-managed transactions

Declarative transactions

8

Container-managed transactions: Transaction attributes

Declarative transactions

9

Container-managed transactions: Transaction attribute Required  If method is not annotated then default transactional attribute

Required is applied

 If a client invokes the component’s method while the client is

associated with a transaction context, the container invokes the component’s method in the client’s transaction context.  If the client invokes the component’s method while the client is not associated with a transaction context, the container automatically starts a new transaction before delegating a method call to the component’s method.

 If the business method invokes other components, the container

passes the transaction context with the invocation.  The container attempts to commit the transaction when the business method has completed.

 The container performs the commit protocol before the method

result is sent to the client.

Declarative transactions

10

Container-managed transaction example R – Required RN – RequiresNew

R Client

Comp1

–– Transaction Transaction context context

R

RN

Comp2

R Comp3

Comp5

RN

R

Comp4

Comp6

Declarative transactions

11

@Stateless @TransactionManagement(TransactionManagementType.CONTAINER) public class TellerBean implements Teller { @PersistenceContext private EntityManager em; @Resource private SessionContext ctx;

@TransactionAttribute(TransactionAttributeType.REQUIRED) public void transferFunds(long amount, String fromAccount, String toAccount) { // Load entities from DB: BankAccount ba1=em.find(BankAccount.class, fromAccount); BankAccount ba2=em.find(BankAccount.class, toAccount); if (ba1.balance < amount) { ctx.setRollbackOnly(); return; } ba1.withdraw(amount); ba2.deposit(amount); } Declarative transactions

12

Transactions and asynchronous communication  The client transaction context does not get propagated

into the asynchronous method execution  Therefore one can conclude that whenever an asynchronous method m gets invoked:  If m is defined with REQUIRED as transaction attribute

then it will always work as REQUIRES_NEW  If m is defined with MANDATORY as transaction attribute then it will always throw a TransactionRequiredException  If m is defined with SUPPORTS as transaction attribute then it will never run with a transaction context Declarative transactions

13

Distributed transactions - example  In the following figure, a client invokes the enterprise bean X.  Bean X updates data using two database connections that the Deployer configured to connect with two different databases, A and B.  Then X calls another enterprise bean, Y.  Bean Y updates data in database C.

 The EJB server ensures that the updates to databases A, B, and C are either all committed or

all rolled back. Declarative transactions

14

Distributed transactions - example

Declarative transactions

15

Distributed transactions - example  The application programmer does not have to do

anything to ensure transactional semantics.  Behind the scenes, the EJB server enlists the database connections as part of the transaction.  When the transaction commits, the EJB server and the database systems perform a two-phase commit protocol to ensure atomic updates across all three databases.

Declarative transactions

16

Distributed transactions – example  The Enterprise JavaBeans architecture makes it

possible to perform the updates to databases A and B in a single transaction

Declarative transactions

17

Client-managed transactions

Declarative transactions

18

Client-managed transactions  The client programmer demarcates the transaction with begin and commit calls.

 If the enterprise beans X and Y are configured to use an existing transaction (i.e., their methods have transaction attributes that either require or support an existing transaction context), the EJB server ensures that the updates to databases A and B are made as part of the client’s transaction.

Declarative transactions

19

What about .Net?  Also has declarative transactions:  .NET Windows Communication Foundation (WCF) Transactions 

introduced in .NET 3.0 and built on top of System.Transactions available in .NET 2.0.

 Provides a declarative approach to transaction

management that is implemented using various attributes and properties, such as:   

TransactionScopeRequired, TransactionAutoComplete, and TransactionFlow Declarative transactions

20

Declarative vs manual Transactions  Using declarative transactions allows to REUSE the

component in different use case implementations, where it could be:  Either the ROOT of component tree – thus controlling

the transaction begin and commit;  Or some Dependent component – thus NOT deciding about beginning or ending of the transaction.

Declarative transactions

21