Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 152 153 154 155 156 157 < 158 > 159 160 161 162 163 164 .. 166 >> Next

The afterBegin() method, shown on lines 28 to 32, called before the first method invocation inside a transaction, allows us to save our current state. All methods, which are transaction-aware, are then within transaction boundaries. Line 34 shows the before completion method, which is unused in our example but could be used for final cleanup, to reset transaction state (to rollback only for example). Once the transaction has been committed, we need to either reset from our prior state (transaction failed) or simply continue on (transaction succeeded). Lines 39 to 52 show the implementation of the afterCompletion() method and allow us to correctly reset our bean state when a transaction either commits or fails. The boolean committed flag tells us whether the transaction committed (we can discard our previous state) or failed (we need to restore our state).
NOTE
When Transactions Go Awry, or Consistent State in Stateful Session EJBs 437
01 package org.javapitfalls.item50;
02
03 import javax.ejb.*;
04 import java.util.*;
05
06 public class TellerBean implements SessionBean,
07 SessionSynchronization
08 {
09
10 private SessionContext sc;
11 private double balance;
12 private double priorBalance;
13
14 public TellerBean() { balance = 0.0;}
15 public void setSessionContext(SessionContext sc)
16 {
17 this.sc=sc;
18 }
19
20 public void ejbCreate() { }
21
22 public void ejbRemove() { }
23
24 public void ejbPassivate() { }
25
26 public void ejbActivate() { }
27
28 public void afterBegin()
29 {
30 System.out.println("TellerBean::afterBegin");
31 priorBalance = balance;
32 }
33
34 public void beforeCompletion()
35 {
36 System.out.println("TellerBean::beforeCompletion");
37 }
38
39 public void afterCompletion(boolean committed)
40 {
Listing 50.1 TellerBean.java, using session synchronization (continued)
438 Item 50
41 System.out.println("TellerBean::afterCompletion ("
42 + committed + ")");
43 if (committed)
44 {
45 priorBalance=balance; // prior and current state match
46 }
47 else
48 {
49 balance=priorBalance; // restore state
50 }
51
52 }
53
54 public double getBalance()
55 {
56 System.out.println("getBalance" + balance);
57 return balance;
58 }
59
60 public double Withdraw(double amount )
61 {
62 System.out.println("Withdraw" + amount);
63 balance = balance - amount;
64 return balance;
65 }
66
67 public double Deposit(double amount )
68 {
69 System.out.println("Deposit" + amount);
70 balance = balance + amount;
71 return balance;
72 }
73 }
Listing 50.1 (continued)
The Memento Pattern
Our previous scenario showed an example where we only needed to restore a simple set of variables. However, often in real life, we need to restore a complex set of data where setting and restoring variables can be a tedious and error-prone operation. To solve the restore problem, we can use a variant of the value object pattern known as the Memento pattern.
When Transactions Go Awry, or Consistent State in Stateful Session EJBs 439
The Memento pattern is a rather simple pattern whose purpose is to store the state of an object. When using a memento, an originator, such as an EJB or other object that requires state, creates an instance of a memento and passes it to a caretaker, which manages the object's state on behalf of the originator. Typically, mementos hide the contents of the object from all but the originator of that object. In our case, the originator and caretaker are the same class; however, in a more complex system, we could easily envision an object monitor managing mementoes for us. When the originator needs to restore state, the caretaker provides the memento back to the originator, and the object can then restore.
The Memento pattern is actually a rather simple one to implement in Java. Using inner classes, as shown on lines 04 to 16 of Listing 50.2, we create a memento that contains two methods, a constructor and a restore method, which allows us to reset state using the contents of the moment.
In Listing 50.2, we use the memento by creating two instances representing current state and prior state, as shown in lines 19 to 21.
The value of the memento pattern is most clear when we need to contain a large amount of state. The actual object's state is contained wholly within the memento and not within the object itself. We access the object state throughout a set of state variables, currentState and priorState. Current state is used whenever we perform normal object operations such as getting and setting data. We store object state for later use into the priorState object whenever necessary. In the example, a simple method that changes the object's state, shown on lines 24 to 28, saves the current object's state contents in the priorState memento using the setFrom method. Likewise, line 31 shows how we might restore our state as required.
Our memento example is a rather simple one using predefined variables to contain an object's state. It's easy to imagine a more generic memento implementation that uses reflection to examine an object's content and save and restore state automatically without requiring object-specific code.
Previous << 1 .. 152 153 154 155 156 157 < 158 > 159 160 161 162 163 164 .. 166 >> Next