Transactions and Transaction Processing in Java - Prof. Ye Wu, Study notes of Engineering

An overview of transactions, their processing, and isolation levels in the context of java enterprise edition (ejb). The author discusses the concept of a transaction, its atomicity, consistency, isolation, and durability. The document also covers flat and nested transactional processing, transaction attributes, and programmatic transactions using usertransaction interface. Additionally, it explains the different isolation levels and their impact on read and write operations.

Typology: Study notes

Pre 2010

Uploaded on 02/12/2009

koofers-user-psv-1
koofers-user-psv-1 🇺🇸

7 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Transactions
Ye Wu
http://www.ise.gmu.edu/~wuye
SWE 645
Component-based Software Development
2006-10-25 © Dr. Ye Wu 2
What is a Transaction
A single unit of work that embodies the various individual
steps that comprise a composite exchange.
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 3
Flat Transactional Processing
Transaction
Executing
Transaction
Committed
Transaction
Rolled Back
OK
Abort
2006-10-25 © Dr. Ye Wu 4
Nested Transactions Processing
Transaction
Executing
Transaction
Committed
OK
Abort
Perform One or more
Smaller grained Transactions
Transaction
Rolled Back
pf3
pf4
pf5

Partial preview of the text

Download Transactions and Transaction Processing in Java - Prof. Ye Wu and more Study notes Engineering in PDF only on Docsity!

Transactions

Ye Wu

http://www.ise.gmu.edu/~wuye

SWE 645

Component-based Software Development

2006-10-

© Dr. Ye Wu

2

What is a Transaction

-^

A single unit of work that embodies the various individualsteps that comprise a composite exchange.

–^

Atomictiy – A transaction will be treated as a single unit of work

-^

Consistency – Guarantee that the transaction will leave the systemor data in a consistent state.

-^

Isolation – Data that a transaction accesses will not be affected byany changes make by other transactions until the first transactioncompletes

-^

Durability – When a transaction is committed, any changes to datathat is make must be recorded in permanent storage.

2006-10-

© Dr. Ye Wu

3

Flat Transactional Processing

TransactionExecuting

TransactionCommitted TransactionRolled Back

OK

Abort

2006-10-

© Dr. Ye Wu

4

Nested Transactions Processing

TransactionExecuting

TransactionCommitted

OK Abort

Perform One or more Smaller grained Transactions

TransactionRolled Back

2006-10-

© Dr. Ye Wu

5

Transactions and EJB

Programmatic transactions

Declarative transactions (Container managedtransactions)

Client-Initiated Transactions

2006-10-

© Dr. Ye Wu

6

Transaction Attribute – Container

Managed Transactions

-^

Supports Transactions

–^

Supports – specified method will use a transaction if one is alreadyavailable. Otherwise no transaction.

-^

Required – specified method must always run within a transaction

-^

RequiresNew – specified method must always run within its owntransaction

-^

Mandatory – specified method must be invoked within the contextof a pre-existing transaction

-^

Transactions not Supported

–^

NotSupported – the method should not be run within a transaction

-^

Never -

2006-10-

© Dr. Ye Wu

7

Transaction Attribute Usage

Required – when the code needs to change thevalue of some data

Supports – when the code needs to read data froma data source

NotSupported – Communicating with resourcesthat do not support transactional processing

2006-10-

© Dr. Ye Wu

8

Programmatic Transaction

-^

Maintaining transactional state across multiplemethods in a stateful session bean.

-^

Providing transactional behavior for a resourcethat doesn’t provide transactional processing

-^

Java Transaction Service(JTS) – System level

-^

Java Transaction API(JTA) – Application level

2006-10-

© Dr. Ye Wu

13

Isolation Levels

Example

Customer customer =

entityManager.find(Customer.

class

,id);

R1:

System.

out

.println(custom

er.getFirstName() + " "+customer.getLastName());

R2:

entityManager.refresh(cust omer); System.

out

.println(customer.ge

tFirstName() + " "+customer.getLastName());

Customer customer =

entityManager.find(Customer.

class

,id);

W1:

customer.setFirstName(fir stName);

W2:

customer.setLastName(las tName);

AAA BBB

CCC DDD

2006-10-

© Dr. Ye Wu

14

Isolation LevelsREAD UNCOMMITTED

R1:

System.

out

.println(custom

er.getFirstName() + " "+customer.getLastName());

AAA BBB

R2: System.

out

.println(customer.ge

tFirstName() + " "+customer.getLastName());

CCC BBB

W1:

cust.setFirstName(fName);

CCC BBB

W2:

cust.setLastName(lName);

CCC DDD

2006-10-

© Dr. Ye Wu

15

Isolation Levels

READ COMMITTED

R1:

System.

out

.println(custom

er.getFirstName() + " "+customer.getLastName());

AAA BBB

R2: System.

out

.println(customer.ge

tFirstName() + " "+customer.getLastName());

AAA BBB

W1:

cust.setFirstName(fName);

CCC BBB

W2:

cust.setLastName(lName);

CCC DDD

EJB3.0 Default

2006-10-

© Dr. Ye Wu

16

Isolation Levels

READ COMMITTED

R1:

System.

out

.println(custome

r.getFirstName() + " "+customer.getLastName());

AAA BBB

R2: System.

out

.println(customer.ge

tFirstName() + " "+customer.getLastName());

CCC DDD

W1:

cust.setFirstName(fName);

CCC BBB

W2:

cust.setLastName(lName);

CCC DDD

2006-10-

© Dr. Ye Wu

17

Isolation LevelsREAD REPEATABLE

R1:

System.

out

.println(custome

r.getFirstName() + " "+customer.getLastName());

AAA BBB

R2: System.

out

.println(customer.ge

tFirstName() + " "+customer.getLastName());

AAA BBB

W1:

cust.setFirstName(fName);

CCC BBB

W2:

cust.setLastName(lName);

CCC DDD

EJB3.0 READ Lock

2006-10-

© Dr. Ye Wu

18

Isolation Levels

SERIALIZABLE

R1:

System.

out

.println(custom

er.getFirstName() + " "+customer.getLastName());

R2: System.

out

.println(customer.ge

tFirstName() + " "+customer.getLastName());

W1:

cust.setFirstName(fName);

W2:

cust.setLastName(lName);

EJB3.0 WRITE Lock

2006-10-

© Dr. Ye Wu

19

Locking

Optimistic LockingA model takes an optimistic approach to locking the entity.i.e. it assumes there is a good chance that the transaction inwhich changes are made to an entity will be the only onethat actually changes the entity during that interval.

Pessimistic LockingA model that eagerly obtain a lock on the resource beforeoperating on it.

2006-10-

© Dr. Ye Wu

20

Optimistic Locking

@Entitypublic class Customer implements Serializable {

@Versionprivate long version;public long getVersion() {

return version;

}public void setVersion(long version) {

this.version = version;

} }