Download presentation
Presentation is loading. Please wait.
1
J2EE Lecture 12: Transaction
Dr. Ming Qiu Xiamen University Software School
2
Mastering EJB 3.0, Chap 10 12.1 Introduction Transactions make the mission-critical operations run predictably In the past, developers need to code directly to a transaction API. With EJB, you can gain the benefits of transactions without writing any transaction code
3
12.2 Motivation for transactions
Mastering EJB 3.0, Chap 10 12.2 Motivation for transactions Atomic operations Transfer funds from one account to another
4
12.2 Motivation for transactions
Mastering EJB 3.0, Chap 10 12.2 Motivation for transactions Many problems with this approach The code is bulky and unwieldy Need to consider every possible problem that might occur an consider how to roll back our changes Error handling gets out of control if we perform more complex processes Testing this code is a challenge We would like a way to perform both operations in a single, large, atomic operation
5
12.2 Motivation for transactions
Mastering EJB 3.0, Chap 10 12.2 Motivation for transactions Network or Machine Failure
6
12.2 Motivation for transactions
Mastering EJB 3.0, Chap 10 12.2 Motivation for transactions Multiple Users Sharing Data
7
12.3 Benefits of Transactions
Mastering EJB 3.0, Chap 10 12.3 Benefits of Transactions To avoid all these problems Use transactions Transaction A series of operations that appear to execute as one large, atomic operation Guarantee an all-or-nothing value proposition Account for network or machine failure in a graceful, reliable way Allow multiple users to share the same data Guarantee that any update of data is completely and wholly written
8
12.3 Benefits of Transactions
Mastering EJB 3.0, Chap 10 12.3 Benefits of Transactions EJB in Action, Chap 6 The ACID Properties Atomicity Guarantees that many operations are bundled together and appear as one contiguous unit of work Consistency Guarantees that a transaction leaves the system’s state to be consistent after a transaction completes. The system need not be in a consistent state during the transaction The balance of account may temporarily be negative during the transaction.
9
12.3 Benefits of Transactions
Mastering EJB 3.0, Chap 10 12.3 Benefits of Transactions Isolation Protects concurrently executing transaction without seeing each other’s incomplete results. Ensures that nobody touches your data while you are in the transaction Locks on data are automatically assigned as necessary Durability Guarantees that updates to managed resource A transaction, once committed, is guaranteed to become permanent. Implemented by using transaction logs in the database server
10
Distributed System Concepts and Design, Chap 16
12.4 Transactional Models Transactions originate from database management systems. In that context, a transaction is an execution of a program that accesses a database. Transactions were introduced to distributed systems in the form of transactional file servers a transaction is an execution of a sequence of client requests for file operations. From the client’s point of view, a transaction is a sequence of operations that forms a single step, transforming the server data from one consistent state to another.
11
12.4 Transactional Models Flat Transactions
Mastering EJB 3.0, Chap 10 12.4 Transactional Models Flat Transactions A series of operations that are performed atomically as a single unit of work Problems that make a transaction abort Invalid parameters passed to one of the components An invariant system state was violated Hardware or software failure
12
Mastering EJB 3.0, Chap 10 12.4 Transactional Models
13
12.4 Transactional Models How transactional State is Rolled Back
Mastering EJB 3.0, Chap 10 12.4 Transactional Models How transactional State is Rolled Back When Persist operations happen in a transaction, Resource manager does not permanently apply the updates to database, but waits until a commit statement has been issued If the transaction aborts, the data is not persisted at all.
14
12.4 Transactional Models Famous trip-planning problem
Mastering EJB 3.0, Chap 10 12.4 Transactional Models Famous trip-planning problem Purchase a train ticket from Boston to New York Purchase a plane ticket from New York to London Purchase a balloon ride ticket from London to Paris Finds out that there are no outgoing flight from France If performed under a flat transaction, roll back the entire transaction But it may be possible to use another way of transportation out of France
15
12.4 Transactional Models Nest Transaction
Mastering EJB 3.0, Chap 10 12.4 Transactional Models Nest Transaction Enable you to embed atomic units of work within other unit of work It can roll back without forcing the entire transaction to roll back The large unit can attempt to retry the embedded unit of work
16
Mastering EJB 3.0, Chap 10 12.4 Transactional Models
17
12.4 Transactional Models Flat Transactions Nested Transactions
Distributed System Concepts and Design, Chap 17 12.4 Transactional Models Flat Transactions Nested Transactions Client X Y Z M N T 1 2 11 P 12 21 22
18
12.4 Transactional Models Nested Transactions .
Distributed System Concepts and Design, Chap 17 12.4 Transactional Models Nested Transactions a.withdraw(10) c . deposit(10) b.withdraw(20) d.deposit(20) Client A B C T 1 2 3 4 D X Y Z T = openTransaction openSubTransaction a.withdraw(10); closeTransaction b.withdraw(20); c.deposit(10); d.deposit(20);
19
Mastering EJB 3.0, Chap 10 12.4 Transactional Models EJB in Action, Chap 6 Application servers and enterprise resources (DBMS) do most of the heavy lifting in transaction management. Resource Persistent storage from which you read or write Database, a message queue, or other storage Resource Manager DBMS, message queue, or other storage Transactional Object or transactional component an enterprise bean, a .NET-managed component, a CORBA component Transaction Manager (the application server) Responsible for coordinates a transaction over multiple distributed resources
20
EJB in Action, Chap 6 12.4 Transactional Models
21
Distributed System Concepts and Design, Chap 17
12.4 Transactional Models . BranchZ BranchX participant C D Client BranchY B A join T a.withdraw(4); c.deposit(4); b.withdraw(3); d.deposit(3); openTransaction b.withdraw(T, 3); closeTransaction T = Note: the coordinator is in one of the servers, e.g. BranchX
22
12.5 Distributed Transaction
Mastering EJB 3.0, Chap 10 12.5 Distributed Transaction Some possible scenarios where distributed transactions are applicable Multiple application servers coordinating in the same transaction Update to different databases in the same transaction Perform a database update and send or receive a JMS message from a message queue in the same transaction Connect to a legacy system as well as one or more other type of resources in the same transaction Requires collaboration of one or more types of resource managers across multiple processes to solve a business problem
23
12.5 Distributed Transaction
Mastering EJB 3.0, Chap 10 12.5 Distributed Transaction Durability Guarantees that all resource updates that are committed are made permanent If multiple resource managers are involved in transaction, it is difficult to implement durability
24
12.5 Distributed Transaction
Distributed System Concepts and Design, Chap 17 12.5 Distributed Transaction The Two-Phase Commit Protocol Designed to allow any participant to abort its part of a transaction. Due to the requirement for atomicity, if one part of a transaction is aborted, then the whole transaction must be aborted.
25
12.5 Distributed Transaction
Distributed System Concepts and Design, Chap 17 12.5 Distributed Transaction Two-Phase Commit Protocol For Flat Transactions Phase One The coordinator sends a canCommit? request to each of the participants in the transaction. When a participant receives a canCommit? request it replies with its vote (Yes or No) to the coordinator. Before voting Yes, it prepares to commit by saving objects in permanent storage. If the vote is No the participant aborts immediately.
26
12.5 Distributed Transaction
Distributed System Concepts and Design, Chap 17 12.5 Distributed Transaction Two-Phase Commit Protocol For Flat Transactions Phase Two The coordinator collects the votes (including its own). If there are no failures and all the votes are Yes the coordinator decides to commit the transaction and sends a doCommit request to each of the participants. Otherwise the coordinator decides to abort the transaction and sends doAbort requests to all participants that voted Yes. Participants that voted Yes are waiting for a doCommit or doAbort request from the coordinator. When a participant receives one of these messages it acts accordingly and in the case of commit, makes a haveCommitted call as confirmation to the coordinator.
27
12.5 Distributed Transaction
Distributed System Concepts and Design, Chap 17 12.5 Distributed Transaction Two-Phase Commit Protocol For Flat Transactions canCommit? Yes doCommit haveCommitted Coordinator 1 3 (waiting for votes) committed done prepared to commit step Participant 2 4 (uncertain) status
28
12.5 Distributed Transaction
Mastering EJB 3.0, Chap 10 12.5 Distributed Transaction
29
12.6 JTS and JTA Object Transaction Service An optional CORBA service
Mastering EJB 3.0, Chap 10 12.6 JTS and JTA Object Transaction Service An optional CORBA service A suite of well-defined interfaces that specify how transactions can run behind the scenes. CosTransactions Basic interfaces that transactional objects, resources, resource managers and transaction managers use to interoperate CosTSPortability Offer a portable way to perform transactions with many participant
30
12.6 JTS and JTA Java Transaction Service Java binding to OTS
Mastering EJB 3.0, Chap 10 12.6 JTS and JTA Java Transaction Service Java binding to OTS Only system developers are interesting in working with JTS We deal with JTS through the Java Transaction API
31
12.6 JTS and JTA The Java Transaction API
Mastering EJB 3.0, Chap 10 12.6 JTS and JTA The Java Transaction API javax.transaction.xa.XAResource JTA uses it to communicate with X/Open XA-enabled resource manager javax.transaction.TransactionManager JTA uses to communicate with the application server javax.transaction.UserTransaction Used by beans and other application programs to work with the EJB transactions
32
Mastering EJB 3.0, Chap 10
33
12.7 EJB Transactions Demarcating transactional boundaries
Mastering EJB 3.0, Chap 10 12.7 EJB Transactions Demarcating transactional boundaries Who begin a transaction Who issues either a commit or abort When each of these steps occurs Who demarcate the transactional boundaries in EJB world? You can demarcate the transactions yourself Let the container do that for you
34
12.7 EJB Transactions Three kinds of transactions
Mastering EJB 3.0, Chap 10 12.7 EJB Transactions Three kinds of transactions Bean-managed transaction The bean is responsible for issuing a begin, commit or abort statement for the transaction. Container-managed transaction The EJB container performs the transaction management Client-controlled transaction Write code to start and end the transaction from the client code outside of your bean
35
Mastering EJB 3.0, Chap 10 12.7 EJB Transactions
36
Mastering EJB 3.0, Chap 10 12.7 EJB Transactions
37
Mastering EJB 3.0, Chap 10 12.7 EJB Transactions
38
12.7 EJB Transactions Choosing a Transaction Style
Mastering EJB 3.0, Chap 10 12.7 EJB Transactions Choosing a Transaction Style Bean-managed transaction You have full control over transactional boundaries Contained-managed transaction Simple Client-managed transaction use sparingly
39
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction Do not need to write any code about transaction Need to provide instructions to container for how to operate transaction Transaction Attribute Value In the bean’s deployment descriptor For all business methods in the bean
40
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction Contained-Managed Transaction Example
41
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction
42
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction EJB Transaction Attribute Values Required Bean always run in a transaction Enable bean to start a new transaction or join existing ones Examples – Charging a credit card Customer 1 : Customer Service Center Customer 2: Billing component as part of a purchasing workflow
43
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction RequiresNew Always require a new transaction Suspend the existing transaction Supports Make the bean join a existing transaction Similar to Required But not start a new transaction Mandatory Mandates that a transaction must be already running when the bean method is called. Throw javax.ejb.EJBTransactionRequiredException, when no transaction is running
44
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction NotSupported Do not involve a bean in a transaction at all. Examples A calls B, and A begins a transaction When it come to B, the transaction A started is suspended, and none of B’s operations are transactional When B completes, A’s transaction is resume Never Can not involve a bean in a transaction Throw javax.ejb.EJBException when involved in a transaction
45
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction
46
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction
47
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction Contained-Managed Transaction Example
48
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction
49
12.8 Contained-Managed Transaction
EJB in Action, Chap 6 12.8 Contained-Managed Transaction Marking a CMT for rollback The setRollbackOnly and getRollbackOnly methods can only be invoked in an EJB using CMT with these transaction attributes: REQUIRED, REQUIRES_NEW, or MANDATORY. Otherwise, the container will throw an IllegalStateException.
50
12.8 Contained-Managed Transaction
EJB in Action, Chap 6 12.8 Contained-Managed Transaction
51
12.8 Contained-Managed Transaction
EJB in Action, Chap 6 12.8 Contained-Managed Transaction
52
12.8 Contained-Managed Transaction
EJB in Action, Chap 6 12.8 Contained-Managed Transaction
53
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction
54
12.8 Contained-Managed Transaction
Mastering EJB 3.0, Chap 10 12.8 Contained-Managed Transaction
55
12.9 Bean-Managed Transaction
Mastering EJB 3.0, Chap 10 12.9 Bean-Managed Transaction Demarcate transaction by yourself Allow for more transaction control than container-managed transactions do Use the JTA interface javax.transaction.UserTransaction
56
12.9 Bean-Managed Transaction
Mastering EJB 3.0, Chap 10 12.9 Bean-Managed Transaction javax.transaction.UserTransaction
57
12.9 Bean-Managed Transaction
Mastering EJB 3.0, Chap 10 12.9 Bean-Managed Transaction
58
Mastering EJB 3.0, Chap 10
59
12.9 Bean-Managed Transaction
Mastering EJB 3.0, Chap 10 12.9 Bean-Managed Transaction Bean-Managed Transaction Example
60
12.9 Bean-Managed Transaction
Mastering EJB 3.0, Chap 10 12.9 Bean-Managed Transaction Complete your transactions in the same method!
61
12.9 Bean-Managed Transaction
EJB in Action, Chap 6 12.9 Bean-Managed Transaction
62
12.10 Client-Controlled Transaction
Mastering EJB 3.0, Chap 10 12.10 Client-Controlled Transaction Use JTA to Control transactions from client code Get hold of JTA UserTransaction interface By using resource injection (in a EJB) By looking it up in the JNDI registry
63
12.11 Transaction in Spring Spring’s Transaction Manager
Spring in Action, Chap 5 12.11 Transaction in Spring Spring’s Transaction Manager
64
12.11 Transaction in Spring HibernateTransactionManager
Spring Framework Doc 12.11 Transaction in Spring HibernateTransactionManager
65
Spring Framework Doc 12.11 Transaction in Spring
66
Spring Framework Doc 12.11 Transaction in Spring
67
Spring Framework Doc 12.11 Transaction in Spring
68
12.11 Transaction in Spring Transaction propagation
Spring Framework Doc 12.11 Transaction in Spring Transaction propagation
69
12.11 Transaction in Spring Transaction propagation
Spring Framework Doc 12.11 Transaction in Spring Transaction propagation
70
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation Isolation Level READ UNCOMMITTED Does not offer any isolation guarantees (Highest performance) READ COMMITTED Solves the dirty read problem REPEATABLE READ Solves the dirty read and unrepeatable read problem SERIALIZABLE Solves the dirty read, unrepeatable read and phantom problem
71
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation READ UNCOMMITTED No isolation guarantee Highest Performance Dangerous to use in mission-critical systems Appropriate if an instance of the component will be running only when there are no other concurrent transactions.
72
12.12 Transaction Isolation
Distributed System Concepts and Design, Chap 17 12.12 Transaction Isolation Dirty Read a=200, b=300 Transaction V : Transaction W : a.withdraw(100) aBranch.branchTotal() b.deposit(100) a.withdraw(100); $100 total = a.getBalance() $100 total = total+b.getBalance() $300 total = total+c.getBalance() b.deposit(100) $300 The other application reads your update before you committed
73
12.12 Transaction Isolation
Distributed System Concepts and Design, Chap 17 12.12 Transaction Isolation Dirty Read
74
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation READ COMMITTED Read committed data only Appropriate if the programs want to generate a report Not mission critical Create a snap for the committed data Slower than READ UNCOMMITTED Default isolation level for most databases Oracle or Microsoft SQL Server
75
12.11 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.11 Transaction Isolation Unrepeatable Read Problem a=100, b=200, c=300 Transaction T : balance = b.getBalance(); a.withdraw(balance/10); U b.setBalance(balance*1.1); c.withdraw(balance/10) balance = b.getBalance(); $200 $220 a.withdraw(balance/10) $80 $280 balance = b.getBalance(); balance = b.getBalance(); $220 Guarantee by using transactional locks to lock out other transactions from modifying the data
76
12.11 Transaction Isolation
Distributed System Concepts and Design, Chap 17 12.11 Transaction Isolation Lost Update a=100, b=200, c=300 Transaction T : balance = b.getBalance(); b.setBalance(balance*1.1); a.withdraw(balance/10) U c.withdraw(balance/10) balance = b.getBalance(); $200 $220 $80 $280
77
12.12 Transaction Isolation
Hibernate in Action, Chap 10 12.12 Transaction Isolation Unrepeatable Read
78
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation REPEATABLE READ Whenever you read committed data from a database, you will be able to reread the same data again at a later time. Appropriate if you need to update one or more data elements in a resource
79
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation The Phantom Problem Instance A queries the database using some criteria and retrieves a data set Instance B inserts new data that would satisfy your query Instance A perform the query again, the result data set changed.
80
12.12 Transaction Isolation
Hibernate in Action, Chap 10 12.12 Transaction Isolation Phantom Read
81
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation SERIALIZABLE Guarantees that transactions execute serially with respect to each other. Used for mission-critical systems
82
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation Transaction Isolation Summary
83
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation Isolation and Lock By acquiring lock, transactions guarantee serializability But it lead to major performance problems A dead lock scenario can arise To improve performance, two types of locks are introduced Read locks and write locks Locking exists in many cycles Databases, version control systems, and Java language EJB abstract concurrency control away from developers via isolation levels
84
12.12 Transaction Isolation
Mastering EJB 3.0, Chap 10 12.12 Transaction Isolation Pessimistic and Optimistic Concurrency Control
85
12.12 Transaction Isolation
Hibernate in Action, Chap 10 12.12 Transaction Isolation Choosing an isolation model First, eliminate the read uncommitted isolation level Secondly, most applications don’t need serializable isolation a choice between read committed and repeatable read. Read lock and write lock Versioned data
86
12.12 Transaction Isolation
Hibernate in Action, Chap 10 12.12 Transaction Isolation Enabling versioning in Hibernate @Entity public class Item { ... @Version @Column(name = "OBJ_VERSION") private int version; }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.