Ejb Transactions: Session 5

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ejb Transactions: Session 5 as PDF for free.

More details

  • Words: 1,532
  • Pages: 30
EJB Transactions Session 5

Transactions

Grey Matter, Sella Synergy India

Think Spot

Is your transaction safe?

How will you handle this situation? Let’s say you have two different transactions running, hitting the same database. You don’t want the effect of one transaction to corrupt the state of another transaction.

Transactions

Grey Matter, Sella Synergy India

Road Map



What is Transaction ?



ACID Properties of Transaction



EJB Transactions



Container Managed Transaction Setup



Forcing Rollback



Transactions

How Transaction React to System/Application Exceptions

Grey Matter, Sella Synergy India

2min

What is Transaction?







Transaction

A transaction is a mechanism to handle groups of operations as though they were one In a transaction, either all operations occur or none at all. Operations involved in a transaction may rely on many different servers and databases.

Grey Matter, Sella Synergy India

2min

ACID Properties of Transaction

A transaction is formally defined by the set of properties known by the acronym ACID



The acronym of ACID stands for:



Transaction



Atomic



Consistent



Isloated



Durable

Grey Matter, Sella Synergy India

2min

EJB Transactions



A transactions can be determined: By a client around one or more method invocations



By the container around each method invocations



By the bean in a single method



Clien t tx1.begin(); EJB1.method(); EJB2.method(); tx1.commit();

Transactions

This is an example of a client demarcated transaction. Notice that the transaction can span one or more EJBs, one or more methods, and one or more databases. Grey Matter, Sella Synergy India

2min

The Tag



The tag: Specifies whether a bean uses container or bean managed transactions −

Used only with <session> or <message-driven>



(entity EJBs always use container managed transactions) Can be either Bean or Container



Examples: <enterprise-beans> <sessions> <ejb-name>JspSession ... Container Transactions

Grey Matter, Sella Synergy India

2min

Container-Managed Transactions 

CMT Are started and stopped by the container before and after a method invocations 

Is configured in the deployment descriptor



Is mandatory for entity EJBs.



This is a scenario where the container invoke() starts a new transaction 1 From for a single Client method call.

Transactions



Pre - invocation 2 Start TX



Check Security



DB Load



Etc. invoke()

Container

Server

3 Component

Post - invocation4 ●

DB Store



Commit / Rollback TX



Etc.

Grey Matter, Sella Synergy India

2min

The Tag



The tag declares how container-managed transactions should work for one or more methods. Example: ...

<method> <ejb-name>JspSession <method-name>* Required

. . . Transactions

Grey Matter, Sella Synergy India

2min

The Six Values of



The tag can have six values Never



Mandatory



NotSupported



Supports



Required



RequiresNew



Transactions

This is the default, when transaction attributes are not specified

Grey Matter, Sella Synergy India

2min

Never



Never specifies that the EJB must never be invoked within the context of a transactions

Scenario 1 (Client TX): Client

1 – begin 2 – method

Contai ner

Active Transaction No Active Transaction

EJ B

Remote Exception Scenario 2 (No Client TX): Client 1 – method

Transactions

Contai ner

Grey Matter, Sella Synergy India

1 – method

EJ B

2min

Mandatory



Mandatory transactions require the client to pass a global transaction to the EJB

Active Transaction No Active Transaction method

Scenario 1 (Client TX): Client

1 – begin 2 – method 3– Commit

Scenario 2 (No Client TX): Client method

Transactions

Contai ner

Contai ner

Grey Matter, Sella Synergy India

Transaction Required Exception

EJ B

EJ B

2min

NotSupported



NotSupported cause client transaction to be suspended during the EJB's execution.

Scenario 1 (Client TX): Client

1 – begin 2 – method 3– Commit

Scenario 2 (No Client TX): Client method

Transactions

Contai ner

Contai ner

Grey Matter, Sella Synergy India

Active Transaction No Active Transaction method

method

EJ B

EJ B

2min

Supports



Supports implies that the EJB will be included in any clientstarted transactions.

Scenario 1 (Client TX): Client

1 – begin 2 – method 3– Commit

Scenario 2 (No Client TX): Client method

Transactions

Contai ner

Contai ner

Grey Matter, Sella Synergy India

Active Transaction No Active Transaction method

method

EJ B

EJ B

2min

Required



Required transactions force the EJB to be executed within a transaction started either by the client or the container

Scenario 1 (Client TX): Client

1 – begin 2 – method 3– Commit

Scenario 2 (No Client TX): Client method

Transactions

Contai ner

Contai ner

Grey Matter, Sella Synergy India

Active Transaction No Active Transaction method

1 – begin 2 – method 3– Commit

EJ B

EJ B

2min

RequiresNew



