Professional Documents
Culture Documents
Transactions Transaction Characteristics: ACID: What Is A Transaction? Atomic
Transactions Transaction Characteristics: ACID: What Is A Transaction? Atomic
What is a transaction?
logical unit of work
Atomic
Transaction must execute completely or not at all (all-or-nothing)
If any step fails, changes performed by other steps must be undone
Consistent
Data must remain consistent (existing foreign keys, multiplicities, etc.)
Invariants may be violated during a transaction
Responsibility of programmer!
complex
involves manipulation of a lot of data (e.g. on different DBs)
Isolated
The data that a transaction accesses cannot be affected by any other part
of the system until the transaction is completed
Durable
All changes made in a transaction must be written to physical storage
Changes must not be lost if the system crashes
15 May 2003
15 May 2003
Transaction Participants
Transaction Management
Transactional application
An application that uses transactions
May consist of Servlets, JSPs, EJBs
Resource manager
disadvantages
complex code
components difficult to reuse
Transaction demarcation
begin
commit or rollback
15 May 2003
15 May 2003
Transactional Properties
Container Managed
Required
RequiresNew
This EJB component must be the root of a transaction
If there is a pending transaction the container shall suspend it
Supports
NotSupported
Bean Managed
the bean developer directly does the commit/rollback work
only session beans and message driven beans can be bean managed
Mandatory
Never
15 May 2003
Transactional Properties
Client's
Transaction
Business Method's
Transaction
Required
None
T1
None
T1
None
T1
None
T1
None
T1
None
T1
T2
T1
T2
T2
ERROR
T1
None
None
None
T1
None
ERROR
Mandatory
NotSupported
Supports
Never
15 May 2003
Transactional Properties
Transaction
Attribute
RequiresNew
15 May 2003
Required
RequiresNew
Supports
Methods without (or with an atomic) update operation, dont-care
No overhead => efficient, but it can lead to unpredictable results
NotSupported
Hint for the client that rollback is not supported
Example: Access to a service via HTTP
Mandatory
Never
15 May 2003
Transactional Properties
<container-transaction>
<method>
<ejb-name>AccountEJB</ejb-name>
<method-intf>Home</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
Entity Beans
attributes can be defined for business methods (in remote/local interface)
and create-, findXXX-, remove-methods (in home interfaces)
<container-transaction>
<method> <ejb-name>AccountEJB</ejb-name>
<method-name>deposit</method-name>
</method>
<method> <ejb-name>AccountEJB</ejb-name>
<method-name>withdraw</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
15 May 2003
Session Beans
15 May 2003
Explicit
EJBContext ctx = ;
ctx.setRollbackOnly()
Automatic
15 May 2003
Roll Back
10
11
15 May 2003
12
Option C: No caching
The container returns Entity-instance to the pool after transaction end
13
Container Configuration
Standard CMP 2.x EntityBean
Standard CMP EntityBean
Standard BMP EntityBean
Clustered CMP 2.x EntityBean
Clustered CMP EntityBean
Clustered BMP EntityBean
Instance Per Transaction CMP EntityBean
Instance Per Transaction BMP EntityBean
15 May 2003
14
Roll Back
changed instance variables in a (stateful) session bean are not restored
upon transaction roll back !!!
no ejbLoad / ejbStore for session beans
javax.ejb.SessionSynchronization interface
void afterBegin()
transaction just started
void beforeCompletion()
last chance for a roll back
15 May 2003
15
15 May 2003
16
Bean-Managed Transactions
CMT Remarks
JDBC Transactions
try {
con.setAutoCommit(false); ... ; con.commit();
}
catch(Exception e){
try {con.rollback(); throw new EJBException(e);}
catch(SQLException se){throw new EJBException(se);}
}
No nested transactions
JTA Transactions
UserTransaction ut = context.getUserTransaction();
try {
// ^^^ = bean context !
ut.begin(); ... ; ut.commit();
}
catch(Exception e){
try{ut.rollback(); throw new EJBException(e);}
catch(SystemException se){throw new EJBException(se);}
}
15 May 2003
setRollbackOnly
commit
commit must eventually be called
roll back is performed automatically after a time out
17
15 May 2003
18