Sie sind auf Seite 1von 5

Transactions

Transaction Characteristics: ACID

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

e.g. online flight reservation


e.g. bank transfer operation
e.g. book order at amazon.com

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

(C) Fachhochschule Aargau


Nordwestschweiz

15 May 2003

Transaction Participants

Transaction Management

Transactional application
An application that uses transactions
May consist of Servlets, JSPs, EJBs

Manual transaction management


application is responsible for
starting transaction
committing or rolling back the transaction
suspending and resuming transactions

Resource manager

disadvantages

Provides and enforces the ACID transaction properties


Examples:

complex code
components difficult to reuse

relational data bases


JMS provider
EIS systems (=> Connector API)

EJB transaction management


advantages

Transaction demarcation

support of distributed transactions


no complex error code

begin
commit or rollback

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

(C) Fachhochschule Aargau


Nordwestschweiz

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

EJB Transaction Management

Transactional Properties

Container Managed

All work of the EJB component must occur within a transaction


The container shall begin a transaction if none is passed to this component

Transaction attributes are defined in the deployment descriptor


for each EJB method (home & remote)
EJB container is responsible to start / suspend transactions
EJB container handles the commit / rollback automatically
Session-, Message-Ddriven and Entity-Beans can use
container managed transactions

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

This bean may or may not execute within a transaction


All work of the EJB instance is made outside of a transaction
Any inherited transaction is suspended

Bean Managed
the bean developer directly does the commit/rollback work
only session beans and message driven beans can be bean managed

Mandatory

Never

This EJB instance must run within a clients transactional context


This EJB instance must never run within a clients transactional context

(C) Fachhochschule Aargau


Nordwestschweiz

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

(C) Fachhochschule Aargau


Nordwestschweiz

Transactional Properties

Transaction
Attribute

RequiresNew

15 May 2003

Required

RequiresNew

Usually used for beans which update databases


Used if a database update must be performed independent of the
success of a client transaction
Example: StatisticBean / LoggingBean

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

Stronger version of Required (helps to detect assembly errors)


Stronger version of NotSupported (helps to detect assembly errors)
(C) Fachhochschule Aargau
Nordwestschweiz

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

Transactional Properties

Transactional Properties: Restrictions

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

attributes can only be defined on business methods (in remote/local


interface)

(C) Fachhochschule Aargau


Nordwestschweiz

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

EJB 2.0 CMP


only use Required, RequiresNew and Mandatory
(support for Never, Supports, NotSupported is optional)

MDB (Message Driven Beans)


only applicable for the onMessage method
only Required and NotSupported can be used
(other modes depend on transaction context of the client)

15 May 2003

Rolling Back a CM-Transaction

Transactions and EntityBeans

Explicit
EJBContext ctx = ;
ctx.setRollbackOnly()

Automatic

if a system exception is thrown, the container will roll-back the


transaction
if an application exception is thrown, the roll-back is not automatic

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

Roll Back

Requires / Mandatory / RequiresNew


the container will call ejbLoad automatically before each transacted
method
the container will call ejbStore automatically after a successful
modification

system exceptions are extensions of RuntimeException


example application exceptions:
CreateException
FinderException
RemoveException
user-defined checked Exceptions

10

if a transaction is rolled back, then the changes made with SQL


statements are not committed
instance variables (cached values) are not restored

causes the container to automatically roll back the transaction

(C) Fachhochschule Aargau


Nordwestschweiz

11

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

12

EJB 2.0: Commit Options

EJB 2.0: Commit Options [JBoss]

<?xml version="1.0" encoding="UTF-8"?>


<jboss>
<enterprise-beans>
<entity>
<ejb-name>AccountEJB</ejb-name>
<configuration-name>
Commit B BMP EntityBean
</configuration-name>
</entity>
</enterprise-beans>
<container-configurations>
<container-configuration extends="Standard BMP EntityBean">
<container-name>Commit B BMP EntityBean</container-name>
<commit-option>B</commit-option>
</container-configuration>
</container-configurations>
</jboss>

Container can select from the following commit options:


Option A: Container caches "ready" Entity-instances
Exclusive data source access is necessary
Synchronization not necessary before each access

Option B: Container caches "ready" Entity-instances, but no exclusive


access to data source
Synchronization necessary at the beginning of a transaction

Option C: No caching
The container returns Entity-instance to the pool after transaction end

Option D [JBoss]: Similar to Option A, but with a regular automatic


refresh
15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

13

EJB 2.0: Commit Options [JBoss]

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

(C) Fachhochschule Aargau


Nordwestschweiz

14

Transactions and Session Beans


Default
B
A
A
B
B
B
B
B

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

void afterCompletion(boolean committed)


parameter informs whether transaction was committed or rolled back

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

15

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

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);}
}

do not call set/getRollbackOnly of EJBContext in bean-managed


transactions

Web Components can control transactions

No nested transactions

access via JNDI (java:comp/UserTransaction)


begin must not be called when a transaction is active
transaction state can be accessed with getStatus
no UserTransactions in a CMT Bean

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

(C) Fachhochschule Aargau


Nordwestschweiz

setRollbackOnly

commit
commit must eventually be called
roll back is performed automatically after a time out

17

15 May 2003

(C) Fachhochschule Aargau


Nordwestschweiz

18

Das könnte Ihnen auch gefallen