Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
EJB 3 Transactions
Course Multi Tier Business Applications with Java EE
Prof. Dr. Eric Dubuis
Berner Fachhochschule
Biel
Content
June 4, 2008
The problem
Transactions explained
Sample scenarios
Transactions and EJBs
Bean provider responsibilities
Transaction attributes
Explicit rollback
Transactions and stateful session bean
MTA with Java EE / Transactions
The Problem
d:Account
b:BankBean
c:Account
June 4, 2008
Enter Transactions
Definition: Transaction
A (technical) transaction is a series of operations that appear in one
large atomic operation. Transactions guarantee an all-or-nothing
semantic: either all of your operations will succeed, or none of them
will.
Properties of a Transaction
These properties are called the ACID properties:
A:
atomicity
C:
consistency
I:
isolation
D:
durability
June 4, 2008
June 4, 2008
June 4, 2008
June 4, 2008
June 4, 2008
10
Client-Managed Demarcations
June 4, 2008
11
Client-Managed Demarcations
June 4, 2008
12
Transaction Management
Declarative:
Explicit Control:
June 4, 2008
13
June 4, 2008
Container
controls the
transaction
demarcation
Programmer
has explicit
control on
transaction
demarcation
Demarcation:
Start and end point
for a transaction
14
15
June 4, 2008
16
Transaction Attributes
These are the attributes:
MANDATORY
REQUIRED
REQUIRES_NEW
SUPPORTS
NOT_SUPPORTED
NEVER
June 4, 2008
17
From [Burke/Monson-Haefel]
June 4, 2008
18
From [Burke/Monson-Haefel]
June 4, 2008
19
From [Burke/Monson-Haefel]
June 4, 2008
20
From [Burke/Monson-Haefel]
June 4, 2008
21
From [Burke/Monson-Haefel]
June 4, 2008
22
From [Burke/Monson-Haefel]
June 4, 2008
23
If not satisfied with the default REQUIRED, you declare the transaction
attribute by using an annotation.
(Alternatively, you can declare the transaction attribute in the deployment
descriptor, see later.)
@Stateful
public class BankBean implements Bank {
// ...
June 4, 2008
if an attribute is valid
for all methods of a
bean then it can be
specified on the
bean class...
@TransactionAttribute
(TransactionAttributeType.REQUIRES_NEW)
public void transfer(String debitAccountId,
String creditAccountId, double amount)
throws BankException {
...
...and it can
}
be
overridden
when
necessary
MTA with Java EE / Transactions
24
Annotation Definition
package javax.persistence;
public enum TransactionAttributeType {
MANDATORY,
REQUIRED,
REQUIRES_NEW,
SUPPORTS,
NOT_SUPPORTED,
NEVER
}
package javax.persistence;
@Target({METHOD, TYPE}) @Retention(RUNTIME)
public @interface TransactionAttribute {
TransactionAttributeType value() default REQUIRED;
}
June 4, 2008
25
You can specify or override the transaction annotation with the help of
the deployment descriptor. For example:
June 4, 2008
26
Style 1
<method>
<ejb-name>EJBNAME</ejb-name>
<method-name>*</method-name>
</method>
Style 2
1<method>
2
<ejb-name>EJBNAME</ejb-name>
3
<method-name>METHODNAME</method-name>
4</method>
June 4, 2008
27
Style 3
<method>
<ejb-name>EJBNAME</ejb-name>
<method-name>METHODNAME</method-name>
<method-params>
<method-param>PARAMETER_1</method-param>
...
<method-param>PARAMETER_N</method-param>
</method-params>
</method>
This variant is
necessary if there
are overloaded
methods
June 4, 2008
28
}
June 4, 2008
29
The
javax.ejb.SessionSynchroniz
ation interfaces allows a
session bean to receive
additional notification
regarding transactions. The
sets of states of a stateful
session bean with
transactions looks like [EJBCore]:
June 4, 2008
30
Miscellaneous
Further Notes
BMT demarcation
June 4, 2008
31
References
[EJB]
[EJB Core]
[EJB Persistence]
JSR 220: Enterprise JavaBeansTM,Version 3.0
Java Persistence API
http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html
[Burke/Monson-Haefel]
"Enterprise JavaBeans 3.0" Bill Burke and Richard
Monson-Haefel, 5th edition, O'Reilly, 2006,
ISBN: 0-596-00978-X.
June 4, 2008
32