ejb transactions

30
©1996-2000 jGuru.com EJB Transactions

Upload: leah-parker

Post on 30-Dec-2015

45 views

Category:

Documents


3 download

DESCRIPTION

EJB Transactions. Transactions. Simple Transaction Transaction = more than one statement which must all succeed (or all fail) together If one fails, the system must reverse all previous actions Also can’t leave DB in inconsistent state halfway through a transaction - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: EJB Transactions

©1996-2000 jGuru.com

EJB Transactions

Page 2: EJB Transactions

Transactions

• Simple Transaction– Transaction = more than one statement which

must all succeed (or all fail) together– If one fails, the system must reverse all previous

actions– Also can’t leave DB in inconsistent state halfway

through a transaction– COMMIT = complete transaction– ROLLBACK = abort

Page 3: EJB Transactions

Transactions (cont.)

• Distributed Transaction– Transaction involves

• many objects• many statements• many hosts• many databases

– Two-phase commit required

Page 4: EJB Transactions

Distributed Transaction Example

• Client starts transaction• Withdraws money from numbered Swiss bank

account• Deposits amount into offshore Cayman

Islands account• Each remote bank reads and writes from

multiple databases• Transaction commits across all databases

simultaneously

Page 5: EJB Transactions

Transaction Technology

• ACID– Atomicity– Consistency– Isolation– Durability

• Relational DBs– XA

• Object standards– OMG OTS/JTS– MS ITransact– EJB

• JTA -> JTS -> OTS

Page 6: EJB Transactions

CoordinatorCoordinator

Object BObject BObject AObject A

Prepared?Prepared?

Yes Yes

CoordinatorCoordinator

Object BObject BObject AObject A

Commit Commit

Phase 1: Prepare Phase 2: Commit

Two-phase commit

Page 7: EJB Transactions

Transactions

• an atomic unit of work• can consist of multiple operations from multiple

objects• may support the following features

– distribution across a network– two-phase commits– nested transactions

• transaction commands– begin - start a new transaction– commit - apply requested operations/changes– rollback - undo requested operations/changes

Page 8: EJB Transactions

OTS - Object Transaction Service

• defines interfaces and semantics for transaction service• specifies the following features

– transactional objects and servers– recoverable objects and servers– distributed transactions (propagation)– two-phase commit– nested transactions

• defines these interfaces– Current– TransactionFactory– Control / Terminator / Coordinator– Resource– Synchronization– TransactionalObject

Page 9: EJB Transactions

OTS Architecture

commit()rollback()

Resource

register_resource(Resource r)register_synchronization(Synchronization s)rollback_only()

Coordinator

commit()rollback()

Terminator

get_terminator()get_coordinator()

Control commit()rollback()

Resource

commit()rollback()

Resource

commit()rollback()

Resource(2 Phase)

before_completion()after_completion(Status s)

Synchronization

Page 10: EJB Transactions

Transactional vs Recoverable

JDB Connection asTransactional ObjectTransaction Service

JDBC Connection asRecoverable Object

begin()

connect()

after_completion() -> conn.rollback() -> conn.commit()

connect()

register_resource(conn)commit()rollback()

commit()or

rollback()

Page 11: EJB Transactions

JTS - Java Transaction Service

• javax.jts.UserTransaction• provides an interface to a transaction service• represents a subset of OTS 1.1• may be used by EJB clients and bean-managed

enterprise beans• EJB specification does not support

– nested transactions– recoverable objects/servers

• EJB server vendors will likely provide support for recoverable objects, like database connections

Page 12: EJB Transactions

EJB Transaction Support• transaction control specified in DD

– entire EJB instance– per method

• types of transaction control– TX_NOT_SUPPORTED– TX_SUPPORTS– TX_REQUIRED– TX_REQUIRES_NEW– TX_BEAN_MANAGED– TX_MANDATORY

• session beans implement SessionSynchronization

• container/server– transactions across databases– transactions across EJB servers– integration with CORBA Transaction Service (OTS)

Page 13: EJB Transactions

EJB Transaction Interfaces

• SessionSynchronization• void afterBegin();• void beforeCompletion();• void afterCompletion(status);

• EJBContext• UserTransaction getUserTransaction();• boolean getRollbackOnly();• void setRollbackOnly();

Page 14: EJB Transactions

EJB Transaction Interfaces (cont.)

• javax.jts.UserTransaction• void begin();• void commit();• void rollback();• void setRollbackOnly();• int getStatus();

– transactions usually managed by the container– only EJBs that have the transaction attribute TX_BEAN_MANAGED can use this interface

Page 15: EJB Transactions

EJB Transaction Architecture

Synchronization afterBegin() beforeCompletion() afterCompletion()

Transaction Service

EJBObject

