Presentation is loading. Please wait.

Presentation is loading. Please wait.

©1996-2000 jGuru.com EJB Transactions. Transactions Simple Transaction –Transaction = more than one statement which must all succeed (or all fail) together.

Similar presentations


Presentation on theme: "©1996-2000 jGuru.com EJB Transactions. Transactions Simple Transaction –Transaction = more than one statement which must all succeed (or all fail) together."— Presentation transcript:

1 ©1996-2000 jGuru.com EJB Transactions

2 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

3 Transactions (cont.) Distributed Transaction –Transaction involves many objects many statements many hosts many databases –Two-phase commit required

4 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

5 Transaction Technology ACID –Atomicity –Consistency –Isolation –Durability Relational DBs –XA Object standards –OMG OTS/JTS –MS ITransact –EJB JTA -> JTS -> OTS

6 Coordinator Object B Object A Prepared? Yes Coordinator Object B Object A Commit Phase 1: PreparePhase 2: Commit Two-phase commit

7 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

8 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

9 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

10 Transactional vs Recoverable JDB Connection as Transactional Object Transaction Service JDBC Connection as Recoverable Object begin() connect() after_completion() -> conn.rollback() -> conn.commit() connect() register_resource(conn) commit() rollback() commit() or rollback()

11 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

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

13 EJB Transaction Interfaces SessionSynchronization voidafterBegin(); voidbeforeCompletion(); voidafterCompletion(status); EJBContext UserTransactiongetUserTransaction(); booleangetRollbackOnly(); voidsetRollbackOnly();

14 EJB Transaction Interfaces (cont.) javax.jts.UserTransaction voidbegin(); voidcommit(); voidrollback(); voidsetRollbackOnly(); intgetStatus(); –transactions usually managed by the container –only EJBs that have the transaction attribute TX_BEAN_MANAGED can use this interface

15 EJB Transaction Architecture Synchronization afterBegin() beforeCompletion() afterCompletion() Transaction Service EJBObject insertData(data) { createTrans(); trans.begin(); ejb.insertData(data); if (rollbackRequested) trans.rollback(); else trans.commit(); jtsDB (Resource) trans insertData(data) { JTSDriver.connect(); conn.insert(data); return; } EJB Container // Driver.connect()

16 EJB Transaction Sequence ClientEJBHomeEJBObjectSynchronInstanceTrans SvcDatabase javax.jts.UserTransaction.begin() business method register_synchronization(synch) afterBegin() access database regis_res() business method javax.jts.UserTransaction.commit() beforeCompletion() commit() afterCompletion(s) write updates to database

17 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();

18 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

19 Creating transactional bean Home Interface Bean –Optionally transactional client Deployment Descriptor –Define Transaction attributes –Define Isolation Level Client can define Transactions

20 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

21 Client Managed Sample: // get Home javax.transaction.UserTransaction tx = (javax.transaction.UserTransaction)hom e; tx.begin(); // Home.create/find, business methods tx.commit(); // or tx.rollback();

22 Bean-Managed Transactions Same operations as client managed Performed inside methods Bean retrieves transaction context from enterprise context

23 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

24 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

25 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

26 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

27 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

28 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 begun public voidafterBegin(); // informs EJB that the transaction is about to // be committed public voidbeforeCompletion(); // informs EJB that the transaction has been // committed or rolled back. public voidafterCompletion(boolean committed); }

29 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

30 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


Download ppt "©1996-2000 jGuru.com EJB Transactions. Transactions Simple Transaction –Transaction = more than one statement which must all succeed (or all fail) together."

Similar presentations


Ads by Google