You are on page 1of 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

How Transaction React to System/Application
Exceptions

Transactions Grey Matter, Sella Synergy India 2min


What is 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.

Transaction 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:

Atomic

Consistent

Isloated

Durable

Transaction 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


This is an example of a
Clien client
t demarcated
transaction. Notice that
tx1.begin(); the transaction can
span one or more
EJBs, one or more
EJB1.method();
methods, and one
or more databases.
EJB2.method();
tx1.commit();
Transactions Grey Matter, Sella Synergy India 2min
The <transaction-type> Tag


The <transaction-type> 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</ejb-name>
...
<transaction-type>Container</transaction-type>
</session>
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.

Pre - invocation Server


This is a ●
Start TX 2
scenario ●
Check Security
where the ●
DB Load

container ●
Etc. 3
starts a new
invoke()
invoke()
Component
transaction
1 From
for a single
Client Post - invocation4
method call. Container ●
DB Store

Commit / Rollback TX

Etc.

Transactions Grey Matter, Sella Synergy India 2min


The <container-transaction> Tag


The <Container-transaction> tag declares how
container-managed transactions should work for one
or more methods.

<container-transaction> Example:
...
</ enterprise-beans>
<assembly-descriptors>
<container-transactions>
<method>
<ejb-name>JspSession</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
. . . </assembly-descriptor>

Transactions Grey Matter, Sella Synergy India 2min


The Six Values of <trans-attributes>


The <trans-attribute>tag can have six values
Never

Mandatory

NotSupported
− This is the
Supports
− default,
Required

when
transaction
RequiresNew

attributes are
not specified

Transactions Grey Matter, Sella Synergy India 2min


Never


Never specifies that the EJB must never be invoked within the
context of a transactions
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai B
2 – method ner
Remote Exception

Scenario 2 (No Client


TX): Contai EJ
Client 1 – method ner 1 – method
B

Transactions Grey Matter, Sella Synergy India 2min


Mandatory


Mandatory transactions require the client to pass a global
transaction to the EJB
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit

Scenario 2 (No Client


TX): Contai EJ
Client method B
ner

Transaction
Transactions Grey Matter, Sella Synergy India
Required Exception 2min
NotSupported


NotSupported cause client transaction to be suspended during
the EJB's execution.
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit

Scenario 2 (No Client


TX): Contai EJ
Client method method B
ner

Transactions Grey Matter, Sella Synergy India 2min


Supports


Supports implies that the EJB will be included in any client-
started transactions.
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit

Scenario 2 (No Client


TX): Contai EJ
Client method method B
ner

Transactions Grey Matter, Sella Synergy India 2min


Required


Required transactions force the EJB to be executed within a
transaction started either by the client or the container
Active
Transaction
Scenario 1 (Client TX): No Active
Transaction EJ
Client 1 – begin Contai method B
2 – method ner
3–
Commit

Scenario 2 (No Client


TX): Contai 1 – begin EJ
Client method B
ner 2 – method
3–
Commit
Transactions Grey Matter, Sella Synergy India 2min
RequiresNew


RequiresNew transactions force the EJB to execute within a new
transaction that can only be started the container
Active
Transaction
No Active
Scenario 1 (Client TX): Transaction
New EJ
Client 1 – begin Contai Transaction B
method
2 – method ner
3–
Commit

Scenario 2 (No Client


TX): Contai 1 – begin EJ
Client method B
ner 2 – method
3–
Commit
Transactions Grey Matter, Sella Synergy India 2min
A CMT Example

ejb-jar.xml Snippet:
<enterprise-beans>
<session>
<ejb-name> AnEJB</ejb-name>
...
<transaction – type>Containers</transaction – type>
</session>
</enterprise-bean>
...
<assembly-descriptor>
<container – transaction>
<method>
<ejb-name>AnEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
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-isolation><!--last element of this file -->
<isolation-
level>TRANSACTION_READ_UNCOMMITTED</isolation-level>
<method> <ejb-name>dogEJB</ejb-name>
<method-name>*</method-name></method>
</transaction-isolation>
<!-- More transaction-isolation tags -->
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

JavaException Handling
Collections Framework Grey Matter, Sella Synergy India 2min

You might also like