You are on page 1of 6

Transactions

What is a Transaction
• A single unit of work that embodies the various individual steps that comprise a composite exchange.

Ye Wu http://www.ise.gmu.edu/~wuye SWE 645 Component-based Software Development

– Atomictiy – A transaction will be treated as a single unit of work – Consistency – Guarantee that the transaction will leave the system or data in a consistent state. – Isolation – Data that a transaction accesses will not be affected by any changes make by other transactions until the first transaction completes – Durability – When a transaction is committed, any changes to data that is make must be recorded in permanent storage.

2006-10-25

© Dr. Ye Wu

2

Flat Transactional Processing
Transaction Committed

Nested Transactions Processing
Transaction Committed

OK Transaction Executing Abort

OK Transaction Executing

Transaction Rolled Back

Abort

Transaction Rolled Back

Perform One or more Smaller grained Transactions
2006-10-25 © Dr. Ye Wu 3 2006-10-25 © Dr. Ye Wu 4

1

Ye Wu 7 2006-10-25 © Dr. Ye Wu 6 Transaction Attribute Usage • Required – when the code needs to change the value of some data • Supports – when the code needs to read data from a data source • NotSupported – Communicating with resources that do not support transactional processing Programmatic Transaction • Maintaining transactional state across multiple methods in a stateful session bean.Transactions and EJB • Programmatic transactions • Declarative transactions (Container managed transactions) • Client-Initiated Transactions Transaction Attribute – Container Managed Transactions • Supports Transactions – Supports – specified method will use a transaction if one is already available. – Required – specified method must always run within a transaction – RequiresNew – specified method must always run within its own transaction – Mandatory – specified method must be invoked within the context of a pre-existing transaction • Transactions not Supported – NotSupported – the method should not be run within a transaction – Never 2006-10-25 © Dr. Otherwise no transaction. Ye Wu 5 2006-10-25 © Dr. Ye Wu 8 2 . • Providing transactional behavior for a resource that doesn’t provide transactional processing • Java Transaction Service(JTS) – System level • Java Transaction API(JTA) – Application level 2006-10-25 © Dr.

Ye Wu 10 Declaration of Transactional Attribute @TransactionAttribute(REQUIRED) public class StatelessEJB implements StatelessRemote { …… @TransactionAttribute(REQUIRES_NEW) public void removeCustomer(int id) { Transaction Isolation • Isolation levels – – – – READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE 2006-10-25 © Dr. Ye Wu 9 2006-10-25 © Dr. Ye Wu 11 2006-10-25 © Dr.Programmatic Transaction UserTransaction Interface – – – – – – begin() commit() rollback() getStatus() setRollbackOnly() setTransactionTimeout() Programmatic Transaction UserTransaction Interface – Only allowed in session bean or MDB – No nested transactions – Stateless bean has to finish the transaction in the same invocation – MDB has to finish the transaction before the onMessage() method returns 2006-10-25 © Dr. Ye Wu 12 3 .

out.getFirstName() + " " +customer.println(custom er. Ye Wu 16 W2:cust.out.println(customer.refresh(cust omer).setLastName(las tName).out.getLastName()). CCC DDD 2006-10-25 © Dr.id). Ye Wu 14 Isolation Levels READ COMMITTED R1:System.out.0 Default Isolation Levels READ COMMITTED R1:System.ge tFirstName() + " " +customer.getFirstName() + " " +customer. CCC DDD 2006-10-25 © Dr.setFirstName(fName).out. AAA BBB Example Customer customer = entityManager. W2:cust.find(Custom er.find(Custom er.id).println(customer. W1:cust. CCC DDD CCC DDD 13 2006-10-25 © Dr.getLastName()).getLastName()).println(custom er. CCC BBB CCC BBB R2:entityManager.getLastName()).println(custome r.ge tFirstName() + " " +customer. System. CCC BBB W2:cust.setFirstName(fName).println(customer. R2: System. R2: System.println(customer.ge tFirstName() + " " +customer. W1:customer.out. R1:System.setFirstName(fName). AAA BBB CCC BBB W1:cust.getLastName()).getFirstName() + " " +customer. CCC DDD R2: System. 2006-10-25 © Dr. Ye Wu 15 4 .class.ge tFirstName() + " " +customer.getLastName()). Ye Wu W2:customer.out.class.setLastName(lName).Isolation Levels AAA BBB Customer customer = entityManager.setFirstName(fir stName). AAA BBB W1:cust.getFirstName() + " " +customer.setLastName(lName).getLastName()).getLastName()). Isolation Levels READ UNCOMMITTED R1:System.setLastName(lName).out. AAA BBB EJB3.println(custom er.

AAA BBB EJB3.e.getLastName()). } public void setVersion(long version) { this.println(custome r.out.out. AAA BBB 2006-10-25 © Dr.setFirstName(fName).getLastName()). W2:cust.getLastName()).setLastName(lName).getFirstName() + " " +customer. Ye Wu 20 5 . W1:cust. } } • Pessimistic Locking A model that eagerly obtain a lock on the resource before operating on it. Ye Wu 18 Locking • Optimistic Locking A model takes an optimistic approach to locking the entity.out. i.ge tFirstName() + " " +customer.0 WRITE Lock W1:cust. CCC BBB R2: System.println(customer.setFirstName(fName). CCC DDD R2: System. EJB3.Isolation Levels READ REPEATABLE R1:System.ge tFirstName() + " " +customer. Ye Wu 19 2006-10-25 © Dr.println(customer.out. 2006-10-25 © Dr. @Entity Optimistic Locking public class Customer implements Serializable { @Version private long version.getLastName()). public long getVersion() { return version. W2:cust. it assumes there is a good chance that the transaction in which changes are made to an entity will be the only one that actually changes the entity during that interval.version = version.getFirstName() + " " +customer.0 READ Lock Isolation Levels SERIALIZABLE R1:System.setLastName(lName). Ye Wu 17 2006-10-25 © Dr.println(custom er.

Discard the EJB instance • Application Exception In responds to business-logic error © Dr. Ye Wu 21 2006-10-25 6 .lang. Roll back transaction 2.RuntimeException and all its subclasses.Exception handling • System Exception java. 1. Log the exception to alert the sys admin 3.