RequiresNew transactions force the EJB to execute within a new transaction that can only be started the container

Scenario 1 (Client TX): Client

1 – begin 2 – method 3– Commit

Scenario 2 (No Client TX): Client method

Transactions

Contai ner

Contai ner

Grey Matter, Sella Synergy India

Active Transaction No Active Transaction New EJ Transaction B method

1 – begin 2 – method 3– Commit

EJ B

2min

A CMT Example ejb-jar.xml Snippet: <enterprise-beans> <session> <ejb-name> AnEJB ... Containers ... <method> <ejb-name>AnEJB <method-name>* Required
TRansactioins

Grey Matter, Sella Synergy India

2min

Puzzle Corner

Which transaction attributes can cause an in-progress transaction to be suspended?

NotSupported and RequiresNew

Transactions

Grey Matter, Sella Synergy India

2min

Transaction isolation level



The transaction isolation level controls the behavior of a transaction, by balancing: The integrity of the data vs



The concurrent access speed



Transactions

Grey Matter, Sella Synergy India

2min

Integrity Problem Scenarios







A Dirty Read happens when reading a row that was modified by someone else (and that person did not commit yet).

A Non-repeatable Read happens when reading a new committed value while in the middle of a transaction (i.e. The data change during processing)

A Phantom Row happens when reading a row that was deleted by someone else ( and that person did not commit yet)

Transactions

Grey Matter, Sella Synergy India

2min

Per-Method Isolation Level •



For CMT, transaction isolation levels can be set at the method level. Four levels are available: TRANSACTION_READ_UNCOMMITTED, TRANSACTION_READ_COMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE

To Set Specific Isolation Level to a Method in weblogic-ejb-jar.xml: TRANSACTION_READ_UNCOMMITTED <method> <ejb-name>dogEJB <method-name>*

Transactions

Grey Matter, Sella Synergy India

2min

TRANSACTION_READ_UNCOMMITTED



TRANSACTION_READ_UNCOMMITTED Enables reading in modified data even if that data was not committed



yet Is the most permissive transaction isloation level



Is the fastest transaction isolation level



Allows dirty reads, phantom rows and non-repeatable reads.



Transactions

Grey Matter, Sella Synergy India

2min

TRANSACTION_READ_COMMITTED



TRANSACTION_READ_COMMITTED Enables reading in modified data if it has been committed



Does NOT allow dirty reads



Allows phantom rows and non-repeatable reads



Transactions

Grey Matter, Sella Synergy India

2min

TRANSACTION_REPEATABLE_READ



TRANSACTION_REPEATABLE_READ Enables reading in modified data if it has been committed, but with the



guarantee that the data will not change again Does NOT allow dirty reads and non-repeatable reads



Allows phantom rows



Transactions

Grey Matter, Sella Synergy India

2min

TRANSACTION_SERIALIZABLE



TRANSACTION_SERIALIZABLE Locks data so that only 1 transaction at a time can access it: transactions



are done in 'series' Is the most restrictive transaction isolation level, resulting in the highest



integrity Is the slowest transaction isolation level



Does NOT allow dirty reads, phantom rows and non-repeatable reads.



Transactions

Grey Matter, Sella Synergy India

2min

Best Practices – Design Considerations



If possible, always use a CMT



Think about transaction granularity when designing EJBs: Transaction configuration can have a big impact on



performance. Too many fine-grain transactions generate excessive traffic



to transaction and resource managers. Too many coarse – grain transactions may block other



clients accessing the same resources.

Transactions

Grey Matter, Sella Synergy India

2min

Best Practices – Design Considerations

Try to limit a web request to a single transaction



Do not have transactions span user input



Try creating a facade session bean that wraps multiple EJB



method invocations to avoid creating multiple transactions Use Required on methods that update, create, delete or



modify databases Use Supports or Not Supported on methods that perform get



or read-only operations.

Transactions

Grey Matter, Sella Synergy India

2min

Recap



Mark a method with one of six transaction attributes : Required



RequiresNew



Mandatory



Supports



Notsupported



Never



Transactions

Grey Matter, Sella Synergy India

2min

Recap



When the method is called, the container uses the attributes to do one of

the five things: ●

Run the method in the caller’s transaction. OR



Suspends the caller’s transaction and Start a new transaction. OR



Suspend the caller’s transaction and run the method without a transaction. OR



Throw an exception because the caller does not have a transaction.

Transactions

Grey Matter, Sella Synergy India

2min

Q&A

Handling JavaException Collections Framework

Grey Matter, Sella Synergy India

2min

Related Documents

Ejb Transactions: Session 5
November 2019 20
Ejb
November 2019 25
Ejb
May 2020 13
Ejb
May 2020 11
Ejb
November 2019 22
Ejb
November 2019 25