insertData(data) {createTrans();trans.begin();ejb.insertData(data);

if (rollbackRequested)trans.rollback();

elsetrans.commit();

jtsDB(Resource)

transinsertData(data) {

JTSDriver.connect();conn.insert(data);return;

}

EJB Container

// Driver.connect()

Page 16: EJB Transactions

EJB Transaction SequenceClient EJBHome EJBObject Synchron Instance Trans Svc Database

javax.jts.UserTransaction.begin()

business method

register_synchronization(synch)

afterBegin()

access database

regis_res()

business method

javax.jts.UserTransaction.commit()

beforeCompletion()

beforeCompletion()

commit()

afterCompletion(s)

afterCompletion(s)

write updates to database

Page 17: EJB Transactions

Using Transactions with EJB

• a client can control transaction scope– vendor may provide standard Current object

– transactions usually controlled by container, not client

• Current current = new Current();• current.setServiceProviderURL(…);• current.begin();• // get my remote reference here• remRef1.doSomething();• remRef2.doSomethingElse();• current.commit();

Page 18: EJB Transactions

Using Transactions with EJB (cont.)

• transaction control specified in deployment descriptor– per object– per object/method

• six different transaction attributes

• does not support– nested transactions– recoverable objects

Page 19: EJB Transactions

Creating transactional bean

• Home

• Interface

• Bean– Optionally transactional client

• Deployment Descriptor– Define Transaction attributes– Define Isolation Level

• Client can define Transactions

Page 20: EJB Transactions

Three (two?) major styles of transactions

• Container managed (implicit)– Clients nor Bean developers never see

transactions– Specified by descriptor– Safest, most robust technique

• Bean Managed (explicit)– Bean manages

• Client Managed (explicit)– Client begins and ends

Page 21: EJB Transactions

Client Managed Sample:

// get Home

javax.transaction.UserTransaction tx = (javax.transaction.UserTransaction)home;

tx.begin();

// Home.create/find, business methods

tx.commit(); // or tx.rollback();

Page 22: EJB Transactions

Bean-Managed Transactions

• Same operations as client managed

• Performed inside methods

• Bean retrieves transaction context from enterprise context

Page 23: EJB Transactions

Container-Managed Transactions

• Container tool converts implicit to explicit• Container is the transactional client• Usually manages transaction co-ordination• Reads EJB-Jar for bean and deployment

descriptor• 2 Possible uses of DD

– Create code to create transaction in applicable methods

– Create code to check descriptor at run-time

• Layers on JTS/JTA

Page 24: EJB Transactions

Transaction Attributes

• TX_NOT_SUPPORTED– will not start a transaction– existing transaction is suspended

• TX_SUPPORTS– will not start a transaction– existing transaction is not suspended

• TX_REQUIRED– will use existing transaction– will start new transaction, commit when method

completes

Page 25: EJB Transactions

Transaction Attributes

• TX_REQUIRES_NEW– always starts new transaction, commit when

complete– suspend existing transaction

• TX_BEAN_MANAGED– bean can/may use JTS interface to control

transaction

• TX_MANDATORY– must be called within a transaction

Page 26: EJB Transactions

Bean-Managed Transactions• bean obtains transaction context

• UserTransaction ut = myContext.getUserTransaction();

– will fail if not TX_BEAN_MANAGED

• stateful session bean– container suspends existing client transaction– container preserves bean-created transaction across

instance method calls until bean commits or rolls back– only one transaction can exist for the bean

• entity beans and stateless session beans– bean must commit or roll back transaction within a

method– transaction cannot remain open across method calls

Page 27: EJB Transactions

Transaction Specification Requirements

• transaction isolation levels– TRANSACTION_READ_UNCOMMITTED– TRANSACTION_READ_COMMITTED– TRANSACTION_REPEATABLE_READ– TRANSACTION_SERIALIZABLE– declared in the deployment descriptor

• method isolation levels override bean isolation levels• TX_BEAN_MANAGED transaction attribute cannot be mixed

with other transaction attributes declared for a bean• isolation levels must be the same across methods that call

each other

Page 28: EJB Transactions

The SessionSynchronization Interface

• optional interface for session EJBs• invoked by container to inform EJB of transaction state

public interface javax.ejb.SessionSynchronization {

// informs EJB that a transaction has begunpublic void afterBegin();

// informs EJB that the transaction is about to// be committed

public void beforeCompletion();

// informs EJB that the transaction has been// committed or rolled back.

public void afterCompletion(boolean committed);}

Page 29: EJB Transactions

Using Synchronization Callbacks• cannot be implemented by stateless session

beans• afterBegin()

– tells the session bean that it is now in a transaction context

– all business method invocations will now be associated with this transaction context

– a client request will fail if it attempts to invoke a method where the DD specifies a different (or no) transaction context

Page 30: EJB Transactions

Synchronization Callbacks

• beforeCompletion()– a commit is about to be attempted by the

transaction service– no one requested a rollback– last chance to write cached data to the database

• afterCompletion(boolean yn)– commit has completed– yn will indicate whether transaction was

committed or rolled back– can be used to reset conversational state