1 / 30

EJB Transactions

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

leah-parker
Télécharger la présentation

EJB Transactions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 Coordinator Yes Yes Prepared? Commit Commit Prepared? Object A Object B Object A Object B Phase 1: Prepare Phase 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. Control get_terminator() get_coordinator() Terminator Coordinator commit() rollback() register_resource(Resource r) register_synchronization(Synchronization s) rollback_only() Resource Resource Resource Resource commit() rollback() commit() rollback() commit() rollback() commit() rollback() OTS Architecture (2 Phase) Synchronization before_completion() after_completion(Status s)

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

  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 • void afterBegin(); • void beforeCompletion(); • void afterCompletion(status); • EJBContext • UserTransaction getUserTransaction(); • boolean getRollbackOnly(); • void setRollbackOnly();

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

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

  16. EJB Transaction Sequence Client 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() write updates to database commit() afterCompletion(s) afterCompletion(s)

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

  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

More Related